# Writing anonymous functions in Elixir # this shows writing and use of anon funcs # gtowell # July 18, 2022 # NOTE # IO is the name of a package # puts is the name of a function in the IO package # "puts" --> PUT String # Anon func with no args # Can put () before the -> but it is not required a = fn -> 7 end # Call to anon fun. Note the "." after the variable name # Also note that IO.puts is a function. Can wrap the arg in () # but that is optional # general consensus is to use the parens IO.puts a.() IO.puts(a.()) # Anon func with one arg b = fn (x) -> 7*x end IO.puts b.(7) #Anon fun with 2 args # Note the ; to separate expressions # CR-LF can also separate expressions c = fn (x,y) -> z=x+2; z*y end IO.puts c.(3,4) cc = fun(x,y) -> z=x+2 z*y end # Anon fun with three args and it is written on multiple lines d = fn (x,y,z) -> zz = z+3 x*y*zz end IO.puts d.(2,3,4)