[Ramada] Build a functional Pipeline with Ramda.js

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.