Skip to content

Instantly share code, notes, and snippets.

@JamesTheAwesomeDude
Last active March 14, 2024 18:02
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 JamesTheAwesomeDude/f593ecbfb53fc416296ebb1a0fd7b85b to your computer and use it in GitHub Desktop.
Save JamesTheAwesomeDude/f593ecbfb53fc416296ebb1a0fd7b85b to your computer and use it in GitHub Desktop.
Elixir lazy iterate over byte string (aka "binary" type)
defmodule BitStringStream do
def binary_to_stream(s) do
Stream.unfold(s, fn
<<next::8, rem::binary>> -> {next, rem}
<<>> -> nil
end)
end
def bitstring_to_stream(s, n) do
Stream.unfold(s, fn
<<next::bitstring-size(n), rem::bitstring>> -> {next, rem}
<<>> -> nil
end)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment