Stream: beginners

Topic: Applicative functors in Roc


view this post on Zulip Asier Elorz (he/him) (Dec 15 2023 at 09:46):

Applicative functors are a subset of functors (and superset of monads) that are very useful. At the very core, we can think about them as extending fmap (or map) for functions with more than one parameter, being able to call them with more than one value of the functor type.

In functional languages, applicative functors have traditionally relied on partial function application (currying). The idea is that you lift a function with more than one parameter with pure (or just, succeed, etc...) and then apply each parameter. In haskell, you do this with the <*> operator. In elm, you can do this with |> and functions that perform the application. A good example of this is the elm-json-decode-pipeline library.

In languages that have no partial application but have variadics, one can implement applicative functors with variadics, by having a function take, in a single call, the function with many parameters and all the arguments. Ben Deane explains how to achieve this in C++ in this talk.

Roc doesn't have partial function application, and it doesn't have variadics. So I don't know how I would design an API around an applicative functor in Roc. This is disappointing because applicative functors are a great API choice for some specific tasks, such as json decoding, even more for pure functional languages, and it would be great to have them available in Roc.

So, my question is, what is the way in which one should write an applicative functor in Roc? Has this been thought about? Is it possible? Is it desirable or should one look for other patterns when designing APIs in this language?

view this post on Zulip Luke Boswell (Dec 15 2023 at 10:22):

I think this example shows how to write applicative functors in Roc https://www.roc-lang.org/examples/RecordBuilder/README.html

view this post on Zulip Asier Elorz (he/him) (Dec 15 2023 at 10:36):

Thanks! I hadn't seen that example. It is very useful.

So, if I understand correctly, the language has bespoke syntax to enable this pattern, right?

The : <- syntax is something special Roc has that is then desugared to Elm's applicative functor pattern.

view this post on Zulip Luke Boswell (Dec 15 2023 at 10:36):

Well record builder is just syntax sugar. You can make a pipeline just using normal functions

view this post on Zulip Luke Boswell (Dec 15 2023 at 10:37):

Roc parser is an example of this. https://github.com/lukewilliamboswell/roc-parser/blob/f0402a841126f4b2b6e85c8df0adb0941e9c04c0/examples/numbers.roc#L26

view this post on Zulip Luke Boswell (Dec 15 2023 at 10:38):

Not sure if thats the best example to link to.

view this post on Zulip Asier Elorz (he/him) (Dec 15 2023 at 10:39):

Well record builder is just syntax sugar. You can make a pipeline just using normal functions

I see. But in that case, if the function has more than one argument, you would need to write it "curried" by hand, right? Like when the example does
\aID -> \bID -> \cID -> { aliceID: aID, bobID: bID, trudyID: cID }

view this post on Zulip Luke Boswell (Dec 15 2023 at 10:43):

I think so. I'm definitely not an expert or anything, Roc is the first time I've encountered an applicative pipeline. :sweat_smile:

view this post on Zulip Brendan Hansknecht (Dec 15 2023 at 15:52):

@Asier Elorz (he/him) can you give some specific examples with code snippets? Might make it easier to give alternative suggestions.

Side note, anything with encoding and decoding, like json, roc has an alternate solution in the Encode and Decode abilities.

Also, maybe @Richard Feldman just has a solid answer cause I know he has dealt with both forms before and picked this.

view this post on Zulip Asier Elorz (he/him) (Dec 15 2023 at 17:22):

I haven't really started writing the code yet :sweat_smile:

I am mostly researching for a small Roc project I will very definitely one day do when I have free time and the energy to do it.

Also I am curious about how I would do thing in language, in general. Just to nerd about it, without specific applications.

view this post on Zulip Brendan Hansknecht (Dec 15 2023 at 17:30):

That's totally fair.


Last updated: Jul 06 2025 at 12:14 UTC