Elixir Hidden Gems

Did you find any Hidden Gems in elixir? '

if true, do: "Please Share "

Helps to Elixir Beginners

What I found so far are as followed;

Printing the Lists and maps

IO.puts "This is the data #{inspect list}"
IO.puts "This is the data #{inspect map}" 

Joining Lists

[1,2,3] ++ [4,5,6]

Updating Map

map = %{name; "blackode", age: "not human" }
updated_map = %{map | name: "john",age: "23"}
#we can update map like above .

hd and tl defs

hd [1,2,3] #output as 1
tl [1,2,3] #output as [2,3]

String Concatenation

"Hello" <> "Blackode" <> "!" #output as HelloBlackode!

Default Values

map = %{name: "john"}
map #output as %{name: "john"}
map[:age] #outputs as :nil
map[:age] || 23 #output as 23
map #output as %{name: "john"}

List Comprehensions

for x<- [1,2,3], do: {x,x+1}  #output as [{1, 2}, {2, 3}, {3, 4}]
# we can also do pattern Matching here
for {x,y} <- [{1,2},{3,4}], do: x+y #output as [3,7]

Share your Hidden Syntaxes or Gems :bouquet:


Well, you got me confused. I don’t think it gives another map with a new key. There is only one map %{name: "john"} and it stays unchanged, what is different is the final output of the expression map[:age] || 23.

1 Like

Sorry for the confusion… :107: