Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elixir's Range for arithmetic progression

Tags:

haskell

elixir

Is it possible to express arithmetic progression in a list without listing them all?

In Haskell, you could do it with the range function.

[2,4..10] == [2,4,6,8,10]

Is there a similar way to do it with Elixir ?

like image 925
Kit Ko Avatar asked Aug 16 '13 14:08

Kit Ko


3 Answers

Stream.iterate/2 does what you want:

Stream.iterate(2, &(&1+2))
like image 55
José Valim Avatar answered Nov 10 '22 06:11

José Valim


You can use Erlang's lists:seq function, from Elixir:

:lists.seq(2,10,2)
like image 33
Riccardo Marotti Avatar answered Nov 10 '22 06:11

Riccardo Marotti


As I can see, there is a Stream.seq() added a month ago:

  • Add Stream.seq() for generating potentially infinite streams of values
  • Add a range/step/seq function
like image 29
Yura Beznos Avatar answered Nov 10 '22 06:11

Yura Beznos