[Compose] 19. Leapfrogging types with Traversable
with 19 Compose types
2023-09-14 09:00:49 时间
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);
相关文章
- Android问题-打开DelphiXE8与DelphiXE10编译空工程提示“[Exec Error] The command exited with code 1.”
- [React Testing] Test Drive Assertions with Dates in React
- [React Testing] Hide console.error Logs when Testing Error Boundaries with jest.spyOn
- [Angular] Make a chatbot with DialogFlow
- [TypeStyle] Style CSS pseudo elements with TypeStyle
- [React Testing] className with Shallow Rendering
- [Tools] Package Your node.js Projects Into a Standalone Applications with pkg
- [React] Avoid Too Many Spinners with React SuspenseList’s tail Prop
- [Javascript] Avoid Nested For Loops with Generators
- [Functional Programming] propSatisfies with implies
- [Angular] Extract Implementation Details of ngrx from an Angular Application with the Facade Pattern
- [TypeScript] Create Explicit and Readable Type Declarations with TypeScript mapped Type Modifiers
- [Node] Define MongoDB Model with Mongoose
- [PReact] Handle Simple Routing with preact-router
- Cause: org.apache.ibatis.type.TypeException: Error setting non null for xxx with JdbcType错误的详细解决方法