Hello and Happy New Year to everyone, I have been really busy recently and it has been long time I didn't write, however do not worry I didn’t forget I have to finish my functional programming series of articles.
The code below shows that objects and surprisingly even functions (which are objects) can be used as arrays:
You can see in the example above that it is possible to use Array methods through the Array prototype on non Array type objects because they behave the same.
About the curry function:
In mathematics and computer science, currying is the technique of transforming a function that takes multiple arguments (or a tuple of arguments) in such a way that it can be called as a chain of functions, each with a single argument (partial application). It was originated by Moses Schönfinkel and later re-discovered by Haskell Curry. Because of this, some say it would be more correct to name it schönfinkeling.
So from a practical point of view curry helps us to create partially applied functions on the fly without typing a lot of code.
In this section I assume you know what the notion of catamorphism is and what are the fold, map and filter functions, if you don’t please read my previous article 1st Class Functions – Power is in simplicity.
Once we have fold, we can define map and filter in terms of it:
We can now do funny things with arrays of data:
We can now use the select and where methods on any new Array type instances.
Look how easily we can query data!
As shown you can use the built in sort operation, and if you need a join don’t forget the concat method. Since from the past few years applications started to become more and more client based, those short functional helpers defined by less than 10 lines of code appeared to me to be quite useful when processing data incoming from web services. As a comics guys would say “With great power comes great responsibility”. I hope you will have great use of it.