What is the idiomatic way to generate a stream of numbers in Elixir?

I currently have the following generators:

(for i <- 999..100, j <- i..100, into: [], do:  i * j)
|> Stream.filter(&(palindromic?(&1)))
|> Enum.sort
|> List.last

Is there a way to generate a product stream instead?

+4
source share
1 answer

Let's see what we need to do:

  • for each number I'm between 999 and 100
  • for every number j between i and 100
  • take the product i * j

The easiest way to create such a nested enumeration is to create a nested list of lists and then smooth it out or use the function immediately flat_map:

Stream.flat_map(999..100, fn i -> Stream.map(i..100, fn j -> j * i end) end)
+5
source

Source: https://habr.com/ru/post/1629804/


All Articles