Solution Review: Join Two Strings
The solution to Challenge: Join Two Strings.
We'll cover the following
Solution
iex> prefix = fn prefix -> fn str -> "#{prefix} #{str}" end end
#Function<erl_eval.6.17052888>
iex> mrs = prefix.("Mrs")
#Function<erl_eval.6.17052888>
iex> mrs.("Smith")
"Mrs Smith"
iex> prefix.("Elixir").("Rocks")
"Elixir Rocks"
-
In the first command, we’re making a
prefix
function that takes a string as a parameter. It returns another function that also takes a string as a parameter. -
In the second command, we’re passing
Mrs
as an argument to theprefix
function. -
In the third command, we’re passing
Smith
as an argument tomrs
function, which will join both strings. -
In the last command, we’re calling the
prefix
function by passing both strings at the same time.
Get hands-on with 1400+ tech skills courses.