mudasobwa

mudasobwa

Creator of Cure

LazyFor → `Kernel.SpecialForms.for/1` but returning stream

The stream-based implementation of Kernel.SpecialForms.for/1. Allows the same syntax as for and acts the same way. This is an initial release. It does not yet support :into , :uniq, and :reduce options and bitstring comprehensions.

Everything else is supported.

Examples:

iex> import LazyFor

iex> # A list generator:
iex> result = stream n <- [1, 2, 3, 4], do: n * 2
iex> Enum.to_list(result)
[2, 4, 6, 8]

iex> # A comprehension with two generators
iex> result = stream x <- [1, 2], y <- [2, 3], do: x * y
iex> Enum.to_list(result)
[2, 3, 4, 6]

iex> # A comprehension with a generator and a filter
iex> result = stream n <- [1, 2, 3, 4, 5, 6], rem(n, 2) == 0, do: n
iex> Enum.to_list(result)
[2, 4, 6]

iex> users = [user: "john", admin: "meg", guest: "barbara"]
iex> result = stream {type, name} when type != :guest <- users do
...>   String.upcase(name)
...> end
iex> Enum.to_list(result)
["JOHN", "MEG"]

Most Liked

mudasobwa

mudasobwa

Creator of Cure

Slightly moving forward. Simple binary comprehension and options (all but :reduce) do work.

iex|1 ▶ Enum.to_list(stream <<x <- "abcabca">>, do: x)
'abcabca'
iex|2 ▶ Enum.to_list(stream <<x <- "abcabca">>, uniq: true, do: x)
'abc'
iex|3 ▶ stream <<x <- "abcabca">>, take: 2, do: x                 
'ab'

Also :into works as a side effect (as described here)

I am not sure I am into implementing full bitstring support, but :reduce will come soon.

mudasobwa

mudasobwa

Creator of Cure

Well, the latest hot discussion about semi-mutable not actually mutable syntactic sugar reminded me of my tech debt and I implemented reduce:.

  test "binary for comprehensions with reduce, generators and filters" do
    bin = "abc"

    acc =
      stream <<x <- bin>>, Integer.is_odd(x), <<y <- "hello">>, reduce: %{} do
        inner_acc -> Map.update(inner_acc, x, [y], &[y | &1])
      end

    assert acc == %{97 => ~c"olleh", 99 => ~c"olleh"}
  end

Under the hood, the slightly modified inner clause is applied to terminate the stream. Enjoy.

Where Next?

Popular in Announcing Top

OvermindDL1
Been making an MLElixir thing (not released yet…) for fun in spare time in the past day. I’m just trying to see how much I can get an ML...
132 14410 106
New
OvermindDL1
I created a new library (rather I pulled out a couple files from my big project), it manages an operating system PID file for the BEAM. ...
New
kelvinst
Hey everyone! Well, we made this lib a while ago and now we decided to finally go out and public with it! It’s a tool for creating and m...
New
tmbb
I’ve been working on two packages (not on hex.pm yet) to build admin interfaces for phoenix apps: bureaucrat - which contains a bunch ...
New
RobertDober
Earmark is a pure-Elixir Markdown converter. It is intended to be used as a library (just call Earmark.as_html), but can also be used as...
239 12978 134
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New

Other popular topics Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement