From what I have understood:
Currying - functions returning functions
string.capitalize(1)('character')('at the end')
Method chaining - methods returning objects
string.lowercase.capitalize.uppercase
Is this understanding correct?
If so, are there cases one of them is better than the other?
Cause it seems to me that method chaining is better and more readable. You also have autopletion showing what methods you can use if you hit "dot" and it will show all the arguments you can pass.
From what I have understood:
Currying - functions returning functions
string.capitalize(1)('character')('at the end')
Method chaining - methods returning objects
string.lowercase.capitalize.uppercase
Is this understanding correct?
If so, are there cases one of them is better than the other?
Cause it seems to me that method chaining is better and more readable. You also have autopletion showing what methods you can use if you hit "dot" and it will show all the arguments you can pass.
Share Improve this question edited Apr 25, 2022 at 15:33 Wicket 38.8k9 gold badges80 silver badges195 bronze badges asked Nov 8, 2010 at 3:08 ajsieajsie 79.9k110 gold badges284 silver badges387 bronze badges 1- 1 JavaScript does not support currying natively (it can be emulated with closures, but.. also, the example posted does not really look like a sensible curry :-) and thus just chaining (which is not orthogonal to currying) is normally used. A big "reason" to curry is to be able to use partially applied functions -- once again, doable just with closures. – user166390 Commented Nov 8, 2010 at 3:13
1 Answer
Reset to default 9A better equivalent of currying would be the Builder design pattern.
Ergo, you would do something like:
myObject.setIndexRangeToEffect(1,1).setTextTransformation(UPPERCASE).execute();
At any point before calling execute
, you essentially have a "curried" action object.