r/functionalprogramming • u/No-Condition8771 • Jun 15 '24
Intro to FP Dear FP, today
Dear FP,
Today I was today years old when I wrote my first ever currying function. I feel...euphoric? Emotional? I want to cry with joy? I wish I could explain this to a random stranger or my gf...
I've been doing web programming for 20 years, mostly procedural and OOP, and only really really got into FP a month or two ago. Heard currying a million times. Read it recently a dozen times. Didn't make sense, just seemed overcomplicated. Not today.
I did a non curryied version (of course) of some test state validation I'm doing, and then I want to use array_map, which in PHP only passes one argument to the callable. And then and there that forced the issue. It's like I can hear the bird singing outside right now.
I know this is not Rust, or Haskell. But I'm happy now.
Thank you for this subreddit.
9
u/Flyyster Jun 15 '24
The journey gets even more intense as you dive into reactive programming, which utilizes functional programming while also providing concepts for continuous state management and data flows. See ReactiveX
3
u/TestDrivenMayhem Jun 15 '24
It’s a great feeling when you see the simple power of FP techniques. I have few people to share my wins with.. LOL. Another very useful advantage of currying is partial application. Instead of making the calls one f(args)(args) Make the 2 calls separately in different places. So you can for example pass in dependencies into the first function which is then captured inside the function much like a class. Then call the resulting function deeper in your code with user input or api/db response. This was called the maker pattern and common in some of NodeJS applications at a previous job. I am enjoying this in TypeScript land.
https://effect.website/
3
u/pomme_de_yeet Jun 15 '24 edited Jun 15 '24
Warning, I don't know php so feel free to let me know if I'm just completely wrong lol
I don't understand why currying is needed here...
Is there some difference between:
and
that I'm missing? Seems like the problem is already solved by using a closure. Is the evaluation order different or something?
Edit: something else, if you switch the args around:
So then using it is just:
7
u/pthierry Jun 15 '24
Yeah, there are a few techniques or patterns like that in FP, that are both very useful and very beautiful.
Some of them for me were applicative functors, free applicatives (took me some time to wrap my head around those the first time!), monads, free monads, algebraic effects, STM, or the traverse function.
2
u/Arshiaa001 Jun 15 '24
I was today years old when I learned you can do currying in Rust... Or can you? In a sane manner? Without fighting the borrow checker? I should think not.