Public
Edited
Oct 3, 2023
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
const list = [
'item-0',
'item-1',
'item-2',
'item-3',
'item-4',
'item-5',
'item-6',
'item-7'
]
const f = agen.batch(3);
const results = [];
for await (let b of f(list)) {
results.push(b);
yield results;
await Promises.delay(500);
}
}
Insert cell
Insert cell
Insert cell
{
// This example shows how following operations are combined in one step:
// 1) filtering values
// 2) execution of specific operations before and after each value
// 3) transforming returned values
const result = [];
// compose method allows to combine multiple operations in one:
const f = agen.compose(
agen.filter((v, i) => (i % 2 == 0)), // 1) - Filter only even values
agen.each( // 2) - Prints "xml" tags
(v, i) => result.push(`<${v}>`),
(v, i) => result.push(`</${v}>`),
),
agen.map((v, i) => v.toUpperCase()), // 3) - Transforms characters to upper case
);

// Define the original sequence to transform:
const list = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
// Apply filters:
for await (const v of f(list)) {
result.push(` - ${v}`);
yield result.join('\n');
await Promises.delay(300);
}
yield result.join('\n');
}
Insert cell
Insert cell
Insert cell
{
// Prepare the mapping function
const results = [];
const f = agen.each(
(v, i) => results.push(`- before [${i}:${v}]`),
(v, i) => results.push(`- after [${i}:${v}]`)
);

// Iterate over a list of values
const list = [ 'a', 'b', 'c' ]
for await (let value of f(list)) {
results.push(` - ${value}`);
yield results.join('\n');
await Promises.delay(300);
}
yield results.join('\n');
}
Insert cell
Insert cell
Insert cell
{
const list = [
'First Message',
'Hello world',
'Second Message',
'Hello John Smith'
]
const f = agen.filter((v, i) => v.indexOf('Hello') >= 0)
const result = [];
for await (let item of f(list)) {
result.push(`- ${item}`);
}
yield result;
}

Insert cell
Insert cell
{
const list = [
'item-0',
'item-1',
'item-2',
'item-3',
'item-4',
'item-5',
'item-6',
'item-7'
]
const f = agen.filter((v, i) => i % 2 === 0);
const result = [];
for await (let item of f(list)) {
result.push(`- ${item}`);
}
yield result;
}

Insert cell
Insert cell
Insert cell
{
const list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k'];
let result;
const f = agen.fin((error, count) => {
result = `count: ${count}; error: ${error}`;
});
for await (let v of f(list)) {}
yield result;
}
Insert cell
Insert cell
{
const list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k'];
let result;
const f = agen.fin((error, count) => {
result = `count: ${count}; error: ${error}`;
});
let idx = 0;
for await (let v of f(list)) {
if (++idx === 3) break;
}
yield result;
}

