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. Spring Boot 基础配置

    之前简单接触了一些Spring Boot ,并且写了一个简单的 Demo .本文就来简单学习一下 Spring Boot 的基础配置. 一.Spring Boot 项目入口 上文中有写到,Spring ...

  2. ThinkPHP3.2设置异常页面404跳转页面

    在ThinkPHP3.2版本中当我们访问不存在的页面时会出现非常不友好错误提示页面,类如下图: 0 «上一篇:div非弹出框半透明遮罩实现全屏幕遮盖css实现»下一篇:利于反向代理绑定任意的域名 po ...

  3. 【取对数】【哈希】Petrozavodsk Winter Training Camp 2018 Day 1: Jagiellonian U Contest, Tuesday, January 30, 2018 Problem J. Bobby Tables

    题意:给你一个大整数X的素因子分解形式,每个因子不超过m.问你能否找到两个数n,k,k<=n<=m,使得C(n,k)=X. 不妨取对数,把乘法转换成加法.枚举n,然后去找最大的k(< ...

  4. 处理QMenu的triggered信号时遇到的一个问题

    最近,在一个Qt程序中使用QMenu类时,遇到了一个小问题,特记录下.首先,我模仿一下问题出现的场景:假设我在做一个高大上的XX管理系统,比如说:学生信息管理系统.在这个系统中,学生的各项信息(比如: ...

  5. Windows Phone background Audio 后台音频

    Windows Phone 后台音频的确不是什么新鲜的话题了,但发现目前在WP平台的音频播放应用多多少少会有一些瑕疵,所以在此给大家在此介绍下这个功能给有需要的朋友们. 首先介绍下我们的应用在后台播放 ...

  6. PHP运行时强制显示出错信息

    调试很重要 error_reporting(E_ALL); ini_set('display_errors', '1'); 将出错信息输出到一个文本文件 ini_set('error_log', di ...

  7. PUSH MESSAGE 云控等交互类测试业务的自动化

    针对的业务: 1. PUSH消息,即由云端服务向客户端推送消息 2. MESSAG消息,即用户间消息.用户群消息和聊天室消息 上干货,框架见下图:

  8. Git_解决冲突

    人生不如意之事十之八九,合并分支往往也不是一帆风顺的. 准备新的feature1分支,继续我们的新分支开发: $ git checkout -b feature1 Switched to a new ...

  9. LeetCode152:Maximum Product Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  10. Java基础加强总结(二)——泛型

    一.体验泛型 JDK1.5之前的集合类中存在的问题——可以往集合中加入任意类型的对象,例如下面代码: package cn.gacl.generic.summary; import java.util ...