Last active
December 3, 2024 23:58
-
-
Save richytong/2d1473f30f810f8de3046c80c0acf2cb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // runnable at https://rubico.land/docs/transform | |
| const rubico = require('rubico') | |
| const Transducer = require('rubico/Transducer') | |
| const { pipe, compose, transform } = rubico | |
| const square = number => number ** 2 | |
| const toString = value => value.toString() | |
| const randomInt = () => Math.ceil(Math.random() * 100) | |
| const streamRandomInts = async function* (n) { | |
| let ct = 0 | |
| while (ct < n) { | |
| ct += 1 | |
| yield randomInt() | |
| } | |
| } | |
| const Stdout = { | |
| concat(...args) { | |
| console.log(...args) | |
| return this | |
| }, | |
| } | |
| transform( | |
| streamRandomInts(10), | |
| compose([ | |
| Transducer.map(square), | |
| Transducer.map(toString), | |
| ]), | |
| Stdout, | |
| ) | |
| // 8281 | |
| // 8836 | |
| // 1156 | |
| // 8649 | |
| // 5625 | |
| // 2500 | |
| // ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment