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. ARM Memory Copy

    MODULE ARM_MEMORY PUBLIC ARM_MEMCPY PUBLIC ARM_MEMSET PUBLIC ARM_MEMSET8 PUBLIC ARM_MEMSET16 PUBLIC ...

  2. USB设备的插入和弹出的监听以及软弹出可移动媒体(如Windows的移除USB设备) .

    一.监听USB设备的插入和弹出 当USB设备插入或者弹出时,Windows会产生一条全局消息:WM_DEVICECHANGE 我们需要做的是,获得这条消息的wParam参数,如果为DBT_DEVICE ...

  3. wordpress入门

    安装bitnami wordpress. 打开仪表盘:开始菜单--Bitnami Wordpress协议栈 Manager Tool -- Go to Appllication -- Access W ...

  4. Google 镜像站搜集

    在特殊的地方和特殊的时间,流畅顺利的打开一个网站也变得如此艰难. 2016.01.16 更新.本站订阅更新功能已上线,欢迎订阅! 以下是直接使用谷歌的方法,如需***戳这里(VPN | Shadows ...

  5. 利用Android Lost通过互联网或短信远程控制安卓设备

    利用Android Lost通过互联网或短信远程控制安卓设备 作者:Jack Wallen| 杰克·瓦伦翻译:PurpleEndurer.2014-11-15第1版 使用智能手机要考虑的一个至关重要的 ...

  6. Revit API创建一个拷贝房间内对象布局命令

    本课程演示创建一个拷贝房间内对象布局命令,完整演示步骤和代码.这个命令把选中房间内的对象复制到其它选中的一个或多个房间中,而且保持与源房间一致的相对位置.通过本讲座使听众知道创建一个二次开发程序很简单 ...

  7. 轮子科技的.NET Core分享

    2016年8月11日 应轮子科技一众好友的邀请,在轮子科技给大家做了一个无责任的瞎聊段落,聊聊.NET的Core的一些内容. 恩,演讲者就只有我一个了,讲师是微软的 MVP 杨守斌,就是因为这个,所以 ...

  8. 最简单的例子理解Javascript闭包

    理解Javascript的闭包非常关键,本篇试图用最简单的例子理解此概念. function greet(sth){ return function(name){ console.log(sth + ...

  9. Spring Boot开发之明月千城(一)

    原文地址:http://qindongliang.iteye.com/blog/2205633 最近数据分析的项目也即将告一段落了,中间也积累了很多知识,特此记录一下.其中用的最爽的Web组合开发就是 ...

  10. 多线程UI

    遇到过要在工作线程中去更新UI以让用户知道进度,而在多线程中直接调用UI控件操作是错误的做法. 最后解决方法是将操作UI的代码封装,通过Invoke / BeginInvoke 去委托调用. priv ...