aboutsummaryrefslogtreecommitdiff
path: root/lib/util/stream.ex
diff options
context:
space:
mode:
authorNathan Perry <np@nathanperry.dev>2024-08-08 14:53:04 -0400
committerNathan Perry <np@nathanperry.dev>2024-08-08 14:53:04 -0400
commit0659c3be4e996ca97a1a9d02142f2809507d2b38 (patch)
treea80381a8092cdb4587c0623921f355308408ede5 /lib/util/stream.ex
parent7ccdb63fb7f4008b7d785c687b4f55b6b4291483 (diff)
voice playback workselixir
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