zoukankan      html  css  js  c++  java
  • [Functional Programming] Capture Side Effects in a Task / Async

    We examine the data structure Task, see some constructors, familiar methods, and finally how it captures side effects through laziness.

    We are going to check two libarays, one is 'data.task'. another is 'crocks/Async':

    Install:

    npm i -S data.task
    npm i -S crocks

    You can use 'of' construct method:

     Task.of(1)
        .fork(e => console.error(e), a => console.log(a)) // 1
    
     Async.of('U Wut M8')
      .fork(e => console.error(e),a => console.log(a)) // U Wut M8

    You can do rejection:

     // Foucs to reject:
     Task.rejected('not work')   
     .fork(e => console.error(e), a => console.log(a)) // not work
    
     Async.Rejected('Async badguy')
      .fork(e => console.error(e),a => console.log(a)) // Async badguy

    You can .map / .chain:

     Task.of(1)
        .map(x => x + 1)
        .fork(e => console.error(e), a => console.log(a)) // 2
    
    
    Task.rejected(1)
        .map(x => x + 1)
        .fork(e => console.error(e), a => console.log(a)) // 1 
        
    Async.of(1)
        .map(x => x + 1)
        .fork(e => console.error(e),a => console.log(a)) //2
      
    Async.Rejected(1)
        .map(x => x + 1)
        .fork(e => console.error(e),a => console.log(a)) // 1
    
    Task.of(1)
        .map(x => x + 1)
        .chain(x => Task.of(x + 2))
        .fork(e => console.error(e), a => console.log(a)) // 4    
    
    Async.of(1)
        .map(x => x + 1)
        .chain(x => Async.of(x + 2))
        .fork(e => console.error(e),a => console.log(a)) // 4  

    You can use 'contructor function':

    const lunchMissiles = () =>
        new Task((rej, res) => {
            console.log('lunchMissiles');
            res('missile!')
        });    
      
    const lunchRocky = () =>
        Async((rej, res) => {
            console.log('lunchRocky');
            res('Rocky!')
        });       
    
    lunchMissiles()
        .map(x => x + "!")
        .fork(e => console.error(e), a => console.log(a)) // lunchMissiles missile!!
    
    
    lunchRocky()
        .map(x => x + "!")
        .fork(e => console.error(e), a => console.log(a)) // lunchMissiles missile!!    

    Finally, we can split the side effect without calling 'fork', and you compose with the rest of app:

    const taskApp =  lunchMissiles()
    .map(x => x + "!");
    
    const asyncApp = lunchRocky()
        .map(x => x + "!")
    
    
    taskApp.map(x => "   From Task").fork(e => console.error(e), a => console.log(a))
    asyncApp.map(x => "   From Async").fork(e => console.error(e), a => console.log(a))
  • 相关阅读:
    译文-浏览器下载图片的方式和时间点
    总结一下各种0.5px的线
    CSS3渐变效果工具
    [CSS]《CSS揭秘》第四章——视觉效果
    如何机制地回答浏览器兼容性问题
    如何更愉快地使用em —— 别说你懂CSS相对单位
    CSS学习(二):背景图片如何定位?
    React-简单通用的抛物线动画
    如何更愉快地使用rem —— 别说你懂CSS相对单位
    linuxC进程间通信的几种方式
  • 原文地址:https://www.cnblogs.com/Answer1215/p/10439223.html
Copyright © 2011-2022 走看看