[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).字幕 ...
随机推荐
- HDU 3979 Monster (贪心排序)
Monster Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- 使用Edge模式通知Internet Explorer以最高级别的可用模式显示内容
一.EasyUI$的window('open')在IE8下兼容性问题 今天在公司使用EasyUI的$('#win').window('open');方法打开一个window窗体时发现EaysUI的脚本 ...
- mysql大文本数据类型的使用需要考虑实际情况
mysql数据类型简介(http://news.newhua.com/news1/program_database/2008/618/08618103911CD92HJ6CKI2I9I0AH5CGK1 ...
- (Delphi) Using the Disk Cache 使用磁盘缓存
The Chilkat Spider component has disk caching capabilities. To setup a disk cache, create a new dire ...
- zTree树
<link rel="stylesheet" href="/static/zTreeV3/css/metroStyle/metroStyle.css" t ...
- 【python】python读取文件报错UnicodeDecodeError: 'gbk' codec can't decode byte 0xac in position 2: illegal multibyte sequence
python读取文件报错UnicodeDecodeError: 'gbk' codec can't decode byte 0xac in position 2: illegal multibyte ...
- Oracle中删除用户下所有对象的多种方法
Oracle删除用户下所有对象的方法未必人人都会,下面就为您介绍两种常用的Oracle删除用户下所有对象的方法,希望对您学习Oracle删除用户方面能有所帮助. 方法1: drop user XX ...
- EditText 限制输入,自定义样式,监听输入的字符,自动换行
自动获取焦点 <!-- 添加:<requestFocus /> 会自动获取焦点 --> <EditText android:layout_width="matc ...
- 将iPod中的音乐拷贝到Mac中
需求目标 iPod 中有很多音乐是从原来的电脑中同步进去的,新的电脑中没有 iTunes 的音乐库.所有的音乐都在 iPod 中,会不会突然有一天坏掉了,还是备份到电脑中比较安心啊.那么如何把音乐从 ...
- js如何判断用户是在pc端和还是移动端访问
js如何判断用户是在pc端和还是移动端访问 来源:A5技术交流 作者:wofa 时间:2014-04-25收藏本页 最近一直在忙我们团队的项目“咖啡之翼”,在这个项目中,我们为移动平台提供了一个优秀的 ...