[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).字幕 ...
随机推荐
- TCP编程的迷惑
server : ip -- 192.168.96.132 client: ip--192.168.96.131 在服务端,accept函数的其中一个入参是listen-socket,会返回一个新的c ...
- WinForm MDIParent如何防止重复打开
DI,全称是多文档界面(Multiple Document Interface),主要应用于基于图形用户界面的系统中.其目的是同时打开和显示多个文档,便于参考和编辑资料. 下面是一个WinForm M ...
- 《完全用Linux工作》作者:王垠
完全用 GNU/Linux 工作 理解 GNU/Linux 注:本文是清华“牛仔”王垠的“成名作”,在网上引起很大的争议.对他崇拜地五体投地者有,对他嗤之以鼻者也有,总之成了一年多以前Linux 爱好 ...
- window.onbeforeunload() 事件调用ajax
经常有这样的需求,就是在离开某个web页面时,用户不一定点注销,这样会导致会话不能及时销毁.为实现用户离开页面时,自动注销功能,需要在web页面的onbeforeunload事件处理函数中发送注销命令 ...
- UIScrollView的判断位置的属性如下:
contentSize:CGSize类型,scrollview可以滑动的区域,例如,一个view的frame为(0,0,320,480),而scrollview的contentSize为(320,10 ...
- 在arcgis使用python脚本进行字段计算时是如何解决中文问题的
来自:https://www.jb51.net/article/73561.htm 一.引言 在arcgis打开一个图层的属性表,可以对属性表的某个字段进行计算,但是在平常一般都是使用arcgis提供 ...
- 在Activity中使用Thread导致的内存泄漏
https://github.com/bboyfeiyu/android-tech-frontier/tree/master/issue-7/%E5%9C%A8Activity%E4%B8%AD%E4 ...
- Android之找回打包key密码的方法
昨天准备给自己的应用发布一个新版本,在apk打包时,发现之前的用的keystore密码忘了.蛋碎了一地,我把我所能想到的密码都试了一遍(注:我平常在各个门户网站注册基本上用的都是那几个字母和数字组合做 ...
- [Android Security] APK自我保护 - 代码乱序
cp : https://segmentfault.com/a/1190000005095406 乱序原理 为了增加逆向分析的难度,可以将原有代码在 smali 格式上进行乱序处理同时又不会影响程序的 ...
- 【已解决】Android微信开放平台,申请移动应用的 应用签名 如何获取
在微信开放平台,申请移动应用的时候: https://open.weixin.qq.com/cgi-bin/appcreate?t=manage/createMobile&type=app&a ...