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 9 to 1

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

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

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.

andre1sk

andre1sk

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

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

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

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}]
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.

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

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 )
— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews