[Functional Programming] mapReduce over Async operations and fanout results in Pair(rejected, resolved) (fanout, flip, mapReduce)

This post is similar to previous post. The difference is in this post, we are going to see how to handle both successfuly result and error result by using Pair functor.

 

So, still we have our funs.js: which is the same as previous post.

复制代码
const fs = require('fs');
const {Async, constant, composeK, curry} = require('crocks');
const {fromNode} = Async;

const access = fromNode(fs.access);
const readFile = fromNode(fs.readFile);

const accessAsync = curry((mode, path) =>
  access(path, mode)
  .map(constant(path)));

// readFileAsync :: Option -> a -> Async Error b
const readFileAsync = curry((option, path) =>
    readFile(path, option));

const checkRead = accessAsync(fs.constants.F_OK);
const readTextFile = readFileAsync('utf-8');

// loadTextFile :: String -> Async Error String
const loadTextFile = composeK(
    readTextFile,
    checkRead
);

const fork = a => a.fork(
    console.log.bind(null, 'rej'),
    console.log.bind(null, 'res')
);

module.exports = {
    loadTextFile,
    fork
}
复制代码

 

For our main.js, we still have the same data input:

const data = [
    'text.txt',
    'text.big.txt',
    'notfound.txt',
];

This time the difference of requirements are:

1. we want to read those files one by one, keep all the successfully results in Pair(result, _);

2. we want to keep the error result in Pair(_, error);

 

复制代码
const concatSpecial = (acc, currAsync) =>
    acc.chain(
        xs => currAsync.bimap(
            e => Pair(xs, e),
            currVal =>  xs.concat(currVal))
    );

// Async (Pair [String] Error) [String]
const flow = mapReduce(
    loadTextFile,
    concatSpecial,
    Async.Resolved([])
);

flow(data).fork(
    e => console.log(e.snd(), e.fst()), // Pair(success, error)
    r => console.log(r), // Just success result
)
复制代码

We are still using 'mapRedcue' to map over each filename, fetching the content; then we call 'concatSpecial' method, we want to concat all the successful result into one array. Therefore we give an empty array wrapped in Async:

const flow = mapReduce(
    loadTextFile,
    concatSpecial,
    Async.Resolved([])
);

 

We can do some pointfree refactor for 'concatSpical', it's not necssary, but just as a partice:

复制代码
const fn = flip(
    xs => bimap(
        e => Pair(xs, e),
        currVal =>  xs.concat(currVal)
    )
);

const concatSpecial = (acc, currAsync) =>
    acc.chain(
        fn(currAsync)
    );
复制代码

For the function 'fn', we should take 'xs' as first param, then 'currAsync' as second param. 

But since we also pass in 'currAsync' as first param, then we need to use 'flip':

acc.chain(
    fn(currAsync) // pass currAsync as firt, then xs => fn(currAsync)(xs)
);

 

We can also replace 'Pair' with 'fanout':

const fn = flip(
    xs => bimap(
        fanout(constant(xs), identity),
        currVal =>  xs.concat(currVal)
    )
);

 

---

 

Full code:

复制代码
const {fork, loadTextFile} = require('./funs.js');
const {Async, bimap, fanout, constant, flip, Pair, identity, mapReduce} = require('crocks');

const data = [
    'text.txt',
    'text.big.txt',
    'notfound.txt',
];

const fn = flip(
    xs => bimap(
        e => Pair(xs, e),
        fanout(constant(xs), identity),
        currVal =>  xs.concat(currVal)
    )
);
/*
const concatSpecial = (acc, currAsync) =>
    acc.chain(
        xs => currAsync.bimap(
            e => Pair(xs, e),
            currVal =>  xs.concat(currVal))
    );*/
const concatSpecial = (acc, currAsync) =>
    acc.chain(
        fn(currAsync)
    );
// Async (Pair [String] Error) [String]
const flow = mapReduce(
    loadTextFile,
    concatSpecial,
    Async.Resolved([])
);

flow(data).fork(
    e => console.log(e.snd(), e.fst()), // Pair(success, error)
    r => console.log(r), // Just success result
)
复制代码

 

posted @   Zhentiw  阅读(186)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2017-03-10 [Angular] Export directive functionalities by using 'exportAs'
2017-03-10 [Postgres] Group and Aggregate Data in Postgres
2017-03-10 [Ramda] Create a Query String from an Object using Ramda's toPairs function
2017-03-10 [Ramda] Filter an Array Based on Multiple Predicates with Ramda's allPass Function
2016-03-10 [RxJS] Reactive Programming - Using cached network data with RxJS -- withLatestFrom()
点击右上角即可分享
微信分享提示