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 * ; const safeDbl = n => safe(isNumber, n).map(dbl); const res = safeDbl().option(); 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 * ;
/*
const safeDbl = n => safe(isNumber, n).map(dbl);
*/ const safeDbl = safeLift(isNumber, dbl);
const res = safeDbl().option(); console.log(res)

[Javascript Crocks] Make your own functions safer by lifting them into a Maybe context的更多相关文章

  1. [Javascript Crocks] Compose Functions for Reusability with the Maybe Type

    We can dot-chain our way to great success with an instance of Maybe, but there are probably cases wh ...

  2. [Javascript Crocks] Apply a function in a Maybe context to Maybe inputs (curry & ap & liftA2)

    Functions are first class in JavaScript. This means we can treat a function like any other data. Thi ...

  3. [Javascript Crocks] Recover from a Nothing with the `coalesce` Method

    The alt method allows us to recover from a nothing with a default Maybe, but sometimes our recovery ...

  4. [Javascript Crocks] Safely Access Object Properties with `prop`

    In this lesson, we’ll use a Maybe to safely operate on properties of an object that could be undefin ...

  5. [Javascript Crocks] Create a Maybe with a `safe` Utility Function

    In this lesson, we’ll create a safe function that gives us a flexible way to create Maybes based on ...

  6. [Javascript Crocks] Understand the Maybe Data Type

    In this lesson, we’ll get started with the Maybe type. We’ll look at the underlying Just and Nothing ...

  7. JavaScript基础-即时函数(Immediate Functions)(017)

    1.即时函数的声明方法 即时函数(Immediate Functions)是一种特殊的JavaScript语法,可以使函数在定义后立即执行:(function () {    alert('watch ...

  8. JavaScript Patterns 4.5 Immediate Functions

    The immediate function pattern is a syntax that enables you to execute a function as soon as it is d ...

  9. [Javascript Crocks] Recover from a Nothing with the `alt` method

    Once we’re using Maybes throughout our code, it stands to reason that at some point we’ll get a Mayb ...

随机推荐

  1. hihocoder 1866 XOR

    题面在这里 拆位分析一下就OK啦 /* y + (y xor x) */ #include<bits/stdc++.h> #define ll long long using namesp ...

  2. Codeforces Round #354 (Div. 2) C. Vasya and String 二分

    C. Vasya and String 题目连接: http://www.codeforces.com/contest/676/problem/C Description High school st ...

  3. python—第三库的安装方法

    Windows系统下安装第三方Python库的三种方法: 1.使用easy_install命令安装 一般在安装完Python后再C:\Python27\Scripts 目录下有 easy_instal ...

  4. 小程序navigator点击有时候会闪一下

    <navigator hover-class="none">

  5. [Phonegap+Sencha Touch] 移动开发18 Sencha Touch项目通过phonegap打包后的程序名字的问题

    之前说过 sencha phonegap init com.pushsoft.myapp MyApp 之后打包的程序安装包apk的名字是"MyApp.apk",显示在手机桌面上的程 ...

  6. Asp.net处理程序(第六篇)

    四.Web服务处理程序 对于Web服务来说,标准的方式是使用SOAP协议,在SOAP中,请求和回应的数据通过XML格式进行描述.在Asp.net 4.0下,对于Web服务来说,还可以选择支持Ajax访 ...

  7. MFC小程序------01 代码管理器

    1.代码入库: 2.代码查找: 3.查看全部代码: 4.程序设置: 自己学习MFC写的一个小程序,当中还有很多功能还待完好,比方数据库的导入功能还没有写,但导出功能是能够用的,查找算法也不是非常好,还 ...

  8. linux系统负载相关的概念和度量

    系统负载有 CPU利用率 和 LoadAverage这2个概念. cpu利用率:cpu utilization,是进程(task)被内核调度进程实际分配了CPU资源后,在时间片内使用CPU进行工作运算 ...

  9. java string常见操作(二)

  10. 【springboot+easypoi】一行代码搞定excel导入导出

    原文:https://www.jianshu.com/p/5d67fb720ece 开发中经常会遇到excel的处理,导入导出解析等等,java中比较流行的用poi,但是每次都要写大段工具类来搞定这事 ...