[Compose] 21. Apply Natural Transformations in everyday work

We see three varied examples of where natural transformations come in handy.

const Right = x => ({
    chain    : f => f(x),
    ap       : other => other.map(x),
    traverse : (of, f) => f(x).map(Right),
    map      : f => Right(f(x)),
    fold     : (f, g) => g(x),
    concat   : o => o.fold(_ => Right(x), y => Right(x.concat(y))),
    toString : () => `Right(${x})`
});

const Left = x => ({
    chain    : f => Left(x),
    ap       : other => Left(x),
    traverse : (of, f) => of(Left(x)),
    map      : f => Left(x),
    fold     : (f, g) => f(x),
    concat   : o => o.fold(_ => Left(x), y => o),
    toString : () => `Left(${x})`
});

const fromNullable = x => x != null ?
                          Right(x) :
                          Left(null);

const Task = require(‘data.task‘);
const {List} = require(‘immutable-ext‘);

const fake = id =>
    ({
        id,
        name: ‘user1‘,
        best_friend_id: id + 1
    });

const Db = ({
    find: id =>
        new Task((rej, res) => {
            res(id > 2 ? Right(fake(id)) : Left(‘not found‘))
        })
});

const eitherToTask = e =>
    e.fold(
        Task.rejected,
        Task.of
    ); // Right(x) --> Task(user)

const res = Db.find(3) // Task(Right(user))
    .chain(eitherToTask) // Task(user)
    .chain(user =>
        Db.find(user.best_friend_id)
    ) // Task(Right(user))
    .chain(eitherToTask) // Task(user)
    .fork(e => console.error(e),
          r => console.log(r)
    );
时间: 2024-10-12 21:43:21

[Compose] 21. Apply Natural Transformations in everyday work的相关文章

[Compose] 16. Apply multiple functors as arguments to a function (Applicatives)

We find a couple of DOM nodes that may or may not exist and run a calculation on the page height using applicatives. For example we want to get the main content section size by reduce the height of header and footer, nomarlly we will do like this: 1.

2D Transformations

以下是关于2D的一些理论以及推导过程,详细描述了图像的平移.缩放以及旋转等: This article is from: 2D Transformations Transformaions are a fundamental part of computer graphics. Transformations are used to position objects, to shape objects, to change viewing positions, and even to chang

(译)Minimal Shader(最小的着色器)

(原文:https://en.wikibooks.org/wiki/Cg_Programming/Unity/Minimal_Shader) This tutorial covers the basic steps to create a minimal Cg shader in Unity. 本节课包含了在Unity中创建一个最小的Cg着色器的基本步骤. Starting Unity and Creating a New Project(打开Unity创建一个新工程) After downlo

iOS Drawing Concepts[iOS 绘画概念]

iOS Drawing Concepts https://developer.apple.com/library/ios/documentation/2DDrawing/Conceptual/DrawingPrintingiOS/GraphicsDrawingOverview/GraphicsDrawingOverview.html High-quality graphics are an important part of your app’s user interface. Providin

Linq 动态查询排序

Linq的排序一般是这样写的: query.OrderBy(x => x.Tel).Skip(0).Take(10); 实际使用中排序字段可能是通过字符类型的参数来设置的,于是想这样实现: query.OrderBy(x=>x.GetType().GetField("Tel")).Skip(0).Take(10); 上面的写法是无法编译通过的,此路不通,于是找到一个order扩展类: 1 using System; 2 using System.Linq; 3 using

[Javascript] Functor law

Functor laws: 1. Identity: map(id) == id 2. Composition: compose(map(f), map(g)) == map(compose(f,g)) compose( map(toUpper), map(reverse), toArray )("bingo"); compose( map( compose(toUpper reverse), toArray ) )("bingo") Natural Transfo

[转载] 首席工程师揭秘:LinkedIn大数据后台是如何运作的?(一)

本文作者:Jay Kreps,linkedin公司首席工程师:文章来自于他在linkedin上的分享:原文标题:The Log: What every software engineer should know about real-time data’s unifying abstraction. 文章内容非常干货,非常值得学习.文章将以四部分进行阐述,建议大家耐心看完. 第一部分:Log是什么? 第二部分:数据集成 第三部分:日志和实时流处理 第四部分:系统建设 我在六年前的一个令人兴奋的时

C#强力粉碎文件代码分享

类似360的文件粉碎机 7 using System; 8 using System.Collections.Generic; 9 using System.ComponentModel; 10 using System.Data; 11 using System.Drawing; 12 using System.Linq; 13 using System.Text; 14 using System.Windows.Forms; 15 using System.IO; 16 using Syst

ANSI_common-lisp

前言 本书的目的是快速及全面的教你 Common Lisp 的有关知识.它实际上包含两本书.前半部分用大量的例子来解释 Common Lisp 里面重要的概念.后半部分是一个最新 Common Lisp 辞典,涵盖了所有 ANSI Common Lisp 的操作符. 这本书面向的读者 ANSI Common Lisp 这本书适合学生或者是专业的程序员去读.本书假设读者阅读前没有 Lisp 的相关知识.有别的程序语言的编程经验也许对读本书有帮助,但也不是必须的.本书从解释 Lisp 中最基本的概念