4 ms·
Currying is a functional approach. But there is an object-oriented approach that works even better: You have a Function add object that has methods bindWith1(x)
by guard-of-terra 14y ago
Currying is a functional approach. But there is an object-oriented approach that works even better:
You have a Function add object that has methods bindWith1(x) and bindWith2(y) both returning a function of one argument.
With add they do the same but with say divide they do different things (both useful).
- michaelfeathers 14y agoYes, you can do that, but to me it's not as nice as having as syntax that makes it easy and natural. This is where the ML-derived languages like OCaml, F# and Haskell beat other functional languages, in my opinion. You can argue that it's nice to be able to choose the argument to bind, but I think that's actually a bad feature. It gets rid of the design pressure to pick an order of arguments that maximizes easy composition.
- guard-of-terra 14y agoMy experience with clojure/haskell tells me that order of arguments of common functions feels horribly wrong sometimes.
- michaelfeathers 14y agoI guess there are no guarantees. Which ones are you thinking of in Haskell? There it seems to matter more.
- guard-of-terra 14y agoI always seem to struggle with array/list indexing functions.
- crntaylor 14y agoLike these? > let lst = [1,2,3] > lst!!0 1 > lst!!1 2 > let arr = listArray (0,2) [1,2,3] > arr!0 1 > arr!2 2 I don't see how it would be particularly helpful to have the arguments to the indexing functions the other way around, but in case you really like the index coming before the list/array, you can always do > (!0) arr 1 > (!!1) lst 2
- pohl 14y agoI've noticed that, too. One of the answers/comments for this question suggests that (for Haskell, at least) the order of arguments is chosen to be in decreasing probability of partial application. Maybe that accounts for the odd feel.
- jes5199 14y agoI was going to say that learning clojure, I get confused because some of the functions are backwards from haskell. So, I think maybe there's just not consensus about what's the best order.
- tikhonj 14y agoI had exactly the same experience when doing a project in Racket after using Haskell for a while. For what it's worth, I like the Haskell ordering more in most cases.
- freyrs3 14y agoYou can do the same in Haskell with the flip function. In fact the bindWith2 is the same as the flip function for binary functions, but without the need for "functions objects" with bound methods. λ: (/) 2 4 0.5 λ: flip (/) 2 4 2.0