[Functional Programming] Pointy Functor Factory
A pointed functor is a functor with an
ofmethod
class IO {
// The value we take for IO is always a function!
static of (x) {
return new IO(() => x)
}
constructor (fn) {
this.$value = fn;
}
map (fn) {
return new IO(compose(fn, this.$value))
}
}
What's important here is the ability to drop any value in our type and start mapping away.
IO.of('tetris').map(concat(' master'));
// IO('tetris master')
Maybe.of().map(add());
// Maybe(1337)
Task.of([{ id: }, { id: }]).map(map(prop('id')));
// Task([2,3])
Either.of('The past, present and future walk into a bar...').map(concat('it was tense.'));
// Right('The past, present and future walk into a bar...it was tense.')
The benifits using 'of' instead of 'new' is that we can map over the 'Task.of().map(fn)' right away.
Consider this:
const m = new Maybe():
m.map(add()) //VS Maybe.of().map(add());
To avoid the new keyword, there are several standard JavaScript tricks or libraries so let's use them and use of like a responsible adult from here on out. I recommend using functor instances from folktale, ramda or fantasy-land as they provide the correct of method as well as nice constructors that don't rely on new.
[Functional Programming] Pointy Functor Factory的更多相关文章
- [Functional Programming] Arrow Functor with contramap
What is Arrow Functor? Arrow is a Profunctor that lifts a function of type a -> b and allows for ...
- Functional Programming without Lambda - Part 2 Lifting, Functor, Monad
Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...
- 关于函数式编程(Functional Programming)
初学函数式编程,相信很多程序员兄弟们对于这个名字熟悉又陌生.函数,对于程序员来说并不陌生,编程对于程序员来说也并不陌生,但是函数式编程语言(Functional Programming languag ...
- [Functional Programming] Function signature
It is really important to understand function signature in functional programming. The the code exam ...
- Beginning Scala study note(4) Functional Programming in Scala
1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...
- 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 ...
- 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 ...
- Functional programming
In computer science, functional programming is a programming paradigm, a style of building the struc ...
- Java 中的函数式编程(Functional Programming):Lambda 初识
Java 8 发布带来的一个主要特性就是对函数式编程的支持. 而 Lambda 表达式就是一个新的并且很重要的一个概念. 它提供了一个简单并且很简洁的编码方式. 首先从几个简单的 Lambda 表达式 ...
随机推荐
- Bzoj5188/洛谷P4185 [Usaco2018 Jan]MooTube(并查集)
题面 Bzoj 洛谷 题解 最暴力的方法是直接判两个点之间的路径最小值是否\(\geq k\),用\(Dijkstra\)可以做到该算法最快效率,但是空间复杂度始终是\(O(n^2)\)的,会\(ML ...
- 「UOJ351」新年的叶子
「UOJ351」新年的叶子 题目描述 有一棵大小为 \(n\) 的树,每次随机将一个叶子染黑,可以重复染,问期望染多少次后树的直径会缩小. \(1 \leq n \leq 5 \times 10^5\ ...
- [CC-CHEFGRPH]Time to Study Graphs with Chef
[CC-CHEFGRPH]Time to Study Graphs with Chef 题目大意: 一个有向图可以分成\(n+2(n\le10^{12})\)层,第\(0\)层和第\(n+1\)层有\ ...
- bzoj 4769: 超级贞鱼 -- 归并排序
4769: 超级贞鱼 Time Limit: 1 Sec Memory Limit: 128 MB Description 马达加斯加贞鱼是一种神奇的双脚贞鱼,它们把自己的智慧写在脚上——每只贞鱼的 ...
- ubuntu 关闭n卡
ubuntu对n卡支持不好,电脑耗电和发汤,把它关闭掉 #sudo add-apt-repository ppa:bumblebee/stable#sudo apt-get update#su ...
- TEA加密算法java版
这个算法简单,而且效率高,每次可以操作8个字节的数据,加密解密的KEY为16字节,即包含4个int数据的int型数组,加密轮数应为8的倍数,一般比较常用的轮数为64,32,16,推荐用64轮. 源代码 ...
- 几个未公开的 DBCC 命令
http://blog.csdn.net/CathySun118/article/category/538610 https://ask.hellobi.com/blog/lyhabc/1612 1. ...
- ArcGIS for Android 点击选择
public void onSingleTap(float arg0, float arg1) { //selFeatureLayer.selectFeature(5); Point p = mMap ...
- Java:volatile 关键字的一点理解
背景 学了六年C#,一直没有使用过 volatile,对多线程编程也是偶尔才会使用,这次学习 Java 又遇到了 volatile,准备稍微深入的了解一下. volatile 的作用? 几乎所有支持这 ...
- linux 程序移植到Android
用动态链接的方法: arm-linux-gcc hello.c -o hello.out -Wl,-dynamic-linker=/system/lib/ld-linux.so.3 并且拷贝文件到安卓 ...