Skip to content

Instantly share code, notes, and snippets.

@apboobalan
Created July 17, 2020 12:50
Show Gist options
  • Save apboobalan/80bebd2e86a7f98c70a2a6f8a7d022dd to your computer and use it in GitHub Desktop.
Save apboobalan/80bebd2e86a7f98c70a2a6f8a7d022dd to your computer and use it in GitHub Desktop.
defmodule Streamy do
def start do
start = fn -> 0 end
next = fn value -> {[value + 1], value + 1} end
stop = fn value -> value end
Stream.resource(start, next, stop)
end
end
require Integer
Streamy.start()
|> Stream.filter(&Integer.is_even/1)
|> Stream.map(&"#{&1}... ")
|> Enum.take(5)
|> IO.puts()
# This returns 2... 4... 6... 8... 10...
# If we want 0 to be included the start function should return -1.
# This is because only the elements in the list which is returned in the next function tuple are Streamed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment