Maybe has two types: Just / Nothing. Just() will just return the value that passed in. Nothing returns nothing...

Just/ Nothing are both functors, they should have .map() method:

const Just = x => ({
map: f => Just(f(x)),
inspect: () => `Just ${x}`,
}) const Nothing = x => ({
map: f => Nothing('Nothing'),
inspect: () => `${x}`,
}) const Maybe = {
Just,
Nothing
}

We added 'inspect' method so that in REPL we can log out understandable message. for example, 'Just 4'  instead of '{ map: [Function: map] }'.... or whatever...

Currently, the code below return 'Just 5'

const inc = n => n + ;

const input = Just()
const result = input.map(inc)

But we don't need 'Just' as a result, we want just 5; in order to achieve that, we add 'option()' method:

const Just = x => ({
map: f => Just(f(x)),
inspect: () => `Just ${x}`,
option: (_) => x,
}) const Nothing = x => ({
map: f => Nothing('Nothing'),
inspect: () => `${x}`,
option: defVal => defVal
})

For Just, it return whatever the current value 'x', ignore the default value we passed in; Nothing it will return the defautl vlaue back.

Now, we got:

const input = Just(4)
const result = input.map(inc).option() // const input = Nothing()
const result = input.map(in) // Nothing
const result = input.map(inc).option() //

Since we don't know it should be Just or Nothing, we can use some predict function as helper:

const safeNum = num => typeof num === 'number' ? Maybe.Just(num): Maybe.Nothing()
const input = safeNum()
const result = input.map(inc).option() // const input = safeNum('')
const result = input.map(inc).option() //

---------------

const Just = x => ({
map: f => Just(f(x)),
inspect: () => `Just ${x}`,
option: (_) => x,
}) const Nothing = x => ({
map: f => Nothing('Nothing'),
inspect: () => `${x}`,
option: defVal => defVal
}) const safeNum = num => typeof num === 'number' ? Maybe.Just(num): Maybe.Nothing() const Maybe = {
Just,
Nothing
} const inc = n => n + ; const input = safeNum()
const result = input.map(inc).option() console.log(result)

[Functional Programming] Write a simple version of Maybe的更多相关文章

  1. [Functional Programming] Write simple Semigroups type

    An introduction to concatting items via the formal Semi-group interface. Semi-groups are simply a ty ...

  2. Functional Programming without Lambda - Part 2 Lifting, Functor, Monad

    Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...

  3. Functional Programming without Lambda - Part 1 Functional Composition

    Functions in Java Prior to the introduction of Lambda Expressions feature in version 8, Java had lon ...

  4. a primary example for Functional programming in javascript

    background In pursuit of a real-world application, let’s say we need an e-commerce web applicationfo ...

  5. Functional programming

    In computer science, functional programming is a programming paradigm, a style of building the struc ...

  6. BETTER SUPPORT FOR FUNCTIONAL PROGRAMMING IN ANGULAR 2

    In this blog post I will talk about the changes coming in Angular 2 that will improve its support fo ...

  7. [Functional Programming] Function signature

    It is really important to understand function signature in functional programming. The the code exam ...

  8. Monad (functional programming)

    In functional programming, a monad is a design pattern that defines how functions, actions, inputs, ...

  9. Beginning Scala study note(4) Functional Programming in Scala

    1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...

随机推荐

  1. 路径方案数 [SPFA,拓扑排序]

    路径方案数 [题目描述] 给一张无向图,n 个点和 m 条边,cyb 在 1 号点,他要去 2 号点, cyb 可以从 a 走到 b,当且仅当 a 到 2 的最短路,比 b 到 2 的最短路长. 求 ...

  2. bash shell 关系

    linux的bash和shell关系 shell通俗理解:把用户输入的命令翻译给操作系统. shell 是一个交互性命令解释器.shell独立于操作系统,这种设计让用户可以灵活选择适合自己的shell ...

  3. Unity 2D游戏开发教程之游戏中精灵的跳跃状态

    Unity 2D游戏开发教程之游戏中精灵的跳跃状态 精灵的跳跃状态 为了让游戏中的精灵有更大的活动范围,上一节为游戏场景添加了多个地面,于是精灵可以从高的地面移动到低的地面处,如图2-14所示.但是却 ...

  4. RxSwift 系列(六)

    前言 本篇文章将要学习RxSwift中数学和集合操作符,在RxSwift中包括了: toArray reduce concat toArray 将一个Observable序列转化为一个数组,并转换为一 ...

  5. Google的代码高亮-code-prettify

    前不久发现,在wordpress中贴代码的时候,发现code标签并没有意料中的好使用,在贴代码的时候没有高亮真的是一件无法忍受的事情. 正巧,两周前听过同事Eason的一个关于Markdown的分享, ...

  6. java8新特性——Stream API

    Java8中有两大最为重要得改变,其一时Lambda表达式,另外就是 Stream API了.在前面几篇中简单学习了Lambda表达式得语法,以及函数式接口.本文就来简单学习一下Stream API( ...

  7. Codeforces 408 E. Curious Array

    $ >Codeforces \space 408 E. Curious Array<$ 题目大意 : 有一个长度为 \(n\) 的序列 \(a\) ,\(m\) 次操作,每一次操作给出 \ ...

  8. [BZOJ4554][TJOI2016&&HEOI2016]游戏(匈牙利)

    4554: [Tjoi2016&Heoi2016]游戏 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 857  Solved: 506[Sub ...

  9. Java的运行机制概括

    这次随笔主要记录一下我对Java的平台无关性一些新的理解,以前只知道是Java是一门很容易跨平台的语言,正如 "Compile once, run anywhere" 这句话,也知 ...

  10. ThinkPHP -- 去除URL中的index.php

    原路径是 http://localhost/test/index.php/index/add 想获得的地址是 http://localhost/test/index/add 那么如何去掉index.p ...