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. 根据twitter的snowflake算法生成唯一ID

    C#版本 /// <summary> /// 根据twitter的snowflake算法生成唯一ID /// snowflake算法 64 位 /// 0---0000000000 000 ...

  2. C#远程调用技术WebService修炼手册

    一.课程介绍 一位伟大的讲师曾经说过一句话:事物存在即合理!意思就是说:任何存在的事物都有其存在的原因,存在的一切事物都可以找到其存在的理由,我们应当把焦点放在因果关联的本质上.所以在本次分享课开课之 ...

  3. Unity3D实践系列10, Canvas画布的创建和使用

    Canvas是所有ui元素的父物体. 当添加一个Button类型的GameObject后,在"Hierarch"窗口中自动添加了一个Canvas,以及EventSystem. 在C ...

  4. 关闭Delphi的RTTI

    {$IF CompilerVersion >= 21.0}{$WEAKLINKRTTI ON}{$RTTI EXPLICIT METHODS([]) PROPERTIES([]) FIELDS( ...

  5. spring-boot项目在eclipse中指定配置文件启动

    原文:https://blog.csdn.net/ztx114/article/details/80076339 如下图我的项目有三个配置文件,假如我向指定用application-test.yml启 ...

  6. WCF:该不该用枚举值

    WCF支持枚举,不过在个别场景下会出现服务消费失败,如:传递或返回的枚举值(本质是int或其它)没有在枚举中定义.这种异常还很难定位,出现这种情况一般是因为BUG,因此简单的放弃使用枚举可能不是一个明 ...

  7. Codeforces Round #131 Div1 B

    Problem 给出Ai(i∈[0,9]).表示每一个数字至少须要出现多少次.问有多少个数满足下面三个条件:1. 数至多有N位:2. 数不含有前导0:3. 每一个数 i 出现的次数不少于Ai(mod ...

  8. Java文件管理系统

    上一个版本设计参照了 windows 资源管理器的原则,使用了多年.也发现了少许问题,也许是我们应用场景不一样... 目前的设计结合了目前的云概念.目录采用的是虚拟方式和实际的目录无关,只是存在一定的 ...

  9. Universal-Image-Loader解析(三)——用ListView和ViewPager加载网络中的图片

           现在我们终于可以通过这个框架来实现ListView中加载图片了,至于ViewPager还是别的,原理其实都是一样的 一.ListView 1.布局文件 list_layout.xml & ...

  10. 让java从Mysql返回多个ResultSet

    首先,JDBC对于SQLSERVER来说默认是支持返回,但对于MySql来说,只默认支持存储过程返回多个ResultSet,那对于手写SQL怎么办. 其实很简单,只要一个在连接字符串中加一个参数:al ...