We ' ll learn how to take advantage of Ramda's automatic function currying and data-last argument order to combine a series of pure functions into a left-to-right composition, or pipeline, with RAMDA ' s pipe function.
A Simple example would take ' teams ' array and output of the best score team ' s name. We use the ' r.sort ', ' r.head ' and ' r.prop ' to get job done:
Const TEAMS = [ ' Lions ', Score:5}, ' Tigers ', score:4}, ' Bears ', Score:6}, ' Monkeys ', score:2function(teams) { = R.sort (A, b) = B.score > A.score, teams); = R.head (sorted); = R.prop (' name ', bestteam); return = gettopname (teams) console.log (Result)
One thing in Ramda which are really cool that, for example, ' R.sort ' takes the arguements of the If you don ' t passin the second A Rguement which is ' teams ', it'll then return a function, so that it enable you currying function and take second Argueme NT as Param.
Const TEAMS =[{name:' Lions ', score:5}, {name:' Tigers ', score:4}, {name:' Bears ', score:6}, {name:' Monkeys ', score:2},];const Getbestteam= R.sort (A, b) = B.score >a.score); Const Getteamname= R.prop (' name '); Const Gettopname=function(teams) {const sorted=Getbestteam (teams); Const Bestteam=R.head (sorted); Const name=Getteamname (Bestteam); returnname;} Const result=Gettopname (teams) console.log (Result)
We'll still get the same result.
Use the ' r.pipe ' to chain function together
In functional programming or Lodash (_.chain), we get used to write chain methods, in Ramda, we can use R.pipe ():
Const teams = [{name: ' Lions ', Score:5 ' Tigers ', Score:4 ' Bears ', Score:6 ' Monkeys ', Score:2},];const getbestteam = R.sort ((b) and B. Score > A.score); Const Getteamname = R.prop (' name ' = R.pipe (Getbestteam, R.head , getteamname); /* const gettopname = function (teams) {Const SORTED = Getbestteam (teams); Const Bestteam = r.head (sorted); Const NAME = Getteamname (Bestteam); return name;} */ const result = Gettopname (teams) Console.log ( Result)
[Ramada] Build a functional Pipeline with Ramda.js