Skip to content

An experiment in getting Elixir comprehensions to return streams

Notifications You must be signed in to change notification settings

brettbeatty/stream_comprehension_elixir

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

StreamComprehension

StreamComprehension allows for comprehensions to build streams instead of lists.

Sometimes when working with streams I find myself longing for the versatility of list comprehensions. This project is an attempt to have the best of both worlds (for fun; no intent to use this in production).

Usage

You should be able to use stream comprehensions the same as regular comprehensions (less the :into and :reduce options because that defeats the point), but the output will be a stream instead of a list.

import StreamComprehension

input = Stream.cycle([ok: "twenty-three", error: "forty-two", ok: "nineteen"])

my_stream =
             # only use words from :ok tuples
  stream for {:ok, word} <- input,
             # for demonstration purposes only; always truthy
             send(self(), {:word, word}),
             # show off bitstring generator
             <<character <- word>>,
             # filter on character being in lowercase alphabet
             character in ?a..?z do
    character
  end
#=> #Function<59.58486609/2 in Stream.transform/3>

flush()
#=> :ok

Enum.take(my_stream, 38)
#=> 'twentythreenineteentwentythreenineteen'

flush()
# {:word, "twenty-three"}
# {:word, "nineteen"}
# {:word, "twenty-three"}
# {:word, "nineteen"}
#=> :ok

About

An experiment in getting Elixir comprehensions to return streams

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages