141 points · by corentin88 · 5y ago · 158 comments
I'm not a big fan of OOP either, the older I get the more I end up writing a cross between procedural and functional code with the least possible abstractions in the way - while also trying to find the smallest natural coupling between blocks of code.
Patterns are very subjective, and they usually have a place where it's objectively good to use them - but I'm not sure about currying, it feels more like a hack for when you can't do what should be done to the underlying code.
Currying and partial application are really nice when you have a pipeline operator, which is why the partial application and pipeline operator proposals are so intertwined.
users
|> List.map (fn user -> user.Email)
|> sendEmail
|> Result.mapError (fn _ -> "Error sending emails")Currying != partial application.
And while currying is useful in curried languages (to convert back from uncurried to curried), and partial application is useful period, the question is whether currying is useful, in general, in an uncurried language.
I don't think it is:
* You usually want to perform partial application, currying is an unnecessary intermediate step.
* Currying conflicts with rich "imperative" parameters e.g. overloading, default parameters, keyword parameters.
* Uncurried languages are usually imperative and effectful, that you have filled all the parameter spots does not mean you want to invoke the function.
Having a curried language is neat, and in curried languages converting back from uncurried to curried functions is tremendously useful. But it's not so for uncurried languages (which is most of them).
> which is why the partial application and pipeline operator proposals are so intertwined.
Again, partial application != currying. And pipeline operators can perform partial application (implicitly or explicitely) on their own. For a flagrant example, just see Clojure:
(->> users
(map :email)
(sendEmail)
(mapError (constantly "Error sending emails"))In particular I don't know what's meant by "curried" and "uncurried" languages
This doesn't contradict anything you said, but I think Idris is a language that curries everything and has default parameters.
https://docs.idris-lang.org/en/latest/tutorial/miscellany.ht...
errors = list()
for user in users:
if not sendEmail(user.email)
errors.append("Error sending emails")Because I say "what" do to do without going into details of "how" I feel like I have spent less mental capacity reading this portion of code.
However, it does require your transformations to be simple and composable, shove a giant multi-line lambda in there it yes, the loop will be better.
For what it's worth, your example doesn't map 1-to-1 to the intention of what I wrote. Doing it in a more imperative style, I'd have to write
emails = list()
for user in users:
emails.push(user.email)
res = sendEmail emails
if res.IsError:
Error("Error sending emails")
else:
res
(excuse my pseudo Python) users.stream()
.map(User::getEmail)
.map(Email::send)
.map(res -> "Error sending email: " + res)
.collect(toList())https://docs.oracle.com/javase/8/docs/api/java/util/stream/p...
with elixir (the previous example) you'd likely use simple hashmaps or lists like {email: "mail"} or [:error, "reason for failure"].
That doesn't sound like its amazing, but actually is in practice, because the whole language is written with that in mind (pattern matching to effectively do function/method overloading depending on the value of each argument for example)
its very concise while still being very explicit. The same would be possible with java, but you'll need to create a lot of classes/interfaces/enums/boilerplate to facilitate it.
i'd still prefer java any time at a dayjob though, because boring and dumb is generally better if you need to write code that's gonna be in use for decades... and likely going to be changed by a lot of people with varying levels of experience.
An error could be a malformed e-mail address, or a timeout from the e-mail server.
What you do not want is to send 10000 e-mail, then have an error like "Error sending emails", then re-run a few time, only to have some people receive 5 e-mail, and some people receive 0 e-mail.
Dependency injection and currying both do the same thing, and they are both useless most of the time just like Inheritence. You want to avoid nesting as much as possible. We should be rejecting dependencies. We should be returning values and passing them through functional pipelines.
Free yourself from the dogma, just do what makes sense.
I dare mention Rich Hickey and his talk “Simple Made Easy”, and postulate that we should be using whatever abstraction is _the simplest_ (not the easiest one) to reason about, to which the grand parent rightfully refers, as far as I understand.
Objects are easy to many, but far from being objectively simple.
What about partial application of named parameters?
Builder pattern. In a language that accommodates it well, such as Elm, it would look like this:
IconLib.downloadIcon
|> IconLib.withSize 12
|> IconLib.withColor "red"
|> IconLib.withCssClass "dl-icon"
Allowing currying of any parameter like: standardDownloadIcon = IconLib.downloadIcon
|> IconLib.withSize 12
largeDownloadIcon = IconLib.downloadIcon
|> IconLib.withSize 16 half = (`div` 2)
Also, I think the idea with curried languages is that you're supposed to write your functions with the parameter that people are most likely to want to curry first.I believe this is a notational issue more than anything. Fixpoint style in languages like Haskell is very natural and intuitive, if you have to write a `curry` function like the article shows, you have already lost.
It's much more convenient to `ad-hoc curry` with things like:
const f = x => myFunc(someArg, x, someOtherArg);
return myArr.map(f).whateverElse();In uncurried langages, generic curring has few use cases. Partial application does, and some libraries may want to provide both curried and uncurried versions of some things, but generally “curry” itself is unhelpful.
Consider advanced component creation in virtual dom frameworks. Components may want to yield some render responsibilities to the end-user while not exposing their inner api. They can use currying here to hide details from the end-user while providing their own public api.
I've also seen it be useful when you have sub components editing different parts of a model. You write one generic prop-assigner setModelProp(prop, val) alongside the model in the scope that owns the model. Then you give each subcomponent a curried setter with the prop locked in. Those sub-components don't have the ability to edit the model any other way now, and you haven't written a ton of boilerplate setters.
That's really not true. The Y combinator is not generally useful for instance, because every modern language supports recursion.
> Currying is indispensable in some composability workflows.
It's not.
> Then you give each subcomponent a curried setter with the prop locked in.
That's partial application, not currying.
There are convenience use cases for libraries providing curried versions of some functions e.g. you might want to provide both `setModelProp(prop, val)` and `setModelProp(prop)(val)`. But that's because you expect significant use cases fo partially applied versions of the utility functions. Currying is not necessary for this, it just makes things more convenient for users.
I wish I had been told that before trying to understand and actually use it. I once wrote a JS benchmark based on Rosetta code to compute Fibonacci numbers. Not only is the Y combinator code convoluted and quite hard to understand, it's also between 1e5...2e5 times slower than a plain iterative implementation.
Somehow I find it ironic to post such a result here.
In that case I don't know any indispensible situations for currying.
Same here. I think this is a correct approach, sprinkled with some reactive code here and there (prefer reactive over other abstractions such as 2-way binding et al).
```
The venerable master Qc Na was walking with his student, Anton. Hoping to prompt the master into a discussion, Anton said "Master, I have heard that objects are a very good thing - is this true?" Qc Na looked pityingly at his student and replied, "Foolish pupil - objects are merely a poor man's closures."
Chastised, Anton took his leave from his master and returned to his cell, intent on studying closures. He carefully read the entire "Lambda: The Ultimate..." series of papers and its cousins, and implemented a small Scheme interpreter with a closure-based object system. He learned much, and looked forward to informing his master of his progress.
On his next walk with Qc Na, Anton attempted to impress his master by saying "Master, I have diligently studied the matter, and now understand that objects are truly a poor man's closures." Qc Na responded by hitting Anton with his stick, saying "When will you learn? Closures are a poor man's object." At that moment, Anton became enlightened.
```
Currying (and closures) are great when you don't want to go through the overhead of constructing an entire object just to hold state. They're lightweight pseudo-objects. But the moment the number of variables you are closing over starts getting too big, it is better to formalize it into an object instead. That's how I interpret this story.
Why do closures and objects arise in such a natural fashion? I'm not entirely sure, and I don't think there's one right answer. All I can think is that I can't imagine the depth of Alan Kay's knowledge on the subject, given he pioneered scheme and smalltalk, two languages that define the core of the closure/object paradigm.
My reasoning is we need a partitioned stateful function which is impossible to achieve and the alternative are worse:
- partition global state with your own object id
- passing all state as arguments
- Managing state through a monad or effect system
- Managing state through coroutines / continuations
- Managing state through dynamic scoping (poor man's effect system)
- Associating implicitly mutating functions with types without having full objects (Rust)
Some of these are pretty powerful, especially effects and continuations. I think the reason we don't see more of them is because we haven't figured out a wholly ergonomic way to fit them into a language yet.
(define (make-queue)
(define data empty)
(lambda (cmd [arg #f])
(match cmd
['enqueue (set! data (append data (list arg)))]
['dequeue
(define value (first data)
(set! data (rest data))
value])))
Don't see the connection yet? Let me try again. (define (make-queue)
(define data empty)
(define (instance cmd [arg #f])
(match cmd
['enqueue (set! data (append data (list arg)))]
['dequeue
(define value (first data)
(set! data (rest data))
value]))
instance)----------
This brought to mind my introduction to OOP by a talented but notoriously inarticulate programmer:
NIP: Do you know what OOP is?
Me: No.
NIP: Ah. Well, you have objects and methods. Objects: objects aren't anything. And methods are how you talk to objects. Okay?
Me: Sure.
I knew there was no point in asking questions -- this was the man at his most expository. I took him to mean, "Forget what objects are, just work with them."
-------------
Which I take to mean "Objects are black boxes. How they behave is what they are"
This implementation I wrote is much more elegant and performant than the O(n) stack frame crud in that article:
const curry = fn => {
const curried = (...a) => a.length >= fn.length ? fn(...a) : curried.bind(undefined, ...a)
return curried
}
In fact, I can't think of any implementation in JS that would be more performant, using any paradigm, functional, object oriented, or imperative.Try to best it if you can. I'm curious to see what HN can come up with ;-)
The only difference is that currying would ignore any additional parameters in a flexible language like JS.
It's easy to make this implementation only consider the first parameter. See below.
const curry = fn => {
const curried = (a,x) => a.length + 1 >= fn.length ? fn(...a, x) : curried.bind(undefined, [...a, x])
return curried.bind(undefined, [])
}
I still don't understand your point about eager execution. The function is supposed to be executed when enough parameters have been passed. I suppose you mean that JS does not check if the call arguments length equals the function signature, so strict currying is safer to avoid errors? If so, that makes sense. I tend to consider that caveat of JS a feature though, which is why partial application is what I'd use to accomplish currying. It subsumes it and provides more flexibility by allowing multiple arguments to be curried in a single call.Lucky for us there is a partial application proposal, albeit still in stage 1, which makes partial application—to make functions curried—a lot easier[2].
” To improve the compositionality of our code we had defined a ton of curried functions using Ramda. But every abstraction has a cost associated with it. On profiling, we found out that the `curry` function was taking up a lot of CPU. Benchmarks show that removing curry from the code makes it up to 100 times faster. Curry function is at the core of Ramda, almost every function is curried.”
https://blog.dream11engineering.com/lessons-learned-from-run...
I'm a pretty big fan of strong, static type systems and FP in general, and I have been writing typescript professionally for a few years now.
I decided a long time ago that trying to write Haskell in TS just isn't worth the effort.
Adopting FP ideas such as pure functions, restricting side-effects and then otherwise just using the type system to model the flow of your program goes a very long way. You don't have to write Haskell in TS to reap most of the benefits.
1: https://github.com/ramda/ramda/blob/master/source/curry.js 2: https://github.com/ramda/ramda/blob/master/source/internal/_... 3: https://github.com/ramda/ramda/blob/master/source/internal/_...
value |> fn(?, "static string")
However earlier this month they advanced a version of the pipeline operator with a definite topic marker which makes partial application useless in the RHS of the pipeline operator. This was of course really controversial, as you can see if you read thought the most resent issues (particularly the closed ones).Mh, it's already 4 years old. What a shame.
To make matters worse it appear that generally OOP style darlings such as static class blocks and true private fields seem to gain more momentum.
Here are some trivial examples. As you increase the N, the closure containing ones get faster, except the last one (Chrome/M1). I think in real world scenarios the benefits of the optimizations are less clear cut and also the reason why V8 has a reputation for being a bit of a memory hog which is why we should give it a hand and write dumb imperative code.
https://perf.link/#eyJpZCI6InN0NmQ4cTRrd20zIiwidGl0bGUiOiJDb...
A lot of people here asking "why". Only solid example of it being the more elegant solution (in my opinion) is on the front end with redux.
Maybe you have a large amount of objects in state. Each object has many properties and is used by many components. If you are using an implementation pattern that predefines selectors, you don't want tons of different functions selecting one property each. The simpler way is a single selector that "picks" the property dynamically by currying the property name to the single, more general selector.
The real-world example doesn’t tell me why I would prefer currying instead of putting the date at the end and making it optional, then checking if missing to assign current date. And for the other argument I could create separate functions logDebug, logWarning and logError, those would call the generic function with fixed strings, right?
`sendEmail(mockEmailService)(to, subject, body) `
I find it helps to differentiate between side-effecting dependencies and parameters, making code purer, easier to test, more scalable etc.
Sure, but what would you do if e.g. the importance string isn't fixed? Well, you might write a function that takes in the importance string and returns a function that calls the generic function with the dynamic string. That's currying.
Trying to use currying in languages where it isn't built in makes no sense to me. It doesn't seem to compare well against doing the obvious partial application with a lambda. And it doesn't play well with variadic functions, default arguments, etc. (I have ideas for automatic currying of record types that I hope to explore in a toy language at some point, but that only does so much.)
From searching for practical applications a while back, I understood currying as a way to create a new function with fixed values rather than fixed arguments. Meaning if you had a function take arguments FIRST and SECOND, with FIRST being computationally expensive, currying would allow you to create a new function with the slower part pre-calculated.
But after brief testing I’m unsure that’s the case. It seems the examples might’ve been referring to the argument itself being a computationally expensive operation, in which case—and going back to your example—you could save the computation’s result to a variable beforehand and use that.
For example, if you want to pass an object to onClick event:
const handleOnClick = _.memoize(
(myObj) =>
(evt) => {
// you can do something with myObj here
});
// on render
return <button onClick={handleOnClick(myObj)} >Try it</button>;
Memoize works on class component but I don't know if it works on function component.Or a function that return new date, but it'll make things complicated.
It's the function caller that's responsible for that parameter with partial application / currying / closure.
It doesn't matter if you develop both, but if you use a library you'll need partial application to define default parameter.
Also you totally do not need to be able to call `f( a, b ,c )` as `f( a )( b )( c )`, rather, you should avoid the latter for reasons of clarity, simplicity, and performance. You'd be just producing anonymous single-use throwaway functions for no good reason at all. You do not want to curry when you can just `g = ( a, c ) => f( a, 'xy', c );`. This is simpler, clearer, more flexible, and more to the point. Where this is not enough, just use a class. Everyone talks about inheritance, but where classes shine is that you can bind a multitude of functionality (implemented in methods) to a single set of configuration data (`this`) (and mutable state where called for).
Currying can lead to code that looks simpler, but I believe it hides real complexity in a non-obvious way. Well-named wrapper functions like ‘logNow’ can be great, but arbitrary curried functions are too tricky to keep track of on teams that include juniors, backend engineers and experienced senior FE too.
What are some ways you've seen it abused in JS projects?
let logNow = log.bind(null, new Date()); // [sic, "now" is fixed]
let debugNow = logNow.bind(null, "DEBUG");
Also, uglifiers and transpilers do some horrifying stuff, this won't break if fn.length is tempered with. Unlike the article implementation. let logNow = (importance, message) => log(new Date(), importance, message);
let debugNow = message => logNow("DEBUG", message);
And with this implementation, current IDEs provide a more informative auto-complete. const one = curriedSum(1)
one(2)
is the same as const one = sum.bind(null, 1)
one(2)
with the exception that the latter is explicit and not as slow as `curriedSum`.Both `sum` and `curriedSum` can be used the same way:
curriedSum(1)(2)
sum.bind(null, 1)(2)
or just: curriedSum(1, 2)
sum(1, 2)
One of the advantages is that non-curried functions make the return value explicit. You can't implement `sum(...addends)` with currying because curried functions have a fixed number of parameters. Once past that, a call will not return a function anymore. sum.bind(null, 1).bind(null, 2).bind(null, 3).bind(null, 4) // Still not called
curriedSum(1)(2)(3)(4) // Maybe it works, maybe undefined is not a functionGotta say currying seems to be useless in real world. No advantage whatsoever over partial application.
If we are being pedants. Wikipedia says:
> currying is the technique of converting a function that takes multiple arguments into a sequence of functions that each takes a *single argument*.
So the article isn't currying either. It's a hybrid between currying and partial application.
TBH I can't think of a practical case where you'd prefer currying over partial application. Theory/philosophy aside.
Sure.
> TBH I can't think of a practical case where you'd prefer currying over partial application. Theory/philosophy aside.
For eager-evaluation uncurried language (like javascript) I would also struggle to think of one.
For curried languages, it can sometimes be useful to uncurry functions in order to make types match, after which you might have to curry them back into the "normal" form to use them.
Whenever I run into a curry, I can usually solve the same issue with a simple for loop that makes it instantly clear what the code does, always.
A question for the Javascript experts: is there a way to set a property in the returned function, saying in what line of code it was created? Perhaps something like the __LINE__ macro from C...
const curry = fn => {
return (...args) => {
const curried = (...a) => {
args = [...args]
return args.push(...a) >= fn.length ? fn(...args) : curried
}
return curried()
}
}As for the inelegant claim, beauty is in the eye of the beholder. If performance is not a concern (as it won't be in most currying use-cases) I think the original version is much easier to read without years of js experience. It can be hard for newer or cross-discipline devs to understand mixed usage of rest params and spread operator since they look the same. In a scenario like this I think maintainability is a key ingredient of elegance.
const curry = fn => {
const curried = (...a) => a.length >= fn.length ? fn(...a) : curried.bind(undefined, ...a)
return curried
}
It uses bind which is an extremely powerful operator (a function that maps functions to functions) const sum3 = curry(( a, b, c ) => a + b + c )
const sum3_10 = sum3( 10 ) // ...
const sum3_10_20 = sum3_10( 20 ) // ...
sum3_10_20( 30 ) // 60
sum3_10_20( 5 ) // 60
One of the points of currying is to be able to fork the partial application of the function at any point. const curry = fn => {
const curried = (...a) => a.length >= fn.length ? fn(...a) : curried.bind(undefined, ...a)
return curried
}