Skip to content

Instantly share code, notes, and snippets.

@yeswell
Created April 13, 2023 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yeswell/228a11017f5f363688430bd7153dab06 to your computer and use it in GitHub Desktop.
Save yeswell/228a11017f5f363688430bd7153dab06 to your computer and use it in GitHub Desktop.
import { interval, Observable } from 'rxjs';
import { map, withLatestFrom } from 'rxjs/operators';
function addOne(n: number): number {
return (n + 1);
}
function toOdd(n: number): number {
return ((n * 2) + 1);
}
function toNatural(n: number): number {
return ((n - 1) / 2);
}
function toSquare(n: number): number {
return (n ** 2);
}
function generateNaturalNumbers(): Observable <number> {
return interval(500);
}
const naturalNumbers = generateNaturalNumbers();
const smallCathetuses = naturalNumbers.pipe(
map(addOne),
map(toOdd),
);
const bigCathetuses = smallCathetuses.pipe(
map(toSquare),
map(toNatural),
);
const hypotenuses = bigCathetuses.pipe(
map(addOne),
);
const pythagoreanTriples = smallCathetuses.pipe(
withLatestFrom(bigCathetuses, hypotenuses),
);
pythagoreanTriples.subscribe((value) => {
console.log(value);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment