Idiomatic Elixir for collecting last item from dynamic list

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}
    result << {value: result.last, flag: :previous} 

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

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

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 ]
      last_true( tail, new_acc, check_func) 

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 )

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.

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.

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]
  defp last_true([], acc, _check_fn),  do: 
  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)
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}]

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

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

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
|> 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]
  def last_true(items,chk), do:
    |> Enum.reduce([], check_and_shunt(chk))
    |> Enum.reverse

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

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

I know you are talking about,0) - interestingly enough,-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.

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