daanturo

daanturo

How to invoke a tail-call loop expression like Clojure's loop?

Without defining a function, is there an equivalent of Clojure’s loop or Scheme’s named let where I can run a tail-call loop expression while accumulating multiple values?
There’s Enum.reduce, but the number of iterations may not known in advance, and carrying multiple accumulated values with reduce is clunky.

Thank you in advance.

Edit: provided my concrete usage in reply.

Most Liked

Eiji

Eiji

Often there are multiple ways to solve one problem. You need to give an example of what you want to do including example input, output and what you tried so far, before anybody would give you a satisfying solution.

dimitarvp

dimitarvp

Your Elixir code looks good to me, what’s your issue with it?

One thing I’d change is make it more obvious what part of a tuple you’re getting at the end e.g. not use elem but the then construct – just to make it little more readable.

But other than that the code looks good IMO.

Eiji

Eiji

Having 2-element Tuple as acc is not a shame. Many reduce-based solutions written by senior developers are using it. Your code is really good enough. :+1:

However there is other way to write same code. You can use a simple pattern matching solution, for example:

defmodule Example do
  # function header with default arguments
  def sample(files, bookmarks \\ "", counter \\ 1)

  # when done return only bookmarks string
  def sample([], bookmarks, _counter), do: bookmarks

  # tail recursion
  def sample([file | files], acc_bookmarks, acc_counter) do
  	# your logic goes here, for example
  	{file_bookmarks, file_counter} = process_file(file)
  	# recursively call this for rest files with updated bookmarks and counter
  	sample(files, acc_bookmarks <> file_bookmarks, acc_counter + file_counter)
  end
end

Personally I like separating accumulation and process logic, as in example above, which makes the code more clear.

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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

Other popular topics Top

New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 55125 245
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
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
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44778 311
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 44265 214
New

We're in Beta

About us Mission Statement