[Functional Programming] Arrow Functor with contramap
What is Arrow Functor?
Arrowis aProfunctorthat lifts a function of typea -> band allows for lazy execution of the function.Arrowcan be considered aStrong Profunctorif the underlying data running through theArrowis aPair, typically in the form ofArrow (Pair a c) (Pair b d).This will allow you to split execution into two distinct paths, applying
Arrowto a specific path. The parameters ofArrowrepresent the function that it wraps, with the input being on the left, and the output on the right. When anArrowwraps an endomorphism, the signature typically represents both the input and output.
In short, Arrow is
- Take a function
- Function will be lazy
- We can apply params later
const Arrow = require('crocks/Arrow');
const arrUpper = Arrow(
str => str.toUpperCase()
)
log(
arrUpper.runWith('zhentian')
) // ZHENTIAN
Why this can be useful?
In this post, we are going to see an exmple about how to use 'Arrow' with 'contramap':
Currently 'arrUpper' take a string as input, sometimes the data we receive might be is an Object:
// {name: 'zhentian'}
In this case, we cannot directly using 'arrUpper', and we don't want to modify our 'arrUpper' function to be:
const arrUpper = Arrow(o => o.name && o.name.toUpperCase())
What we can do is using 'contramap' to apply an function on the input param of 'arrUpper', to change the Object type to String type, we can keep the 'arrUpper' untouched:
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 nameUpper = arrUpper
.contramap(getName)
log(
nameUpper.runWith({name: 'zhentian'})
) // ZHENTIAN
What 'contramap' does is apply the given function (getName) to the target function (arrUpper)'s params ({name: 'zhentian'}), before the target function get invoked.
So in our example, we transform the {name: 'zhentian'} to just 'zhentian' or 'no name' before passing to arrUpper.
[Functional Programming] Arrow Functor with contramap的更多相关文章
- [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 in ...
- [Functional Programming] Pointy Functor Factory
A pointed functor is a functor with an of method class IO { // The value we take for IO is always a ...
- Functional Programming without Lambda - Part 2 Lifting, Functor, Monad
Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...
- 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)
初学函数式编程,相信很多程序员兄弟们对于这个名字熟悉又陌生.函数,对于程序员来说并不陌生,编程对于程序员来说也并不陌生,但是函数式编程语言(Functional Programming languag ...
- [Functional Programming] Function signature
It is really important to understand function signature in functional programming. The the code exam ...
- 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 ...
随机推荐
- HDU 3472 HS BDC (混合图的欧拉路径判断)
HS BDC Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- GUN WINDOW 工具
GNU utilities for Win32 CoreUtils for Windows 或者 完整的 package dd for windows Unix ports - WHICH, TEE ...
- JavaWeb系列之八(Cookie&Session)
1.jsp的入门 jsp就是一个servlet,终于会被编译成servlet,jsp:java server pages,java服务器端页面,包括html+java+jsp的指令 ...
- C++ classes and uniform initialization
// classes and uniform initialization #include <iostream> using namespace std; class Circle ...
- delphi 文件查找
FindFirst 是用来寻找目标目录下的第一个文件, FindFirst函数在delphi帮助下的定义: function FindFirst(const Path: string; Attr: ...
- 学一点 MYSQL 双机异地热备份—-MYSQL主从,主主备份原理及实践
简单介绍mysql双机,多机异地热备简单原理实战. 双机热备的概念简单说一下,就是要保持两个数据库的状态自动同步.对任何一个数据库的操作都自动应用到另外一个数据库,始终保持两个数据库数据一致. 这样做 ...
- SAP BAPI一览 史上最全
全BADI一览 List of BAPI's BAPI WG Component Function module name Description Description Obj. Ty ...
- NoSQL现状
经过了至少4年的激烈争论,现在是对NoSQL的现状做一个阶段性结论的时候了.围绕着NoSQL发生了如此之多的事情,以至于很难对其作出一个简单概括,也很难判断它达到了什么目标以及在什么方面没有达到预期. ...
- Linux文本行倒序排列6种方法
Linux文本行倒序排列6种方法 1. 命令方法: nl filename | sort -nr | cut -f2 这个方法很unix风格,使用多命令组合完成某种功能是典型的unix特点 单命令: ...
- java 常用集合list与Set、Map区别及适用场景总结
转载请备注出自于:http://blog.csdn.net/qq_22118507/article/details/51576319 list与Set.Map区别及 ...