Public
Edited
Jan 2, 2024
1 fork
Insert cell
Insert cell
Insert cell
Insert cell
function greet(lastCommaFirst) {
const firstSpaceLast = lastCommaFirst.split(',')
.map(s => s.trim())
.reverse()
.join(" ");

console.log(`Hello ${firstSpaceLast}!`)
}
Insert cell
Insert cell
greet("Pollock, Josh")
Insert cell
greet("Pollock , Josh")
Insert cell
greet("Pollock,Josh")
Insert cell
logTable(10)
Insert cell
Insert cell
// An implementation of the observer pattern
class Observable {
constructor(initialValue) {
this._value = initialValue;
this.observers = [];
}

read() {
return this._value;
}

write(newValue) {
if (newValue !== this._value) {
this._value = newValue;
this.notifyObservers();
}
}

// Method to add an observer
subscribe(observer) {
this.observers.push(observer);
}

// Method to notify all observers about the value change
notifyObservers() {
this.observers.forEach(observer => observer(this._value));
}
}
Insert cell
Insert cell
lastCommaFirst = new Observable("Pollock, Josh")
Insert cell
function parseName(lastCommaFirst) {
return lastCommaFirst.split(',')
.map(s => s.trim())
.reverse()
.join(" ")
}
Insert cell
lastCommaFirst.subscribe((lastCommaFirst) => parseName(lastCommaFirst));
Insert cell
Insert cell
function pipe(x /* Observable<T> */, f /* (x: T) => U */) /* Observable<U> */ {
// initialize a new output observable with the current value of f(x)
const obs = new Observable(f(x.read()));

// whenever the input observable changes...
x.subscribe((value) => {
// update the output observable with the new value of f(x)
obs.write(f(value));
})

return obs;
}
Insert cell
Insert cell
firstSpaceLast = pipe(lastCommaFirst, (lastCommaFirst) => parseName(lastCommaFirst))
Insert cell
// run this cell to see the latest value of firstSpaceLast
firstSpaceLast.read()
Insert cell
greeting = pipe(firstSpaceLast, (firstSpaceLast) => `Hello ${firstSpaceLast}!`)
Insert cell
greeting.subscribe((greeting) => console.log(`greeting: ${greeting}`))
Insert cell
// When you submit a name in this field, it will be written to the `name` Observable.
// If you only change the whitespace, you won't see a new console log!
viewof updatedName = Inputs.text({value: "name, your", placeholder: "Enter your last, first name", submit: true, autocomplete: false})
Insert cell
lastCommaFirst.write(updatedName)
Insert cell
logTable(10)
Insert cell
Insert cell
function greetSplit(lastCommaFirst) {
const first = lastCommaFirst.split(',')[1].trim();
const last = lastCommaFirst.split(',')[0].trim();

console.log(`Hello ${first} ${last}!`)
}
Insert cell
Insert cell
first = pipe(lastCommaFirst, (lastCommaFirst) => lastCommaFirst.split(',')[1].trim())
Insert cell
last = pipe(lastCommaFirst, (lastCommaFirst) => lastCommaFirst.split(',')[0].trim())
Insert cell
Insert cell
function combineLatest(...inputs) {
const observables = inputs.slice(0, -1);
const fn = inputs[inputs.length - 1];

/* NEW! */
// store the latest value for each observable
let latestValues = observables.map(obs => obs.read());

// initialize a new output observable
const obs = new Observable(fn(...latestValues));

// every time an observable changes...
observables.forEach((observable, index) => {
observable.subscribe((value) => {
/* NEW! */
// push the update to latestValues
latestValues[index] = value;

// update the output observable
obs.write(fn(...latestValues));
});
});

return obs;
}
Insert cell
Insert cell
greetingSplit = combineLatest(first, last, (first, last) => `Hello ${first} ${last}!`)
Insert cell
greetingSplit.subscribe((greetingSplit) => console.log(`greetingSplit: ${greetingSplit}`))
Insert cell
// When you submit a name in this field, it will be written to the `name` Observable.
// If you only change the whitespace, you won't see a new console log!
Inputs.bind(Inputs.text({value: "name, your", placeholder: "Enter your last, first name", submit: true, autocomplete: false}), viewof updatedName)
Insert cell
logTable(10)
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more