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