Skip to content

Instantly share code, notes, and snippets.

@Qqwy
Last active March 25, 2017 01:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Qqwy/e8fbabc496e4ffe05d75e8770f4323ce to your computer and use it in GitHub Desktop.
Save Qqwy/e8fbabc496e4ffe05d75e8770f4323ce to your computer and use it in GitHub Desktop.
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