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. MySQL数据库sql语句

    零.用户管理: 1.新建用户: >CREATE USER name IDENTIFIED BY 'ssapdrow'; 2.更改密码: >SET PASSWORD FOR name=PAS ...

  2. 【拉格朗日插值法】【找规律】【高精度】Gym - 101156G - Non-Attacking Queens

    题意:问你n*n的国际象棋棋盘上放3个互不攻击皇后的方案数. oeis……公式见代码内 //a(n) = 5a(n - 1) - 8a(n - 2) + 14a(n - 4) - 14a(n - 5) ...

  3. Javascript 继承-原型的陷阱

    注:本文为翻译文章,原文为"JavaScript Inheritance – How To Shoot Yourself In the Foot With Prototypes!" ...

  4. Codeforces Round #361 (Div. 2) D. Friends and Subsequences 二分

    D. Friends and Subsequences 题目连接: http://www.codeforces.com/contest/689/problem/D Description Mike a ...

  5. java 程序消耗 cpu 100% 查找方法

    问题原因:由于HashMap是非线程安全的,在多线程访问时,造成死循环. 查找问题方法: 1. top 找出最耗费cpu的进程号 如:27377 2. top -p 27377 -H 找出此进程下的所 ...

  6. 【Eclipse提高开发速度-插件篇】安装多语言Propertys编辑工具

    安装的的时候能够通过 Help>Eclipse Marketplace的 搜索方式安装 或者 Help>Install New Software Name:Propertys URL:ht ...

  7. android点滴之ContentObserver的使用

    一概念 ContentObserver用于观察(捕捉)特定Uri引起的数据的变化,继而做一些对应的处理,当ContentObserver所观察的Uri发生变化时,便会触发它. 从概念看ContentO ...

  8. 通过adb把apk安装到系统分区

    通过adb把apk安装到系统分区 以谷歌拼音为例:GooglePinyin1.4.2.apk提取出so文件libjni_googlepinyinime_4.solibjni_googlepinyini ...

  9. 给Android组件添加事件一个很好用的方法

    在这里想和大家分享一下很好用的添加事件方法,特别是在处理ListView里的Item事件的时候,很方便. 首先,在XML里布局的时候,添加这样一个属性: android:onClick="C ...

  10. C#程序集系列01,用记事本编写C#,IL代码,用DOS命令编译程序集,运行程序

    本篇主要体验:编写C#,IL代码,用"VS2012开发人员命令提示"编译成程序集,并运行程序. □ C#文件编译为程序集 →在F盘创建as文件夹→在as文件夹下创建MyClass. ...