News for hackers. Stuff that matters.
Re:Currying by solardev on Sunday April 07, 2024 @02:04AM (#39958502)
In my entire life as a programmer, this is the one thing that I've found most confusing. Our old codebase used a lot of curried Ramda functions and nobody on the team could read any of it. We spent a week ripping every single instance of it out and replaced it with regular lodash and never looked back and never had an issue with it again.

After like ten or fifteen hours of trying to understand what currying is, I still have no idea, and frankly don't care. If I see it in a codebase it'd just be a big red flag to avoid that job altogether.

Re:Currying by kccqzy on Sunday April 07, 2024 @03:14AM (#39958848)
> After like ten or fifteen hours of trying to understand what currying is

I have doubts on whether this is a sincere attempt at understanding the concept. The linked wiki page is like a five minute read and that's enough to understand the concept at a user's level.

What I suspect is that something else is tripping you up and causing code readability issues; but since you didn't know what currying is you incorrectly ascribed currying as the problem.

Re:Currying by threatofrain on Sunday April 07, 2024 @03:26AM (#39958922)
Yeah 10-15 hours is kinda crazy for an introductory topic.
Re:Currying by worddepress on Sunday April 07, 2024 @02:18AM (#39958574)
In Haskell it is easy. If you "forget" the last argument to a function, you get returned a function where you can provide that later on. A bit like saying "you can fill this in later". That is a "curried" function.

Example

    add 1 2 // add is curried, you can use it like this, the "normal" way, returns 3

    p = add 1 // since add is curried I can also provide just the first argument
    p 2 // and then apply the last argument to the intermediate results, returns 3
What is the point of using curried functions in JS? I am not really sure. It is not very ergonomic and I wouldn't like to use them in general. Maybe for some specific things it could be useful.

In Haskell curried form is the default and the syntax and semantics really suits it. In JS non-curried is the default and it just looks odd, and you need libraries to support it. That library you mentioned doesn't look nice to use.

Re:Currying by zarzavat on Sunday April 07, 2024 @03:14AM (#39958847)
It’s worth noting that even in Haskell, overuse of the so called point-free style is disliked for much the same reasons:

https://wiki.haskell.org/Pointfree

There is a sliding scale and even at the Haskellers have a limit of how much point-free they can take.

In JavaScript use of the style is problematic in another way: unlike Haskell in JS the length of the argument list is variable, which means that if someone adds another argument to either the caller or callee it can break the code in subtle and unexpected ways.

For this reason it’s good practice to always wrap functions being passed as arguments to another function in a lambda expression.

i.e instead of writing: g(f), you should usually write: g(x => f(x)) unless you have good reason to believe that g(f) is safe. This makes it difficult to use point-free style at all in JS.

For example arr.map(f) is generally unsafe in JS because if `f` adds an extra default argument of type number then your code will break and even TypeScript won’t let you know.

Re:Currying by epolanski on Monday April 08, 2024 @09:11AM (#39969370)
All your JS/TS example says is that the `f` callback is not curried, so your point is entirely lost on me.
Re:Currying by kerkeslager on Sunday April 07, 2024 @03:57AM (#39959075)
I've never liked this "feature".

What if you forget, not in quotes, to give the second argument? Why on earth would you want to get a type error in a completely different part of the code because you got a function instead of an integer? Wouldn't it be desirable to have the compiler just tell you that you forgot the second argument where you forgot the second argument?

Is it really valuable to be able to do:

    p = add 1
...instead of:

    inc = (\x -> add 1 x)
...or, heaven forbid:

    inc x = add 1 x
...?

I mean, I get the idea, they're following the lambda calculus, but this is one of the things that should have been dropped when they started to expand the lambda calculus to a general purpose programming language.

Re:Currying by bPspGiJT8Y on Sunday April 07, 2024 @06:23AM (#39959786)
> What if you forget, not in quotes, to give the second argument?

I will get a type error and it will take me 2-3 seconds to figure out what it is about.

> Why on earth would you want to get a type error in a completely different part of the code because you got a function instead of an integer?

Why would it be in a completely different part of the code? At most it would be 2 lines away, but usually on the same line.

Re:Currying by kerkeslager on Sunday April 07, 2024 @06:37AM (#39959855)
> I will get a type error and it will take me 2-3 seconds to figure out what it is about.

You should really screen capture yourself coding sometime. On a large codebase you're lucky if the compiler even runs in 10 seconds.

> Why would it be in a completely different part of the code? At most it would be 2 lines away, but usually on the same line.

Because possibly you return the partial assuming it's a number, and try to use it somewhere else, possibly even in another file.

Re:Currying by bPspGiJT8Y on Sunday April 07, 2024 @07:03AM (#39959984)
> Because possibly you return the partial assuming it's a number, and try to use it somewhere else, possibly even in another file.

Sorry, I must've been more explicit instead of implying certain usage patterns. What I meant here is that I have a hard time imagining this happening because I would start working on a function by writing its type signature. Unless my types check out, I won't be able to mark this function as "done" and jump to another part of code. So the situation "you return the partial assuming it's a number" simply can not happen, that's exactly what type checking is for. By the time I use it in another place, it has to already have been type checked.

Re:Currying by kerkeslager on Tuesday April 09, 2024 @12:14AM (#39976144)
Sure, but that usage pattern is not without cost. You're basically eschewing relying on type inference across function boundaries.

I think writing out type signatures is The Right Way, but it does take time, and The Right Way doesn't always happen on complicated projects with close deadlines.

Re:Currying by worddepress on Sunday April 07, 2024 @08:00PM (#39965010)
You don't have to use type inference everywhere.

But I don't think this is a problem in practice for whatever reason. Haskell does have a lot of "big scary type error, wtf" type problems, but not from currying. Or at least it did have when I last used it 5 years ago, it may have improved in the compiler error message front.

Also it is better than JS anyway (low bar):

    >  function add(a,b) {return a + b}; add(1);

    <- NaN
Re:Currying by taneq on Sunday April 07, 2024 @03:57AM (#39959071)
Honestly, a lot of these 'advanced' features aren't a good choice in most codebases IMO. The vast majority of 'enterprise' coding is CRUD and glue, where execution speed doesn't matter and being as simple and explicit as possible is the highest virtue. And the significant number of professional coders who worked hard to get their heads around things like pointers and printf format specifiers are still plenty productive in those kinds of codebases.

Things like currying are fun but like anything that encourages gratuitously deep call trees, they wreck your locality of reference (as a developer) and force you to 'decompile' the code in your head in order to understand it. I'm sure that a top level developer would be able to write curried JS in such a way that it was clear and readable to another top level developer, but that's not the point. The code's not for you, it's for newbie who gets stuck with it when you move on.

Re:Currying by 082349872349872 on Sunday April 07, 2024 @02:25AM (#39958614)
Function arguments work like the power rule for exponents in elementary school math:

  f a b = g(a,b) = f' b a
  (x^2)^3 = x^(2*3) = (x^3)^2
Re:Currying by solardev on Sunday April 07, 2024 @02:34AM (#39958657)
You must've gone to a pretty kick ass elementary school. In mine, we'd play Connect Four and make paintings out of fake hieroglyphs...
Re:Currying by 082349872349872 on Sunday April 07, 2024 @02:42AM (#39958697)
I'm fortunate my parents didn't stick with free-to-play, but were willing to spend 5-6 digits more to buy a house in the "good" school district: https://news.ycombinator.com/item?id=39946026
Re:Currying by dxdm on Sunday April 07, 2024 @04:29AM (#39959254)
Willing _and_ able.
Re:Currying by 082349872349872 on Sunday April 07, 2024 @04:51AM (#39959369)
I grew up during the Cold War, so I don't know what the Young Pioneers were learning*, but we were good little capitalists, and we learned the Golden Rule:

"Whoever has the gold makes the rules"

(*maybe the ones who spent their summers at Artek were learning "whoever makes the rules has the gold"?)

Re:Currying by inopinatus on Sunday April 07, 2024 @03:21AM (#39958894)
When I were a lad, elementary school meant four hours of beatings in a muddy field, and we were glad of it
Re:Currying by 082349872349872 on Sunday April 07, 2024 @03:31AM (#39958946)
On that note, I found Orwell's posthumously-published Such, Such Were the Joys (about his boarding school days) suggests the inspirations behind large swaths of 1984.
Re:Currying by inopinatus on Sunday April 07, 2024 @03:49AM (#39959037)
Coming Up for Air is practically my childhood in a nutshell
Re:Currying by bPspGiJT8Y on Sunday April 07, 2024 @02:10AM (#39958541)
> After like ten or fifteen hours of trying to understand what currying is, I still have no idea

Maybe you could recall which learning materials you used?

Re:Currying by solardev on Sunday April 07, 2024 @02:14AM (#39958557)
The Ramda docs, primarily, and Google searches. It just seemed like a lot of unnecessary complexity. We modified the parts we understood (or believed we did), and just rewrote the rest from scratch. The tests still passed, nobody else complained, the managers and customers never noticed, the devs were much happier, QA never said a word, and a whole class of bugs disappeared once we were able to reliably predict the output of simple helper functions. Shrug. Seemed like a win win. Probably the best thing we ever did to that codebase.
Re:Currying by auggierose on Sunday April 07, 2024 @02:06AM (#39958521)
Ramda, lodash, no idea what currying is, ...

Software is in good hands.

Re:Currying by solardev on Sunday April 07, 2024 @02:10AM (#39958540)
Heh, so much of the world runs on basic business software. If it ran fine after the rewrite, with much less cognitive load, why not? These were just bog standard web pages that someone overengineered the hell out of, to a point that nobody else could understand or maintain it. Sure, that might win them l33t points, but wasn't very useful otherwise.
Re:Currying by yakshaving_jgt on Sunday April 07, 2024 @03:44AM (#39959018)
As a Haskell programmer, I have to agree with you.

When you're writing Haskell, you should write idiomatic Haskell. Currying is part of that. It's a trivial concept and we use it liberally at work. If you're writing JavaScript though, I think it's a different story. I don't think JavaScript lends itself to FP in the way that Haskell or Elm does, and trying to make it do that only makes a project harder to work on.

Re:Currying by worddepress on Sunday April 07, 2024 @02:12AM (#39958549)
Almost as if an astronaut can come, do their thing in a whirlwind and leave for another company 2 years later, while the remaining plebs have to figure it all out.
Re:Currying by solardev on Sunday April 07, 2024 @02:30AM (#39958631)
Yeah, a lot of the codebase was like that. Some lone wolf mad genius that occasionally left pieces of pure brilliance behind, but more often than not, just wrote code that was incredibly idiosyncratic and unnecessarily reinvented. Like he'd spend several files making a color gradient visualization system with his own scales, with an enthusiastic but limited understanding of color theory, that ended up producing mostly reasonable but occasionally insane color scales. We spent several days of back and forth discussions with him before understanding what he was actually trying to do (which isn't what his code was actually doing) and then replaced it with a one liner out of a standard visualization/color lib.

The entire codebase was like that. It stood out to me as the work of someone who was very smart but worked alone and never had to work with a team, and never considered what someone trying to retrace his mental process would have to go through. He left zero documentation or comments, built the whole thing in a rush, sold it to another company, and we were the ones hired later to clean up his mess.

Some of it was very good. He was really good at efficiently encoding low level network traffic over packet encodings he invented or heavily modified. Seemed like he would've been a brilliant signals engineer or cryptologist. But having to work with his ordinary business logic code was pretty nightmarish, if interesting from a reverse engineering and psychology point of view.

It's just not how I would ever want to write code meant for mere mortals. But then again, he's a multi millionaire now and I'm a rando nobody living paycheck to paycheck, so I'm in no place to judge lol.

Re:Currying by MarkSweep on Sunday April 07, 2024 @03:37AM (#39958972)
> But then again, he's a multi millionaire now and I'm a rando nobody living paycheck to paycheck, so I'm in no place to judge lol.

If he had not made such a big mess, you might not be getting this paycheck to clean it up. I think they call this “creating scope for other people” at big companies, real staff engineer stuff. =)

Re:Currying by solardev on Sunday April 07, 2024 @10:44AM (#39961115)
Heh, exactly. Some of us are just software janitors for the bigwigs.
Re:Currying by akdor1154 on Sunday April 07, 2024 @02:25AM (#39958615)
Was the codebase in strict Typescript? I find currying to be sometimes useful in the right language / paradigm, as long as you can rely on the compiler to yell at you until you get it right.

Dynamically typed functional retrofit on with no type checking though? Hell no, that's a write-once read-never mess of unmaintainable shite.

(I also have been on a team with The Guy Who Wanted To Ramda, and I'm someone who writes build scripts in ocaml)

Re:Currying by solardev on Sunday April 07, 2024 @02:46AM (#39958721)
It was a mishmash. The parts written by later employees were strongly typed and easy to follow. The parts he wrote weren't typed at all and frequently relied on implicit conversions and bitwise operators to do magic operations that would take us mere mortals several days to even begin to understand, all just to save what would've been like 3 lines of code and a single comment.

Inheriting that codebase felt like watching one of those serial killer dramas... obviously a really smart guy, but with a psychology unlike most people I've ever met, and really hard to follow as a normal person. I still see him with a mixture of respect, awe, curiosity, and wonder.

The thing is, none of the stuff he gave is was actually complicated. It's just a basic business dashboard. But he reinvented almost every single part of it in totally nonstandard ways that the whole project was less about coding and more about anthropology and forensic psychology.

Re:Currying by OscarTheGrinch on Sunday April 07, 2024 @02:52AM (#39958750)
CSI: Code Shite Investigation
Re:Currying by threatofrain on Sunday April 07, 2024 @03:18AM (#39958870)
Currying generally means language level support for making the below example easier.

    const strToInt = (str) => parseInt(str, 10)
Re:Currying by fxj on Sunday April 07, 2024 @07:53AM (#39960144)
In my experience, functional programming really shines in rapid prototyping. However, in production code I would try to avoid it, because it can confuse developers that are not used to it and also sometimes I struggle myself when decoding the short "clever" one-liner that I wrote 5 years ago. But if you want to write very powerful code withing a single line it is really great and makes you very productive for finding solutions fast. It is a bit like the Perl throw-away code we were writing in the 1990s. ;-)

just my 2 ct