hipertracker

hipertracker

How to write idiomatic Elixir for the following code in Ruby? The main difficulty here is that the immutable language cannot accumulate items in such way: if my current item does not meet the condition than I want to get the last item which did it before.

result = []
items.each do |item|
  if condition(item)
    result << {value: item, flag: :ok}
  else
    result << {value: result.last, flag: :previous} 
  end
end

UPDATE: I had an error in the code. I have to find last item from result list.

Showing Posts 1 to 9

bbense

bbense

There’s a couple approaches, if items is always a List, you can write your own reduction. If it might be something else that is still Enumerable, you’ll need to use Enum.reduce. Basically, all “looping” is done with reductions that peel off the head of the loop and then call the function on the tail. If you need state from a previous loop, you put
that in an “acc” or accumulator argument.

Here’s a list based version

def last_true( [], acc,  check_fn)  do
    acc 
end

def last_true( [ head | tail ], acc, check_func ) do 
      case check_func(h) do 
            true -> new_ acc = [ %{value: head, flag: :ok} | acc ]
             -     ->  [ prev_item | _rest ] = acc
                         new_acc = [  Map.put(prev_item, :flag, :previous) | acc ]
      end
      last_true( tail, new_acc, check_func) 
end

Depending on exactly what you want to do, you might need to reverse the output of last_true at this point. There is an optimized erlang function for this case. :lists.reverse. You would call last_true something like this.

   results = last_true(items, [] , &check_fun/1 )
hipertracker

hipertracker OP

I had the error in the example. It should be result.last. This is a dynamic created list, it does not exist outside the main loop. And this is the main difficulty. Maybe I am wrong, but Elixir has no access to the internal built list.

bbense

bbense

That’s exactly what my code does. It builds the list item by item and passes it on to the next step of the reduction. In each step of the recursion, acc contains the previously build version of the results list.

peerreynders

peerreynders

Are you sure? Because to me it looks like
{value: result.last, flag: :previous}

is going to look something like
{value: {value: item, flag: :ok}, flag: :previous}

or worse if nested even further (though maybe there is some subtlety that I’m missing).

Though some of the pattern matching could be relocated

defmodule Test do
  defp new_acc(current, last, acc, check_fn) do
    cond do 
      check_fn.(current) -> 
        [%{value: current, flag: :ok} | acc]
      true -> 
        [%{value: last, flag: :previous} | acc]
    end
  end
  
  defp last_true([], acc, _check_fn),  do: 
    Enum.reverse(acc) 
  defp last_true([head|tail], [last|_] = acc, check_fn ), do: 
    last_true(tail, new_acc(head, last, acc, check_fn), check_fn) 

  def last_true([], _), do: 
    []
  def last_true([head|tail], check_fn), do: 
    last_true(tail, new_acc(head, nil, [], check_fn), check_fn)
    
end
IO.inspect Test.last_true(Enum.to_list(1..10), fn n -> rem(n,2) === 0 end)

.

[%{flag: :previous, value: nil}, %{flag: :ok, value: 2},
%{flag: :previous, value: %{flag: :ok, value: 2}}, %{flag: :ok, value: 4},
%{flag: :previous, value: %{flag: :ok, value: 4}}, %{flag: :ok, value: 6},
%{flag: :previous, value: %{flag: :ok, value: 6}}, %{flag: :ok, value: 8},
%{flag: :previous, value: %{flag: :ok, value: 8}}, %{flag: :ok, value: 10}]
andre1sk

andre1sk

I am not sure that your code matches your desc. The equivalent to your ruby code would be something like:

  items            
  |>Enum.reduce([],  &(chk(&1) && [%{value: &1, flag: :ok}|&2]  
      || [%{value: hd(&2), flag: :previous}| &2] ))
  |>Enum.reverse
peerreynders

peerreynders

Again not accounting for the fact that the first element in the list could fail the condition leading to hd([])

Returns the head of a list, raises badarg if the list is empty.

Nice and short though (personally in this case I think the capture operator messes with readability; just as long as everybody remembers that && and || are short-circuit operators, not boolean operators).

chk = fn n ->  rem(n,2) === 0 end
items = Enum.to_list(1..10)

hd_or_nil = fn [] -> nil ; [head|_] -> head end
items            
|> Enum.reduce([], &(chk.(&1) && [%{value: &1, flag: :ok} | &2]  || [%{value: hd_or_nil.(&2), flag: :previous} | &2]))
|> Enum.reverse
|> IO.inspect

or more verbosely:

defmodule Test do
  defp last([]), do: nil
  defp last([head|_]), do: head
  
  defp check_and_shunt(chk), do:
     fn (item, tail) ->
       cond do
          chk.(item) -> [%{value: item, flag: :ok} | tail]
          true -> [%{value: last(tail), flag: :previous} | tail]
       end
     end
  
  def last_true(items,chk), do:
    items            
    |> Enum.reduce([], check_and_shunt(chk))
    |> Enum.reverse

end
IO.inspect Test.last_true(Enum.to_list(1..10), fn n ->  rem(n,2) === 0 end)
andre1sk

andre1sk

you are totally right :slight_smile:
should be Enum.at() with default nil as opposed to hd.

peerreynders

peerreynders

I know you are talking about Enum.at(list,0) - interestingly enough Enum.at(list,-1) will get the last element of a list - however in general indexed access on a list (short of to the head like you are suggesting) doesn’t feel like idiomatic FP to me.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

What is the correct ruby code you’re trying to emulate?

— All posts loaded —

Where Next? Top

Trending in Questions Top

Hellowman
Hello everyone! I am new to coding with Elixir/Erlang. And i am looking for some utilities for fuzzing testing like AFL++. Does anyone ...
New
yarrichar
I’m very new to Elixir, so sorry if this is a silly question: I am wondering what happens to the mailbox of a process when the machine i...
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
Fl4m3Ph03n1x
Background When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error: * assembling mark...
New
mandar1045
I load tested a plain Phoenix chat app backed by Postgres with 100 million messages in it, everything on default settings, to see what br...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
saleyn
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
261 13183 112
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
charlenopires
Hi everyone :waving_hand: I’ve been working on PhiaUI, an open-source UI component library for Phoenix LiveView, and I wanted to share i...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews