aboutsummaryrefslogtreecommitdiff
path: root/lib/util/stream.ex
diff options
context:
space:
mode:
Diffstat (limited to 'lib/util/stream.ex')
-rw-r--r--lib/util/stream.ex19
1 files changed, 19 insertions, 0 deletions
diff --git a/lib/util/stream.ex b/lib/util/stream.ex
new file mode 100644
index 0000000..b143bc1
--- /dev/null
+++ b/lib/util/stream.ex
@@ -0,0 +1,19 @@
+defmodule Thulani.Util.Stream do
+ def take_until(enum, fun) when :erlang.is_function(fun, 1) do
+ lazy(enum, fn f1 ->
+ fn entry, acc ->
+ if(fun.(entry)) do
+ {:cont, result} = f1.(entry, acc)
+ {:halt, result}
+ else
+ f1.(entry, acc)
+ end
+ end
+ end)
+ end
+
+ @compile {:inline, lazy: 2}
+
+ defp lazy(%Stream{done: nil, funs: funs} = lazy, fun), do: %{lazy | funs: [fun | funs]}
+ defp lazy(enum, fun), do: %Stream{enum: enum, funs: [fun]}
+end