[Javascript Crocks] Recover from a nothing with the ' coalesce ' Method

Source: Internet
Author: User

altThe 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

Related Article

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.