Insert cell
Insert cell
{
// Output:
// - a
// - b
// - c
// - d
// - ...
const list = [
'a',
['b', [['c', 'd'], [['e']], 'f'], 'g'],
['h', [['i'], 'j'], 'k']
];
const f = agen.flatten();
const result = [];
for await (let v of f(list)) {
result.push(v);
yield result;
await Promises.delay(300);
}
yield result;
}
Insert cell
Insert cell
Insert cell
{

// Original data to iterate over:
const list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k'];
// Interrupt iterations before the fourth element (idx === 3):
const f = agen.interrupt((value, idx) => idx === 3);
const result = [];
for await (let b of f(list)) {
result.push(b);
yield result;
await Promises.delay(300);
}
yield result;
}
Insert cell
Insert cell
{
// Interrupts iterations just after the fourth element (idx === 3).
// Note that the first callback (`before` function) is `null`:
const f = agen.interrupt(null, (value, idx) => idx === 3);
const list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k'];
const result = [];
for await (let b of f(list)) {
result.push(b);
yield result;
await Promises.delay(300);
}
yield result;
}
Insert cell
Insert cell
Insert cell
{
const result = [];

const f = agen.iterator((o) => {
(async () => {
await o.next("Hello");
result.push(" > sent after the 'Hello' message consumed");
await o.next("World");
result.push(" > sent after the 'World' message consumed");
await o.next("!");
result.push(" > sent after the '!' message consumed");
o.complete();
})();
return () => result.push("---------------", "End of all iterations!");
});

for await (let item of f()) {
result.push(`* ${item}`);
yield result.join("\n");
await Promises.delay(500);
}
yield result.join("\n");
}
Insert cell
Insert cell
Insert cell
{
// Creates an async iterator.
// The difference with the agen.terator is that
// the returned value is a ready to use iterator
// (and not a function to invoke).
const result = [];
const iterator = agen.iterate((o) => {
(async () => {
await o.next("Hello");
result.push(" > sent after the 'Hello' message consumed");
await o.next("World");
result.push(" > sent after the 'World' message consumed");
await o.next("!");
result.push(" > sent after the '!' message consumed");
o.complete();
})();
return () => result.push(
"---------------",
"End of all iterations!"
)
});

for await (let item of iterator) {
result.push(`* ${item}`);
yield result.join('\n');
await Promises.delay(500);
}
yield result.join('\n');
}
Insert cell
Insert cell
Insert cell
{
// Prepare the mapping function
const f = agen.map((v, i) => v.toUpperCase());

// List of values to transform
const list = [ 'a', 'b', 'c' ]
// Iterate over a list of transformed values
const result = [];
for await (let item of f(list)) {
result.push(item);
}
yield result;
}
Insert cell
Insert cell
Insert cell
Generators.observe(cb => {
let messages = [];
const notify = (value) => {
messages.push(value);
cb(messages.join('\n'));
messages = [...messages];
}
// Generates new values:
const f = agen.multiplexer((o) => {
(async () => {
await o.next('Hello');
await o.next('World');
await o.next('!');
await o.complete();
})();
return () => notify('Done')
});

// Consume values in three different "threads".

// First consumer (the slowest one):
(async () => {
for await (let value of f()) {
notify('* FIRST:' + value);
await new Promise(r => setTimeout(r, 1000));
}
})();

// Second consumer:
(async () => {
for await (let value of f()) {
notify('* SECOND:' + value);
await new Promise(r => setTimeout(r, 500));
}
})();

// Third consumer:
(async () => {
for await (let value of f()) {
notify('* THIRD:' + value);
}
})();

// Output:
// * FIRST: Hello
// * SECOND: Hello
// * THIRD: Hello
// * FIRST: World
// * SECOND: World
// * THIRD: World
// * FIRST: !
// * SECOND: !
// * THIRD: !
// Done
})
Insert cell
Insert cell
Insert cell
{
const f = agen.range(1, 3);
const list = [ 'a', 'b', 'c', 'd', 'e', 'f' ]
const result = [];
for await (let item of f(list)) {
result.push(item);
}
yield result;
// Expected output:
// - b
// - c
// - d
}
Insert cell
Insert cell
Insert cell
{
let prevYear;
// Split cars by year
const f = agen.series((v, i) => {
const split = prevYear && (prevYear !== v.year);
prevYear = v.year;
return split;
})

// See https://en.wikipedia.org/wiki/Car_of_the_Year
const cars = [
{ year: 2005, name : "Audi A6" },
{ year: 2006, name : "BMW 3 Series" },
{ year: 2006, name : "Porsche Cayman S" },
{ year: 2007, name : "Lexus LS 460" },
{ year: 2007, name : "Audi RS4" },
{ year: 2007, name : "Mercedes-Benz E320 Bluetec" },
];
const result = [];
for await (let serie of f(cars)) {
result.push('----------------------');
for await (let car of serie) {
result.push(`- ${car.year} ${car.name}`);
yield result.join('\n');
await Promises.delay(500);
}
}
yield result.join('\n');
// Output:
// ----------------------
// - 2005 Audi A6
// ----------------------
// - 2006 BMW 3 Series
// - 2006 Porsche Cayman S
// - 2006 Honda Civic Hybrid
// ----------------------
// - 2007 Lexus LS 460
// - 2007 Audi RS4
// - 2007 Mercedes-Benz E320 Bluetec
}

Insert cell
Insert cell
agen = import("@agen/utils@0.8")
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more