[Functional Programming] Arrow Functor with contramap
What is Arrow Functor?
Arrow
is aProfunctor
that lifts a function of typea -> b
and allows for lazy execution of the function.Arrow
can be considered aStrong Profunctor
if the underlying data running through theArrow
is aPair
, typically in the form ofArrow (Pair a c) (Pair b d)
.This will allow you to split execution into two distinct paths, applying
Arrow
to a specific path. The parameters ofArrow
represent the function that it wraps, with the input being on the left, and the output on the right. When anArrow
wraps 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 ...
随机推荐
- Bootstrap入门学习(三)——简单项目
此样例来自Bootstrap官网提供的入门级模版.仅仅有主要的东西:引入了预编译版的 CSS 和 JavaScript 文件,页面仅仅包括了一个 container 元素. 引入Bootstrap 创 ...
- Dynamic-Link Library Redirection
Dynamic-Link Library Redirection Applications can depend on a specific version of a shared DLL and s ...
- [Go] sync.Once 的用法
sync.Once.Do(f func()) 是一个非常有意思的东西,能保证 once 只执行一次,无论你是否更换 once.Do(xx) 这里的方法,这个 sync.Once块 只会执行一次. pa ...
- delphi TOnFormVisibleChangeEvent 事件应用
TGQIFileMgrForm = class(TForm) 定义 property OnVisibleChange: TOnFormVisibleChangeEvent read FOnVisibl ...
- mqtt介绍
MQTT是轻量级基于代理的发布/订阅的消息传输协议,它可以通过很少的代码和带宽和远程设备连接.例如通过卫星和代理连接,通过拨号和医疗保健提供者连接,以及在一些自动化或小型设备上,而且由于小巧,省电,协 ...
- C#编程(四十六)----------正则表达式
正则表达式 1.定义一个Regex类的实例 Regex regex=new Regex(“”); 这里初始化参数就是一个正则表达式,”\d”表示配置数字 2.判断是否匹配 判断一个字符串,是否匹配一个 ...
- 2018房地产沉思录 z
在中国,房价问题几乎有一个铁律:越调控越暴涨. 刚刚进入5月,全国各地发布的调控政策数量就已经超过了115个.仅4月份,全国各种房地产调控政策合计多达33次,25个城市与部门发布调控政策,其中海南.北 ...
- WordPress主题开发:网站搜索
调用方法一:手动输入html <form role="search" method="get" id="searchform" act ...
- [ACM] HDU 2255 奔小康赚大钱 (二分图最大权匹配,KM算法)
奔小康赚大钱 Problem Description 传说在遥远的地方有一个很富裕的村落,有一天,村长决定进行制度改革:又一次分配房子. 这但是一件大事,关系到人民的住房问题啊. 村里共同拥有n间房间 ...
- win7下设置环境变量
手工当然可以进行环境变量的设置,但是如果一个小组有需要设置固定环境变量的操作,这可能就会有点麻烦了,xp下设置环境变量比较简单,直接用set,win7下需要使用setx SETX XX_HOME &q ...