Created
July 17, 2020 12:50
-
-
Save apboobalan/80bebd2e86a7f98c70a2a6f8a7d022dd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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