[Compose] 19. Leapfrogging types with Traversable
We use the traversable instance on List to reimplement Promise.all() type functionality.
For example we want to conver:
[Task] => Task([])
Conver array of Task, into Task of array value.
To do that we can use traverse.
Noraml Javascript array doesn't have traverse method, so we need to bring immutable-ext.
const fs = require('fs')
const Task = require('data.task')
const futurize = require('futurize').futurize(Task)
const { List } = require('immutable-ext')
const readFile = futurize(fs.readFile)
const files = List(['box.js', 'config.json'])
files.traverse(Task.of, fn => readFile(fn, 'utf-8'))
.fork(console.error, console.log)
Here 'futurize' give us a easy way to wrap a function into a Task. We can do it manually as well:
/*
const readFile = (filename, encode) => new Task((rej, res) => {
return fs.readFile(filename, encode, (err, content) => {
if (err) rej(err);
res(content);
});
});*/
const readFile = futurize(fs.readFile);
[Compose] 19. Leapfrogging types with Traversable的更多相关文章
- [JS Compose] 5. Create types with Semigroups
An introduction to concatting items via the formal Semi-group interface. Semi-groups are simply a ty ...
- C#复习④
C#复习④ 2016年6月16日 12:37 Main Classes and Structs 类和结构体 1.Contents of Classes 字段,常量,方法,构造函数,析构函数: 特性,事 ...
- Java反射机制能够获取的信息,与应用
一.什么是Java反射机制? [1]反射机制是在运行状态中,对于任何一个类,都能够知道这个类的所有属性和方法: [2]对于任意一个对象,都能够调用它的任意一个属性和方法: 像这种动态获取类的信息以及动 ...
- Effective Java 第三版——29. 优先考虑泛型
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
- Python3 与 C# 扩展之~基础拓展
上次知识回顾:https://www.cnblogs.com/dotnetcrazy/p/9278573.html 代码裤子:https://github.com/lotapp/BaseCode ...
- 【大数据】SparkSql学习笔记
第1章 Spark SQL概述 1.1 什么是Spark SQL Spark SQL是Spark用来处理结构化数据的一个模块,它提供了2个编程抽象:DataFrame和 DataSet,并且作为分布式 ...
- 6、Qt Meta Object system 学习
原文地址:http://blog.csdn.net/ilvu999/article/details/8049908 使用 meta object system 继承自 QOject 类定义中添加 Q_ ...
- FastDFS 环境搭建
原文地址:FastDFS 环境搭建 博客地址:http://www.extlight.com 一.前言 最近闲下来,整理了一下笔记,今天就分享一下 FastDFS 环境搭建吧. 二.介绍 2.1 Fa ...
- Spring MVC SessionAttributes ModelAttribute注解
说明 本文主要针对 @SessionAttributes注解 和 @ModelAttribute注解的基础用法进行解析.至于为什么会将这两个注解放在一起,是因为它们之间还是有点影响的. @Sessio ...
随机推荐
- 2017/11/6 Leetcode 日记
2017/11/6 Leetcode 日记 344. Reverse String Write a function that takes a string as input and returns ...
- nyoj 737 石子合并 经典区间 dp
石子合并(一) 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 有N堆石子排成一排,每堆石子有一定的数量.现要将N堆石子并成为一堆.合并的过程只能每次将相邻的两堆 ...
- 【BZOJ 4403】 4403: 序列统计 (卢卡斯定理)
4403: 序列统计 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 653 Solved: 320 Description 给定三个正整数N.L和R, ...
- python3-开发进阶Flask的基础
一.概述 最大的特点:短小精悍.可拓展强的一个Web框架.注意点:上下文管理机制,依赖wsgi:werkzurg 模块 二.前奏学习werkzurg 先来回顾一个知识点:一个类加括号会执行__init ...
- div块元素垂直水平居中方法总结
1.已知块级元素的宽和高,使用绝对定位+外边距设定水平垂直居中. 父元素position:relative,子元素position:absolute;top:50%;left:50%;margin-t ...
- Android为什么需要广播Broadcast
在Android系统中,为什么需要广播机制呢?广播机制,本质上它就是一种组件间的通信方式,如果是两个组件位于不同的进程当中,那么可以用Binder机制来实现,如果两个组件是在同一个进程中,那么它 ...
- hdu 3586 树形dp+二分
题目大意:给定n个敌方据点,1为司令部,其他点各有一条边相连构成一棵 树,每条边都有一个权值cost表示破坏这条边的费用,叶子节点为前线.现要切断前线和司令部的联系,每次切断边的费用不能超过上限lim ...
- [原]Redis使用场景及使用经验
Redis is an open source (BSD licensed), in-memory data structure store! 欢迎转载,转载请注明出处 刚刚结束一个游戏类的活动项目, ...
- ExifInterface针对照片的使用
ExifInterface是保存照片信息的,那么我们在有需要的时候就可以通过这类,来获取照片的一些数码参数来做适当来逻辑处理,比较典型的案例就是android有的机型拍照或者选择照片后,照片可能会被旋 ...
- Delphi XE 4,Rad Studio XE 4 官方下载,更新Update 1(附破解)
http://blog.csdn.net/maxwoods/article/details/8842889 XE4 Update1 下载: http://altd.embarcadero.com/do ...