benhoven

benhoven

Use case for Enum.reduce_while without accumulator

Hi Everybody,

Recently I discovered interesting use case for reduce_while that does not use acc as accumulator → it doesn’t carry / use the variable in subsequent loops.

Could you please take a look and tell me if it’s clever or stupid?

Enum.reduce_while(some_enumerable, {:error, :show_this_when_empty_input}, fn some_item, _ ->
  with {:ok, something} <- something(some_item),
       {:ok, something} <- something_else(something) do
    {:halt, {:ok, something}}
  else
    # known error - continue OR finish with this error
    {:error, :expected_message} = error -> {:cont, error}

    # unknown error - halt immediately
    {:error, _} = error -> {:halt, error}
  end
end)

What I can get from that is:

  1. {:error, :show_this_when_empty_input} when enumerable is an empty list
  2. {:error, :expected_message} when no element in enumerable passes the test (the function) in 3rd argument
  3. {:ok, result} first positive result from the function → first element that passes and then stop processing
  4. {:error, anything} for results / errors that are not expected

Marked As Solved

Eiji

Eiji

Sorry, I did not get it. Since we accept any function and expect not nil all mentioned clauses could be changed to return “something” like {:error, error} or {:ok, result} and optionally for {:error, :expected_message} we may want to return nil in order to skip specific item.

If you want you can also write a really simple few-lines function like:


defmodule Example do
  def sample(list, default \\ nil, func)

  def sample([], default, _func), do: func

  def sample([head | tail], default \\ nil, func) do
    case func.(head) do
      {:ok, something} -> something
      {:error, :expected_message} = error -> sample(tail, error, func)
      {:error, _} = error -> error
    end
  end
end

and use it like:

Example.sample(some_enumerable, {:error, :show_this_when_empty_input}, fn some_item, _ ->
  with {:ok, something} <- something(some_item),
       {:ok, something} <- something_else(something) do
    {:ok, something}
  else
    {:error, _} = error -> error
  end
end)

Look that in case clauses you may simply add support for {:halt, acc} and {:cont, acc} if you do not want to change anonymous function from an original post. You may even combine Example.sample/3 with such anonymous function.

Also Liked

kokolegorille

kokolegorille

I would also try to move the code to Enum.find, or Enum.find_value, but the condition is more complex, and not really a truthy kind of condition.

It’s hard to capture the with, with Enum.find :slight_smile:

wolf4earth

wolf4earth

I’ve written code similar to this, so yeah, I think that’s a perfectly reasonable usage of Enum.reduce_while. :slight_smile:

Eiji

Eiji

Your code looks good except one point. Calling Enum.reduce* functions does not makes sense if you do not need an accumulator. I would rather write something like:

func = fn
  # return error if needed - equivalent of: {:halt, {:error, error}}
  x when rem(x, 2) != 0 -> {:error, 2}
  # skip item if needed - equivalent of: {:cont, acc}
  x when rem(x, 3) != 0 -> nil
  # return ok tuple for desired item - equivalent of: {:halt, {:ok, item}}
  x -> {:ok, x}
end

iex> Enum.find_value(1..3, func)
{:error, 2} # error returned
iex> Enum.find_value([2, 4], func)
nil # all skipped
iex> Enum.find_value([2, 4, 6], func)
{:ok, 6} # ok returned

For more information please take a look at Enum.find_value/2 documentation.

kokolegorille

kokolegorille

I know recursion could solve this problem quite elegantly.

What I meant is Enum.find_value is not working well in that case.

Because the condition should exit only if ok, or unexpected error… and continue if the error is known. But You need to return nil/or false instead, to continue iteration :slight_smile:

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&amp;query=perfume&amp;page=2, I would like to get: ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
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
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
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

We're in Beta

About us Mission Statement