zoukankan      html  css  js  c++  java
  • [Javascript Crocks] Make your own functions safer by lifting them into a Maybe context

    You probably have functions that aren’t equipped to accept a Maybe as an argument. And in most cases, altering functions to accept a specific container type isn’t desirable. You could use map, passing in your function, or you could “lift” your function into a Maybe context. In this lesson, we’ll look at how we can get a function that accepts raw values to work with our Maybe container without the need to modify the original function or the input values.

    Imaging you want to double a number:

    const crocks = require('crocks');
    const { safeLift, safe, isNumber } = crocks;
    
    const dbl = n => n * 2;
    
    const safeDbl = n => safe(isNumber, n).map(dbl);
    
    const res = safeDbl(2).option(0);
    
    console.log(res)

    Calling 'safe(pred, n).map(fn)' is also a common partten.

    We can use 'safeLift' to simplfiy the code:

    const crocks = require('crocks');
    const { safeLift, safe, isNumber } = crocks;
    
    const dbl = n => n * 2;
    /*
    const safeDbl = n => safe(isNumber, n).map(dbl);
    */
    
    const safeDbl = safeLift(isNumber, dbl);
    const res = safeDbl(2).option(0);
    
    console.log(res)
  • 相关阅读:
    netstat
    ansibe tower的开源替代品semaphore
    ansible playbook 示例
    centos6 安装python2.7
    python celery + redis
    flask + uwsgi 生产环境
    ubuntu访问supermicro ikvm
    [leetcode]Symmetric Tree
    [leetcode]Pascal's Triangle
    [leetcode]Letter Combinations of a Phone Number
  • 原文地址:https://www.cnblogs.com/Answer1215/p/9038215.html
Copyright © 2011-2022 走看看