Stream: helpdesk (published)

Topic: Starting to learn functional programming in Julia


view this post on Zulip Davi Sales Barreira (Jul 30 2022 at 12:53):

Hello, friends.

I'm starting to "formally" dive into functional programming. I was hoping to use Julia for this (instead of something like Haskell). Any suggestions of books/articles/blog posts/packages? I've found MLStyle.jl , but I don't have the proper knowledge yet to understand most of the functional programming idiom. I'd love some guidance from the pros :)

P.S: My main motivation is to learn how to better design some packages I'm working on.

view this post on Zulip Michael Fiano (Jul 30 2022 at 13:23):

"Functional programming" can mean different things. It can mean no side-effects/referential transparency, or a paradigm of programming that uses map/filter/reduce, or both.

view this post on Zulip Michael Fiano (Jul 30 2022 at 13:23):

Which in particular are you interested in?

view this post on Zulip Michael Fiano (Jul 30 2022 at 13:29):

For the latter, Julia has versions of map/filter/reduce builtin to the language, so you really don't need a library. MLStyle.jl pattern matching is not really inherent to functional programming, but is common in some FP languages. You might like Folds.jl and the libraries built on top of it, as well as this JuliaCon 2022 talk: https://live.juliacon.org/talk/WKNY78 for getting started.

view this post on Zulip Davi Sales Barreira (Jul 30 2022 at 13:39):

Thanks @Michael Fiano . The term Functional Programming seem to be quite overloaded. That's actually one of the problems I'm facing and trying to understand.

view this post on Zulip Michael Fiano (Jul 30 2022 at 13:40):

Welcome to CS, where even the term overload is overloaded :smile:

view this post on Zulip Davi Sales Barreira (Jul 30 2022 at 14:01):

@Michael Fiano , I really liked the talk... Do you have any references you suggest for map/filter/ reduce part of FP?

view this post on Zulip Davi Sales Barreira (Jul 30 2022 at 14:02):

As I said, my main interest is in improving my code design. As I've started coding more complex packages, I realized that I needed a better understanding of the subject.

view this post on Zulip Michael Fiano (Jul 30 2022 at 14:03):

Hmm, you could check out the API for map, reduce, foldl, foldr, and filter perhaps.

view this post on Zulip Michael Fiano (Jul 30 2022 at 14:05):

reduce, and by extension foldl and foldr can take some getting used to, but the other two are pretty powerful on their own and pretty easy to grasp.

view this post on Zulip Michael Fiano (Jul 30 2022 at 14:06):

The thing about FP is it favors higher order functions. Julia was designed with this in mind, but Julia is multi-paradigm, and doesn't constrain your design if you need to reach for a comprehension or for loop or such.

view this post on Zulip Davi Sales Barreira (Jul 30 2022 at 14:06):

I mean, truth is that I understand what they do, but I don't understand why it would be better than what one conventionally uses.

view this post on Zulip Michael Fiano (Jul 30 2022 at 14:08):

It is more composable. Instead of looping over something, you can for example pass a re-usable function to apply to all elements of a container, with the higher order function map.

view this post on Zulip Michael Fiano (Jul 30 2022 at 14:09):

map(isodd, 1:10) for example will tell you which elements are odd. You are not restricted to using builtin functions like isodd though. You can design re-usable functions that are used to map over or filter collections.

view this post on Zulip Michael Fiano (Jul 30 2022 at 14:09):

For anything that conforms to the iterate interface.

view this post on Zulip Mason Protter (Jul 30 2022 at 18:21):

For that sort of thing I’d heartily recommend Transducers.jl over the regular map, filter, fold constructions

view this post on Zulip Mason Protter (Jul 30 2022 at 18:22):

The way to think about them is that they’re a more structured form of a loop, and because they have more structure, they can be (ideally) understood better by a compiler

view this post on Zulip Davi Sales Barreira (Jul 30 2022 at 20:40):

Thanks, @Mason Protter . I'll take a look.
At the moment, I'm reading "Grokking Simplicity" on function programming. Have you guys read it?

view this post on Zulip Davi Sales Barreira (Jul 30 2022 at 20:41):

I'd love to hear book references, even if they are academic. I don't mind studying a bit of CS.

view this post on Zulip Rik Huijzer (Jul 31 2022 at 13:29):

What opened my eyes to handling state inside a program in a functional way was https://prog21.dadgum.com/23.html. Part 3 in particular talks about how (not) to pass state through the system.


Last updated: Nov 06 2024 at 04:40 UTC