zoukankan      html  css  js  c++  java
  • [Compose] 15. Applicative Functors for multiple arguments

    Working our way backwards from solution to problem, we define an applicative functor, then use it to apply a function of multiple arguments.

    For example we have this line of code:

    const res = Box(x => x +1).ap(Box(2))// Box(3);

    We want to use a funciton 'ap' (apply) on Box. And x will be 2.

    To define 'ap' function.

    const Box = x =>
    ({
      chain: f => f(x),
      ap: other => other.map(x),
      map: f => Box(f(x)),
      fold: f => f(x),
      inspect: () => `Box(${x})`
    })

    So '

    Box(x => x +1).ap(Box(2))

    '

    Can be translated to:

    Box(2) => Box(2).map(x => x + 1);

    This can be useful when apply curry function:

    const res = Box(x => y => x + y).ap(Box(1)).ap(Box(2));
    console.log(res.inspect()); //Box(3)

    after apply .ap(Box(1)), it becomes to:

    Box(y => 1 +y).ap(Box(2))
    

    after apply .ap(Box(2)), it becomes to:

    Box(1 +2 )

    It ends up, we have a function and continue to using 'ap':

    const add = x => y => x + y;
    const res = Box(add).ap(Box(1)).ap(Box(2));

    This partten is called click-functor!

    The rule is:

    F(val).map(fn) === F(fn).ap(F(val))

    For example now we have:

    const liftA2 = (fn, Fx, Fy) => 
      F(fn).ap(Fx).ap(Fy);

    The problem is we don't know what 'F' it is here...

    So what we can do is transform accorind to the rule we have:

    const liftA2 = (fn, Fx, Fy) => 
      Fx.map(fn).ap(Fy)

    Therefore we don't need to memtion any Functor.

    Example:

    const res2 = liftA2(add, Box(1), Box(2));
    console.log(res2.inspect()); //Box(3)

    Applicate Functor is really good to work with Async functor, because async by natural, data arrives different time:

    const add = x => y => z=> x + y + z;
    const addAsyncNumbers = liftA3(add);
    const res = addAsyncNumbers(
        Async.of(5),
        Async((_, res) => {
            setTimeout(() => {
                console.log('resolve 2');
                res(2)
            }, 500)
    }), Async((_, res) => {
        setTimeout(() => {
            console.log('resolve 3');
            res(3)
        }, 600)
    }));
    res.fork(e => console.error(e), x => console.log('async', x)) // 10
  • 相关阅读:
    Java中List集合去除重复数据的方法
    ActiveMQ消息中间件Producer和Consumer
    JMS Activemq实战例子demo
    利用正则表达式判断输入内容是否全中文
    纯js的右下角弹窗
    java开源项目
    linux部署jdk-tomcat
    你的主机中的软件中止了一个已建立的连接。
    Centos编译安装 LAMP (apache-2.4.7 + mysql-5.5.35 + php 5.5.8)+ Redis
    在LAMP的生产环境内添加PHP的cURL扩展模块
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6204415.html
Copyright © 2011-2022 走看看