[Javascript Crocks] Compose Functions for reusability with the maybe Type

Source: Internet
Author: User

We can dot-chain our The great success with a instance of maybe, but there is probably cases where you need to apply The same series of transformations against different Maybe s. In this lesson, we'll look at some point-free versions of some common maybe methods and see how we can compose them togeth Er to get a reusable function, can applied to any maybe instance.

We is going to rewrite the following code by using function Composion:

ConstCrocks = require ('crocks')Const{and, compose, IsEmpty, isstring, maybe, not, prop, safe} =crocksConst{Join, split, toLower} = require ('Ramda')ConstArticle ={ID:1, Name:'Learning Crocksjs Functional Programming Library'};ConstCreateurlslug = Compose (Join ('-'), Split (' '), toLower);ConstCreateURL = slug = = ' https://egghead.io/articles/${slug} ';ConstCreateurlfromname =Compose (CreateURL, createurlslug);ConstIsnonemptystring =and (Not (IsEmpty), isstring);ConstGetarticlename = obj = Prop ('name', obj). Chain (Safe (isnonemptystring))//If return Nothing Then use ALT value. ALT (Maybe.of ('Page not found'));ConstGetarticleurl = obj =getarticlename (obj). Map (createurlfromname). Option ('default');ConstURL =Getarticleurl (article); Console.log (URL)

We can import those instance methods directly:

Const chain, map, alt, option } = Crocks
ConstCrocks = require ('crocks')Const{and, compose, IsEmpty, isstring, option, maybe, not, prop, safe, chain, map, Alt} =crocksConst{Join, split, toLower} = require ('Ramda')ConstArticle ={ID:1, _name:'Learning Crocksjs Functional Programming Library'};ConstCreateurlslug = Compose (Join ('-'), Split (' '), toLower);ConstCreateURL = slug = = ' https://egghead.io/articles/${slug} ';ConstCreateurlfromname =Compose (CreateURL, createurlslug);ConstIsnonemptystring =and (Not (IsEmpty), isstring);ConstGetarticleurl =Compose (option ('default'), Map (Createurlfromname), Alt (Maybe.of ('Page not found') , Chain (Safe (isnonemptystring)), prop ('name'));/*Const Getarticlename = obj = Prop (' name ', obj). Chain (Safe (isnonemptystring))//If return Nothing Then use Alt    Value. ALT (Maybe.of (' Page not Found ')), const GETARTICLEURL = obj + getarticlename (obj). Map (Createurlfromname) . Option (' Default ');*/ConstURL =Getarticleurl (article); Console.log (URL)

[Javascript Crocks] Compose Functions for reusability with the maybe Type

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.