[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 ...
随机推荐
- oracle like 条件拼接
(1) ibatis xml配置:下面的写法只是简单的转义 namelike '%$name$%' (2) 这时会导致sql注入问题,比如参数name传进一个单引号“'”,生成的sql语句会是:nam ...
- 谢宝友 LINUX 内核专家-----LINUX内核注释
http://download.csdn.net/user/xiebaoyou http://blog.chinaunix.net/uid/25845340.html
- Consul替代Eureka
原文:https://www.cnblogs.com/ityouknow/p/9340591.html 在上个月我们知道 Eureka 2.X 遇到困难停止开发了,但其实对国内的用户影响甚小,一方面国 ...
- 纸牌屋第一季/全集House of Cards迅雷下载
纸牌屋 第一季 House of Cards Season 1 (2013) 本季看点:经过数轮激烈角逐,新一届美国总统加勒特·沃克(迈克·吉尔 Michael Gill 饰)诞生,自称水管工的众议院 ...
- Android:客户端和服务器之间传输数据加密
Android客户端与服务器进行数据传输时,一般会涉及到两类数据的加密情况,一类是只有创建者才能知道的数据,比如密码:另一类是其他比较重要的,但是可以逆向解密的数据. 第一类:密码类的数据,为了让用户 ...
- docker 查看容器的网络连接
#! /bin/bash echo $1 PID=$(docker inspect -f '{{.State.Pid}}' $1) nsenter -t $PID -n netstat |grep E ...
- 《软件定义网络:SDN与OpenFlow解析》
<软件定义网络:SDN与OpenFlow解析> 基本信息 原书名:SDN: Software Defined Networks 原出版社: O'Reilly Media 作者: (美)Th ...
- C#多线程读写同一文件处理
在多线程访问读写同一个文件时,经常遇到异常:“文件正在由另一进程使用,因此该进程无法访问此文件”. 多线程访问统一资源的异常, 解决方案1,保证读写操作单线程执行,可以使用lock 解决方案2,使用S ...
- LaTeX技巧207:使用align环境输入多行公式的技巧
align是输入多行公式中最好用的环境,仅仅是个人浅见,因为他的对齐非常灵活,如果大家需要非常灵巧的对齐方式的多行公式,建议使用align环境,对应的也还有align*和aligned等等类似的环境, ...
- 使用强大的 Mockito 测试框架来测试你的代码
原文链接 : Unit tests with Mockito - Tutorial 译文出自 : 掘金翻译计划 译者 : edvardhua 校对者: hackerkevin, futureshine ...