Morning!
månmån
have fallen down the Algebraic Effects 🐰-hole
you could use multimethod with methods with match-all varargs, right ? (it's been a while - can you use varargs in defmethod?) and then run the pattern a second time to get bindings but you'd have to run the patterns twice - once to find the dispatch value, and again for bindings
pattern looks great... has you seen anything which uses it to match function parameter lists and bind variables ? i.e. choose from a list of function implementations based on which has matching parameters ?
No but I've seen a library that did that with core.match
Another username I remember, killme2008, hmm,, let's check out his Github oh, the CEO of greptime db, neato cheeto https://github.com/killme2008/defun
First thing I did on my first Clojure job was hack on his carmine extension library
> https://github.com/killme2008/defun that could work!
choose from a list of function implementations based on which has matching parameters? multimethods?!?
hmm... well multimethods dispatch on a single value, so you could use a pattern match to get that value and then dispatch the multimethod and destructure in the params for bindings - but then you've effectively got two patterns of different syntaxes for each function, which feels quite awkward and error prone
I do find Elixir's pattern matching function heads (and case) much easier to use (and comprehend when reading code) than multimethods... but there's no particular reason clojure can't have something very similar
I think it becomes an issue with method order resolution, where in regular pattern matching implementations collisions are resolved in order while Clojure will just barf at you or exhibit inconsistent behavior
You'll also run into some difficulties with parsing and default values. Pattern matching complects the dispatch values with the shape of the parsed object but with mm you'll have to work twice (I think)
multimethods dispatch on the value of a function of any parameters, not on a single value, so they could be what you need.
it has to evaluate a function to get that dispatch value, which would be a pattern-match - then, it has to bind variables in the implementation - which would be another run of the same pattern-match - so I think it would have to run the match twice
the killme2008/defun approach seems simpler
You can try implementing it with multimethods, but the second you get to destructuring patterns you're going to have a bad time. Or even wildcards with two args. Give it shot
Unison?
no, I followed the 🐰 even deeper, and rolled my own 😬
in https://github.com/mccraigmccraig/freyja rather than Clojure, 'cos that's where the dayjob is atm, but Elixir is sooooo similar to Clojure I'm wondering how easy an automatic port would be - Elixir has hygienic macros, pesistent data-structures, protocols... the main challenge would probably be around pattern-matching, which Clojure doesn't have
We have pattern matching as five libraries. Pick one 😁
I'm curious, will read your lib
Something in algebraic effects always reminds me of the free monad
there is both a Free monad (for the higher-order effects) and a Freer monad (for the first-order effects) hiding inside that lib 🙂
but the first rule of monad club (in both Elixir and Clojure land) is: we don't talk about monads 🙂
> We have pattern matching as five libraries. Pick one
I've only tried core.match and didn't really get on with it - and iirc it doesn't support function-head matching... have you found a pattern match lib you like ?
Pattern is nice but don't use it for job interview take him tasks 😉 Wow, I even remembered the author https://github.com/pangloss/pattern
Gem library tbh