5

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 ?

Kit Ko
  • 315
  • 2
  • 7

3 Answers3

6

Stream.iterate/2 does what you want:

Stream.iterate(2, &(&1+2))
Juliano
  • 2,402
  • 1
  • 20
  • 22
José Valim
  • 50,409
  • 12
  • 130
  • 115
5

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

:lists.seq(2,10,2)
Riccardo Marotti
  • 20,218
  • 7
  • 70
  • 53
1

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

Yura Beznos
  • 546
  • 4
  • 9