Notes
?≠returns a codepoint:8800.Dividing two integers returns a float. Use
divandtruncto get integer results.In Elixir,
nilisfalse.Regular expressions are based on PCRE (Perl lives on!).
=~performs a regular expression matchiex> str = "once upon a time" iex> str =~ ~r/u..n/ true=~also accepts a string as its right hand argument, in which case it returns true if the left string contains the right.Lists are not arrays. You cannot calculate an offset to a specific element.
map[:missingKey]returnsnil.map.missingKeyraises an exception.
Exercises
Strings
Interpolate current date/time using double-quoted string and a sigil.
iex> now = Time.utc_now iex> "The time is approximately #{now.hour}:#{now.minute} UTC." "The time is approximately 13:34 UTC." iex> ~s/The time is approximately #{now.hour}:#{now.minute} UTC./ "The time is approximately 13:34 UTC."Use IO.puts to output:
1 + 2 = #{ 1 + 2 }iex> IO.puts(~S"1 + 2 = #{ 1 + 2 }") 1 + 2 = #{ 1 + 2 } :okConvert string into a list of words.
iex> ~w/now is the time/ ["now", "is", "the", "time"]
Regular Expressions
Return
trueif string contains ana, followed by any single character, followed by ac.iex> str =~ ~r/a.c/ trueReplace every occurrence of
cat withdog`.iex> Regex.replace(~r/cat/, "double cat cats", "dog") "double dog dogs"Replace only the first occurrence:
iex> Regex.replace(~r/cat/, "double cat cats", "dog", global: false) "double dog cats"
All notes and comments are my own opinion. Follow me at @rgacote@genserver.social