News for hackers. Stuff that matters.
Re:Functional programming with bananas, lenses, envelopes an... by sfvisser on Tuesday April 26, 2022 @02:40AM (#31164671)
These patterns might seem abstract and obscure at first, but have proven tremendously powerful in practice to me.

The current project I’m working on in Rust has a big recursive data type with quite some branches and different instantiations of recursive positions. Box/Vec/Option etc.

By parametrizing the enum with a type variable and tying the knot with a type level fixed point combinator gives you back the originally intended data type. But now suddenly you can put annotations in the recursive positions as well, use generic cata/para and other higher level combinators. Fun and powerful stuff!

(The lack of HKT in Rust can be challenging, but it’s mostly easy to work around it)

Re:Functional programming with bananas, lenses, envelopes an... by charleskinbote on Tuesday April 26, 2022 @07:44AM (#31166250)
Is there any example code you'd be able to point to? I'd have thought this would be difficult without something like haskell's Fix
Re:Functional programming with bananas, lenses, envelopes an... by sfvisser on Tuesday April 26, 2022 @04:43PM (#31172911)
No open source code base I can point to right now unfortunately.

But this is fun stuff, so I couldn't help creating a simple gist to demonstrate the concept: https://gist.github.com/sebastiaanvisser/cd6ee46356e3ecc8945...

The trick is to keep all the combinators specific to your data type. In Haskell you can derive your functor (and applicative/foldable/traversable) and have a single cata to rule them all. In Rust you build them specifically for every datatype. Less neat, but could still be totally worth it!

Re:Functional programming with bananas, lenses, envelopes an... by charleskinbote on Wednesday April 27, 2022 @07:44AM (#31178709)
This is great, thanks for taking the time to flesh it out