4

The following Elixir code is incorrect but conveys (I think) the desired result:

defmodule Question do
  def dbl(n), do: n * 2
  def trp(n), do: n * 3

  def consumer(xs, f) do
    Enum.filter(xs, f.(x) > 5)
  end
end

Question.consumer([1, 2, 3], dbl) # [3]
Question.consumer([1, 2, 3], trp) # [2, 3]

How should the consumer method be written to consume dbl and trp correctly? And then how would you call it?

Thank you!

EDIT:

A related question please. How would you write and call the Scala code below in Elixir:

def dbl(n: Int): Int = n * 2
def trp(n: Int): Int = n * 3

def consume(xs: List[Int], f: (Int) => Int): List[Int] =
  xs.filter(x => f(x) > 5)

consume(List(1, 2, 3), dbl) # List(3)
consume(List(1, 2, 3), trp) # List(2, 3)

(Thank you) * 2

1 Answer 1

8

The Elixir equivalent of Scala's x => f(x) > 5 is fn x -> f.(x) > 5 end. This is how you use it:

defmodule Question do
  def dbl(n), do: n * 2
  def trp(n), do: n * 3

  def consumer(list, f) do
    Enum.filter(list, fn x -> f.(x) > 5 end)
  end
end

You can then call it using:

Question.consumer([1, 2, 3], &Question.dbl/1)   # => [3]
Question.consumer([1, 2, 3], &Question.trp/1)   # => [2, 3]

Additional Notes:

  • You can also use the short-hand &(f.(&1) > 5) instead of the full function
  • Notice the & and /1 - You need to pass a complete reference to the named module methods. See the Elixir guide on the Function captures.
  • On the other hand, if you make the dbl and trp functions anonymous, you can pass them directly as arguments:

    dbl = fn n -> n * 2 end
    trp = fn n -> n * 3 end
    
    Question.consumer([1, 2, 3], dbl)   # => [3]
    Question.consumer([1, 2, 3], trp)   # => [2, 3]
    
  • For reference, read: Why are there two kinds of functions in Elixir?

3
  • Thank you for your answer. Would you have a look at the edit please. I can't figure out how to use Enum.filter with a passed-in function. Enum.filter(xs, f > 5) does not seem to work?
    – ericky
    Mar 31, 2017 at 18:04
  • I only want to filter the collection using the supplied function - no mapping - so the scala output of [2] and [2, 3] is what I'm after.
    – ericky
    Mar 31, 2017 at 18:13
  • Wonderful. Thank you for your quick help - and just what I was looking for!
    – ericky
    Mar 31, 2017 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.