user3357505
user3357505

Reputation:

Change Enum.map from Elixir to Erlang

I need to convert Elixir function into Erlang function: In Elixir I have:

Enum.map(0..n, fn i-> fun(i) end)

And I need to re-write to Erlang.

Any Idea? Thanks

Upvotes: 2

Views: 316

Answers (2)

Hynek -Pichi- Vychodil
Hynek -Pichi- Vychodil

Reputation: 26141

Using list comprehensions:

[ F(X) || X <- lists:seq(0, 10) ].

aka

[ X*X || X <- lists:seq(0, 10) ].

Upvotes: 2

Dogbert
Dogbert

Reputation: 222428

Erlang doesn't have a single generic function that can handle mapping over any data structure like Enum.map in Elixir. The simplest way to do this would be to use lists:seq to generate the list and lists:map:

1> lists:map(fun(X) -> X * X end, lists:seq(0, 10)).
[0,1,4,9,16,25,36,49,64,81,100]

Upvotes: 4

Related Questions