Elixir: recording a parameterized function using and designation

Is it possible to write a parameterized function using and designation?

Here is an example of a parameterized function from Dave Thomas's book "Elixir of Programming"

title = fn (title) -> ( fn (name) -> title <> " " <> name end ) end mrs = title.("Mrs.") IO.puts mrs.("Rose") 

The output of the program above:

 Mrs. Rose [Finished in 0.6s] 

Can title be written using and notation? An example and notation are given below.

 iex> square = &(&1 * &1) #Function<6.17052888 in :erl_eval.expr/5> iex> square.(8) 64 
+5
source share
2 answers

As @Gazler correctly suggested that this is not possible due to syntax limitations, but you can achieve a similar result with a partial application. The difference here is that the title function will return the result directly, instead of returning the function. The mrs function can then execute a partial application by "pre-populating" the first argument.

 title = &(&1 <> " " <> &2) mrs = &title.("Mrs.", &1) IO.puts mrs.("Rose") 
+9
source

It will be impossible. Attempting to use capture function syntax ( & ) in a function declared with capture syntax will raise a CompileError

 iex(1)> &(&(&1)) ** (CompileError) iex:2: nested captures via & are not allowed: &&1 (elixir) src/elixir_fn.erl:108: :elixir_fn.do_capture/4 (elixir) src/elixir_exp.erl:229: :elixir_exp.expand/2 (elixir) src/elixir_exp.erl:10: :elixir_exp.expand/2 
+6
source

All Articles