[Functional Programming] Arrow contramap vs map and promap
In previous post, Arrow Functor with contramap, we have seen how to opreating on params before we invoke the function by using Arrow + contramap. It happens before function get inovked, before we get result. We can say this opreation happens on the left hand side of function (which is the params).
In this post, we will see how to opreate on the right handside of function, which is our result.
const Arrow = require('crocks/Arrow');
const chain = require('crocks/pointfree/chain');
const option = require('crocks/pointfree/option');
const prop = require('crocks/Maybe/prop');
const safe = require('crocks/Maybe/safe');
const getName = compose(
option('no name'),
chain(safe(isString)),
prop('name')
)
const arrUpper = Arrow(
str => str.toUpperCase()
)
const welcome = str => `Welcome. ${str}!`;
const nameUpper = arrUpper
.contramap(getName)
.map(welcome)
log(
nameUpper.runWith({name: 'zhentian'})
) // 'Welcome. ZHENTIAN!'
So after 'contramap', we chian 'map(welcome)'. this will wrap the result from 'arrUpper' into a new String.
Notice that it is equivalent that:
const nameUpper = arrUpper
.map(welcome)
.contramap(getName) const nameUpper = arrUpper
.contramap(getName)
.map(welcome)
We can also shorter the syntax by using 'promap', it is the same as 'contramap + map':
const nameUpper = arrUpper
.promap(getName, welcome)
[Functional Programming] Arrow contramap vs map and promap的更多相关文章
- [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 ...
- 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 ...
- Functional Programming without Lambda - Part 2 Lifting, Functor, Monad
Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...
- 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 ...
- 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 ...
- [Functional Programming] Function signature
It is really important to understand function signature in functional programming. The the code exam ...
- Sth about 函数式编程(Functional Programming)
今天开会提到了函数式编程,针对不同类型的百年城方式,查阅了一部分资料,展示如下: 编程语言一直到近代,从汇编到C到Java,都是站在计算机的角度,考虑CPU的运行模式和运行效率,以求通过设计一个高效的 ...
- Coursera公开课Functional Programming Principles in Scala习题解答:Week 2
引言 OK.时间非常快又过去了一周.第一周有五一假期所以感觉时间绰绰有余,这周中间没有假期仅仅能靠晚上加周末的时间来消化,事实上还是有点紧张呢! 后来发现每堂课的视频还有相应的课件(Slide).字幕 ...
随机推荐
- JTAG Communications model
https://en.wikipedia.org/wiki/Joint_Test_Action_Group In JTAG, devices expose one or more test acces ...
- javaDoc 注释规范
Javadoc虽然是Sun公司为Java文档自动生成设计的,可以从程序源代码中抽取类.方法.成员等注释形成一个和源代码配套的API帮助文档.但是Javadoc的注释也符合C的注释格式,而且doxyen ...
- [廖雪峰] Git 分支管理(2):Bug 分支
软件开发中,bug 就像家常便饭一样.有了 bug 就需要修复,在 Git 中,由于分支是如此的强大,所以,每个 bug 都可以通过一个新的临时分支来修复,修复后,合并分支,然后将临时分支删除. 当你 ...
- STL中经常使用数据结构
STL中经常使用的数据结构: [1] stack.queue默认的底层实现为deque结构. [2] deque:用map管理多个size大小的连续内存块,方便头尾插入. [3] vector: ...
- 对数据集“dsArea”执行查询失败。 (rsErrorExecutingCommand),Query execution failed for dataset 'dsArea'. (rsErrorExecutingCommand),Manually process the TFS data warehouse and analysis services cube
错误提示: 处理报表时出错. (rsProcessingAborted)对数据集“dsArea”执行查询失败. (rsErrorExecutingCommand)Team System 多维数据集或者 ...
- C++学习笔记23,类内函数重载
该博文仅用于交流学习.请慎用于不论什么商业用途,本博主保留对该博文的一切权利. 博主博客:http://blog.csdn.net/qq844352155 转载请注明出处: 在一个类内,最常见的就是构 ...
- 使用Brackets
Brackets功能还是很强大的. 官网:brackets.io常见问题解决:https://github.com/adobe/brackets/wiki/Troubleshooting快捷键:htt ...
- C#复制数组的两种方式,以及效率比较
如何高效地进行数组复制? 如果把一个变量值复制给另外一个数组变量,那么2个变量指向托管堆上同一个引用. 如果想在托管堆上创建另外的一份数组实例,通常使用Array.Copy方法. class Prog ...
- Ioc:autofac lifetime scope.
During application execution, you’ll need to make use of the components you registered. You do this ...
- Mac与Mac之中的共享方式
前往 ---> 连接服务器 ---> 输入smb://带连接的服务器地址(smb://192.168.1.40)即可