[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).字幕 ...
随机推荐
- LINUX 内核守护进程
http://alfred-sun.github.io/blog/2015/06/18/daemon-implementation/
- dos下 和 批处理中的 for 语句的基本用法
for 语句的基本用法 : 最复杂的for 语句,也有其基本形态,它的模样是这样的: 在cmd 窗口中:for %I in (command1) do command2 在批处理文件中:for % ...
- 对json数据key进行替换
原文:https://blog.csdn.net/qq_39750658/article/details/83411897 import java.util.HashMap; import java. ...
- DIOCP数据包太大,请在业务层分拆发送
DIOCP数据包太大,请在业务层分拆发送 DIOCP日志记录异常:数据包太大,请在业务层分拆发送...... 跟踪发现,原因在下图:
- ibatis.net:第六天,QueryForList
xml <statement id="FindOrdersByCustomer" parameterClass="string" resultClass= ...
- MySql、Oracle、MSSQL中的字符串的拼接
字符串的拼接 1,Mysql 在Java.C#等编程语言中字符串的拼接可以通过加号“+”来实现,比如:"1"+"3"."a"+"b ...
- 基于图的图像分割(Graph-Based Image Segmentation)
一.介绍 基于图的图像分割(Graph-Based Image Segmentation),论文<Efficient Graph-Based Image Segmentation>,P. ...
- Java Type Inference (类型推断)
public class Test2 { public static void main(String[] args) { ArrayList<String> list = newArra ...
- 【BZOJ】【4146】 【AMPPZ2014】Divisors
暴力 由于值的范围很小($ \leq 2*10^6$),所以用一个cnt数组统计每个值有多少个数,然后从小到大,统计每个数的倍数即可. 根据调和数?的神奇性质= =这样是$O(nlogn)$的…… / ...
- android adb源码分析(1)
ADB是Android debug bridge的缩写,它使用PC机可以通过USB或网络与android设备通讯. adb的源码位于system/core/adb目录下,先来看下编译脚本Android ...