Skip to content

Instantly share code, notes, and snippets.

@richytong
Last active December 3, 2024 23:58
Show Gist options
  • Select an option

  • Save richytong/2d1473f30f810f8de3046c80c0acf2cb to your computer and use it in GitHub Desktop.

Select an option

Save richytong/2d1473f30f810f8de3046c80c0acf2cb to your computer and use it in GitHub Desktop.
// 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