What is Arrow Functor?

Arrow is a Profunctor that lifts a function of type a -> b and allows for lazy execution of the function. Arrow can be considered a Strong Profunctorif the underlying data running through the Arrow is a Pair, typically in the form of Arrow (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 of Arrow 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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. Functional Programming without Lambda - Part 2 Lifting, Functor, Monad

    Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...

  4. Beginning Scala study note(4) Functional Programming in Scala

    1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...

  5. 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 ...

  6. 关于函数式编程(Functional Programming)

    初学函数式编程,相信很多程序员兄弟们对于这个名字熟悉又陌生.函数,对于程序员来说并不陌生,编程对于程序员来说也并不陌生,但是函数式编程语言(Functional Programming languag ...

  7. [Functional Programming] Function signature

    It is really important to understand function signature in functional programming. The the code exam ...

  8. 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 ...

  9. Functional programming

    In computer science, functional programming is a programming paradigm, a style of building the struc ...

随机推荐

  1. 谨慎注意WebBrowser控件的DocumentCompleted事件

    引言 WebBrowser控件的DocumentCompleted事件一般就被认定为是在页面完全加载完毕后产生,而注释中也是这么写的: 但事实却并非如此. 首先它不一定会在完全加载完毕时才触发,有时就 ...

  2. 作为互联网人,你必须知道的一些IT类网站

  3. 报错:Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

    在保存数据的时候报这个错误,知道是验证错误,但到底是哪个属性验证错误呢? →打断点,运行,观察程序出错的地方→在出错的部分添加try...catch语句块→添加监视,输入((System.Data.E ...

  4. 选股:“均线是水,K线是舟,量是马达!”的选美理念!

    选股:“均线是水,K线是舟,量是马达!”的选美理念! 很多庄家就是故意做数据,让某只股票的数据非常符合“理论”,引诱“技术派”股民

  5. .NET:自定义配置节

    背景 对于编译型应用程序来说,参数化程序行为是非常有必要的,.NET有其标准的配置方法,我们可以可以扩展. 示例 代码 using System; using System.Collections; ...

  6. jQuery Ajax 上传文件夹及文件

    我们先来看一下文件夹结构 这是上传处理的: 看一下系统日志: 升级 HTML5文件实现拖拽上传提示效果改进(支持三种状态提示) 拖拽过程详解: 1:文件未拖出文件选择框的时候提示:将要上传的文件或文件 ...

  7. UITextField的简易封装

    UITextField的简易封装 效果 源码 https://github.com/YouXianMing/UI-Component-Collection 中的 UITextFieldView // ...

  8. 数学图形(1.48)Cranioid curve头颅线

    这是一种形似乎头颅的曲线.这种曲线让我想起读研的时候,搞的医学图像三维可视化.那时的原始数据为脑部CT图像.而三维重建中有一种方式是面绘制,是将每一幅CT的颅骨轮廓提取出来,然后一层层地罗列在一起,生 ...

  9. ZooKeeper服务命令

    ZooKeeper服务命令 在准备好相应的配置之后,可以直接通过zkServer.sh 这个脚本进行服务的相关操作 启动ZK服务: sh bin/zkServer.sh start 查看ZK服务状态: ...

  10. Poly2Tri介绍[转]

    https://blog.csdn.net/xys206006/article/details/83002326 这是Poly2Tri介绍的翻译博文.原文链接:http://sites-final.u ...