Skip to content

Commit

Permalink
Playground updates
Browse files Browse the repository at this point in the history
  • Loading branch information
voltrevo committed Jun 23, 2023
1 parent 61b1f64 commit 5e4f89b
Show file tree
Hide file tree
Showing 4 changed files with 448 additions and 314 deletions.
23 changes: 23 additions & 0 deletions website/src/playground/files/root/examples/useRange.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// This program solves https://projecteuler.net/problem=2 using the `Range`
// library. Range provides operations on iterables that are very similar to the
// higher order functions on arrays. By using iterables, you can work with
// infinite sequences (eg fibonacci), and the processing is done incrementally
// instead of serializing each step into memory.

import { Range } from "../lib/mod.ts";

export default function main() {
return Range.from(fibonacci())
.while((x) => x < 4_000_000)
.filter((x) => x % 2 === 0)
.sum();
}

function* fibonacci() {
let [fibLast, fib] = [0, 1];

while (true) {
yield fib;
[fibLast, fib] = [fib, fibLast + fib];
}
}
Loading

0 comments on commit 5e4f89b

Please sign in to comment.