alt
The method allows us to recover from a nothing with a default maybe, but sometimes our recovery efforts might need t o Enlist some logic to recover properly. In this lesson, we'll see how we can use the method on the maybe to recover from a nothing by coalesce
calling a function.
When one would like option
to a Maybe
but would like to remain within a Maybe
type, coalesce
can be used. coalesce
expects (2) Fu Nctions for it ' s inputs.
The first function is used if invoked on a and would Nothing
return a Just
instance wrapping the result of the function. The second function is used when coalesce
are invoked on a Just
and are used to map the original value, returning a new Just
Instance wrapping the result of the second function.
The use case of ' coalesce ' was similar to ' Alt ', but instead wrting the function as ' Alt ' does:
const getarticlename = obj = Prop ( name " // . alt (Maybe.of ( '
In ' coalesce ', we just combine in one single function:
const Getarticleurl = obj = Prop ('name', obj) // If ReturnNothing and then use the first function of coalesce 'page not found', createurlfromname) . Option ('default'); const URL = getarticleurl (article);
ConstCrocks = require ('crocks')Const{identity, 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);ConstGetarticleurl = obj = Prop ('name', obj). Chain (Safe (isnonemptystring))//If return Nothing Then use first function of coalesce. COALESCE (() ='Page not found', createurlfromname). Option ('default');ConstURL =Getarticleurl (article); Console.log (URL)
[Javascript Crocks] Recover from a nothing with the ' coalesce ' Method