Case statement to anonymous function

Hi,

Can I get some help writing this case statement as anonymous function?

signal = Enum.random([:green, :red])
value = 
case signal do
:green -> "go"
:red -> "stop"
end

Thanks in advance :slight_smile:

Make your Elixir look like Javascript:

value =
  fn
    :green -> "go"
    :red -> "stop"
  end.(signal)

It rhymes with the case statement :slight_smile:

(edit: adjusted the variable names to match the example code)

3 Likes

@al2o3cr Can I know how to call this function and also what does .(value) do?

I adjusted the code above - the value name was from my own iex session, it should have been signal to match the case statement.

Thanks!!! :smiley: