pichi

pichi

How to define recursive anonymous function in Elixir?

How can I define the recursive anonymous function in Elixir? In Erlang, I can write:

1> F = fun X([]) -> []; X([H|T]) -> [H+1|X(T)] end.
#Fun<erl_eval.20.79398840>
2> F([5,41,13]).
[6,42,14]

I can’t find in documentation a way how to write a similar function in Elixir.

First Post!

LostKobrakai

LostKobrakai

Afaik this is not supported in elixir.

Most Liked

John-Goff

John-Goff

I’m not sure I follow honestly. Could you give a more realistic example of what you’re trying to do if the above was too trivial?

Logan

Logan

f = fn
  ([], _fun) ->
    []
  ([h | t], fun) ->
    [h+1 | fun.(t, fun)]
  end

f.([1, 2, 3], f)
John-Goff

John-Goff

In elixir, unlike in Erlang, you can define modules in the shell. So when you do need to write a recursive function you can do

iex> defmodule Rec do
...>   def recurse([]), do: []
...>   def recurse([h | t]), do: [h + 1 | recurse(t)] 
...> end

Last Post!

dom

dom

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
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
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New

Other popular topics 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
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42716 114
New

We're in Beta

About us Mission Statement