-
-
Save Qqwy/e8fbabc496e4ffe05d75e8770f4323ce to your computer and use it in GitHub Desktop.
This file contains 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 StreamTest do | |
def shift(stream) do | |
case Enumerable.reduce(stream, {:cont, :first}, fn | |
val, :first -> {:suspend, val} | |
val, _ -> {:cont, val} | |
end) do | |
{:done, nil} -> nil | |
{:done, final} -> nil | |
{:halted, final} -> {final, fn _,_ -> {:halt, nil} end} | |
{:suspended, val, continuation} -> {val, fn _,_ -> continuation.({:cont, :first}) end} | |
end | |
end | |
end | |
x = [1,2,3] | |
# x = Stream.iterate(0, &(&1+1)) | |
{_, x} = StreamTest.shift(x) | |
{_, x} = StreamTest.shift(x) | |
{_, x} = StreamTest.shift(x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment