PragTob

PragTob

Hey everyone!

This is a multi-layered question and I’ll try my best to try and phrase it cleanly.

As an abstract use case I want to have a function that is adjustable in its behaviour by a caller by passing in an optional function (think Map.merge/3). This function is used to determine what my function does - but it’s not all, it is wrapped and calls a Protocol function on a specific return value. This protocol function might want to call the previous method, hence it requires me to pass along the initially user supplied function… I’d much prefer to be able to create an anonymous function that already is the “wrapped” version of the user supplied function but can’t seem to do it because of recursion…

in code:

  # in the module MyModule
  def original_method(argument, user_function) do
    val = user_function.(argument)
    case val do
       @continue_symbol ->
        MyProtocol.continue(argument, user_function)
      _anything ->
        val
    end
  end

  # MyProtocol
  def continue(argument, fun) do
    resolver = fn(argument) -> original_method(argument, fun) end
    Stdlib.function(argument, resolver)
  end

I’d love the whole of original_method to be an anonymous function by itself (built from my code + the user supplied function), but it seems impossible as it would have to pass itself along to the function it calls on the protocol.

It just feels strange overall. I can’t have it as a nice already wrapped anonymous function and even in the protocol I seem to always have to build my own resolver function calling back to original_method.

Does anyone know of a better way to do this? The concrete code can be found here and the two methods are basically DeepMerge.Integration.do_deep_merge (still working on naming…) and the implementations for DeepMerge.Resolver.resolve. The use case is basically that normally the protocols for each data type know how to resolve the deep_merge, but the user has a shot at changing that behaviour should she choose to (e.g. don’t merge lists).

Thanks a lot for reading this far and helping out, any feedback appreciated :slight_smile: !
Tobi

Showing Posts 1 to 10

OvermindDL1

OvermindDL1

Hmm, can you give an example of the code as you want it to look and what error you get and on what line so we can see what is happening? :slight_smile:

tomekowal

tomekowal

This is a known problem in functional languages. How to make a recursive anonymous function?
Even something as simple as factorial is hard to write:

fac = fn(0) -> 1
            (n) -> fac.(n-1) * n end
** (CompileError) iex:2: undefined function fac/1

There are topics abou it on Erlang mailing list and there is this in particular:

and it shows a trick to get around that limitation:

fac = fn(0, _f) -> 1
            (n, f) -> f.(n-1) * n end
factorial = fn(n) -> fac.(n, fac) end
factorial.(5)

The good news is this workaround is easy. The bad news is that you would have to teach it to every single user of your library (or just use named functions).

PragTob

PragTob OP

Thanks for sharing this great trick! On the one hand, wished I could have come up with it myself (as I did something like it) but at the same time it’s genius. Had to adapt it a bit as Map.merge/3 expects a function with arity 3.

If anyone is interested, this is the commit.

Thanks a ton once again!

OvermindDL1

OvermindDL1

That style is called the Y-Combinator. There are many kinds of combinators, should look them up. :slight_smile:

PragTob

PragTob OP

Thanks! Definitely still need to brush up my FP (only dabbled in Clojure, LISP and Scala a bit before) which is why Elixir is so great to pick up and learn as I can expand my mind on FP, distributed systems/OTP and many other great concepts.

To my shame, before I mistook “Y combinator” for the general “we derive the whole of computation from just functions” which I think is rather called lambda calculus :sweat_smile:

A bit OT, but if you have some good resource to share (book or web) for learning about “ALL THE COMBINATORS” I’d appreciate it. Maybe I should dig out Land of LISP again…

OvermindDL1

OvermindDL1

No need for lisp, just normal functional stuff. :slight_smile:

I usually just point people to the wikipedia article, short, to the point for each combinator: Fixed-point combinator - Wikipedia

For Elixir there are libraries that simplify the combinators as well, such as quark, where the Y-Combinator is named fix for being a fixed-point combinator, but it shows a lot of other non-fixed point combinators as well (that same author makes a lot of awesome libraries like that, especially for functional design, click their name on the lower-right of that page too, especially as a lot of their libs are designed to be used together). :slight_smile:

gregvaughn

gregvaughn

Here’s a great resource to learn about combinators in general, if Ruby as a sample language works for you: Kestrels, Quirky Birds, and Hopeless Egocentricity

OvermindDL1

OvermindDL1

That is fascinating. Ruby uses combinators everywhere.

Also, wtf?!?

[1,2,3,3,4,5].uniq!
  => [1,2,3,4,5]

[1,2,3,4,5].uniq!
  => nil

Uh… wtf?! A prime example of why I like statically typed languages, this oddness would be encoded in the type instead of being, just… wtf…?

gregvaughn

gregvaughn

I’m not sure what static typing has to do with this situation. I think of that wtf as a mutability issue. If you use the non-bang version of uniq you’ll find the result less confusing. I’ve found myself avoiding bang methods in Ruby more and more as my experience grows.

irb(main):001:0> a = [1,2,3,3,4,5]
=> [1, 2, 3, 3, 4, 5]
irb(main):002:0> b = [1,2,3,4,5]
=> [1, 2, 3, 4, 5]
irb(main):003:0> a.uniq
=> [1, 2, 3, 4, 5]
irb(main):004:0> b.uniq
=> [1, 2, 3, 4, 5]
irb(main):005:0> a.uniq!
=> [1, 2, 3, 4, 5]
irb(main):006:0> b.uniq!
=> nil
irb(main):007:0> a
=> [1, 2, 3, 4, 5]
irb(main):008:0> b
=> [1, 2, 3, 4, 5]
irb(main):009:0>

uniq! modifies the array in place and returns the modified version or else it’s already unique and it returns nil to indicate that. It’s a horribly confusing method. uniq just returns a new array with unique elements.

OvermindDL1

OvermindDL1

I noticed the non-bang was consistent yep, but mutating methods or not, returning a value should be fairly consistent, after all ‘why’ did it return nil in the case it did not make changes, yet it returned self when it did, why not always return self so you can chain?

That really really badly should have had a new name indeed, that seems so counter-intuitive and internally inconsistent compared to the surrounding methods (Principle of Least Surprise is violated! :astonished_face:). If anything it should have returned true or false, not self or nil. o.O

Where Next? Top

Trending in Questions Top

axelson
One of my LiveView view’s has a very heavy javascript dependency so I want to avoid loading it for every page on the site, while still us...
New
yasoob
Hi all! I know that quite a few people use Phoenix LiveView but it is hard to get a more up-to-date list of folks/companies using it. I ...
New
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
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
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
silverdr
I considered using Cloak and Cloak Ecto for encryption-at-rest but I noticed that both seem like abandonware these days. Moreover there a...
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
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
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
PJUllrich
On November 23, 2026, an unauthorized third party exploited a vulnerability in hex.pm (CVE-2026-129514, CVSS 10.0) that let them run comm...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews