[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 ...
随机推荐
- C++学习笔记23,类内函数重载
该博文仅用于交流学习.请慎用于不论什么商业用途,本博主保留对该博文的一切权利. 博主博客:http://blog.csdn.net/qq844352155 转载请注明出处: 在一个类内,最常见的就是构 ...
- 使用Brackets
Brackets功能还是很强大的. 官网:brackets.io常见问题解决:https://github.com/adobe/brackets/wiki/Troubleshooting快捷键:htt ...
- url提交参数类
url提交参数类 type /// <summary> /// 准备url /// </summary> TynUrl = class private FUrl, FComma ...
- Chrome 如何知道网站启用了SPDY 协议?
地址栏输入chrome://net-internals/#spdy 在host后查看协议,google和dropbox用https协议的开启了 3. 也可以通过安装插件来查看(SPDY Indicat ...
- 缩放到被选择的部分: ICommand Cmd = new ControlsZoomToSelectedCommandClass();
AddItem("esriControls.ControlsZoomToSelectedCommand"); //ICommand Cmd = new ControlsZoomTo ...
- 【Devops】【docker】【CI/CD】3.Jenkins+GitLab+docker+springboot 实现自动化部署
==================================================================================================== ...
- jquery入门 改动网页背景颜色
我们在浏览一些站点,尤其是一些小说站点的时候,都会有改动页面背景颜色的地方,这个功能使用jquery非常easy实现. 效果图: show you code: <!doctype html> ...
- 对Java通配符的个人理解(以集合为例)
对Java通配符的个人理解(以集合为例) 前言:最近在学习Java,当学到了泛型的通配符时,不是很理解PECS(Producer Extends Consumer Super)原则,以及<? e ...
- Netty端口被占用问题
问题: 最近发现Netty项目每次发布的时候Netty在重启时都会报端口被占用的异常, 需要过十几秒左右手动重启一遍, Netty才能恢复正常 目前猜测是由于Tomcat_restart的时候Ne ...
- 关于XSHM(Cross-Site History Manipulation)
http://blog.chinaunix.net/uid-27070210-id-3255407.html 乍一看,好像和以前 css history hack 差不多,其实原理还是不一样的.浏览器 ...