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.

https://hexdocs.pm/lazy_for

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 Libraries Top

seancribbs
Today I released a new dialyzer Mix task as the dialyzex package! At the time we started writing this task, the existing dialyzer integra...
New
woylie
I released Doggo, a collection of unstyled Phoenix components. Features Unstyled Phoenix components. Storybook that can be added to...
New
arkgil
Hi all! I’m happy to announce that Telemetry v0.3.0 is out! This release marks the conversion from Elixir to Erlang so that all the libr...
New
pkrawat1
Presenting Aviacommerce, open source e-commerce platform in Elixir Aviacommerce is an open source e-commerce platform in Elixir. We at...
New
achempion
Hi, I would like to tell about my initiative to further maintain and develop Waffle project which is the fork of Arc library. The progre...
New
Antrater
Hi everyone! I’m thrilled to announce a huge thing. We have been developing Elixir Moon Design System for quite a while. We are finally ...
New
engineeringdept
I’ve just released the first version of Snap, an Elasticsearch client. It borrows ideas about application structure and process managemen...
New
bryanjos
Hi, I just published version 0.23.0 of Elixirscript. Most of the changes are around JavaScript interop now that Elixirscript uses the ...
New
Hal9000
Here is my first stab at this. README pasted below. https://github.com/Hal9000/elixir_random Comments and critiques are welcome. Th...
New
maltoe
Hello! Came here to announce ChromicPDF, a pet project PDF generator I’ve been working on for the past few months. Why another PDF gener...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New

Sub Categories:

We're in Beta

About us Mission Statement