This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
;; From SICP 2.3.4 | |
;; Leaf defintions | |
(define (make-leaf symbol weight) | |
(list 'leaf symbol weight)) | |
(define (leaf? object) | |
(eq? (car object) 'leaf)) | |
(define (leaf-symbol x) (cadr x)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function simpleGenerator() { | |
yield 1; | |
yield 2; | |
} | |
foreach(simpleGenerator() as $val) { | |
echo "$val\n"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// User Generator | |
function getGuideAuthors(int $guideid): Iterator { | |
// Guide::getUserids could be a generator too! | |
foreach (Guide::getUserids($guideid) as $userid) { | |
// Yield full ORM object. | |
yield User::get($userid); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function range(int $start, int $end, int step = 1): Iterator { | |
while ($start < $end) { | |
yield $start; | |
$start += $step; | |
} | |
} | |
function infinite() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function lazy_map(callable $f, Iterator $list): Iterator { | |
foreach ($list as $item) | |
yield $f($item); | |
} | |
function lazy_filter(callable $f, Iterator $list): Iterator { | |
foreach ($list as $item) | |
if ($f($item)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types = 1); | |
/** | |
* Like array_map() but over an iterable, and it returns a new iterable with | |
* mapping instead of a mapped array. The callable should take two arguments: | |
* a value to map and its key in the stream. | |
*/ | |
function iterator_map(callable $cb, iterable $itr): iterable { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
describe(strict_types = 1); | |
/** | |
* Like array_filter() but over an iterable, and it returns a new iterable with | |
* filtering instead of a filtered array. The callable, if non-null, should | |
* take arguments like iterator_map(). When the callable is null, null values | |
* will be filtered out (NOT falsey values, just x === null). | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types = 1); | |
/** | |
* Like array_reduce() but over an iterable, and it returns a single value as | |
* the result of calling $cb over the contents of iterable $itr. | |
* | |
* If $initial is not null, $initial is set to the value of the first element | |
* of the iterable, and $cb is called with the first element as the carry value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
describe(strict_types = 1); | |
/** | |
* Example code for consuming data from a Store API (called Storeify) and chaining generators | |
* together to filter, map, and eventually reduce all Order data into a | |
* daily revenue total. | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fetch = require('node-fetch'); | |
const cheerio = require('cheerio'); | |
function getPlayerStats(battletag, region, platform) { | |
battletag = battletag.replace('#', '-'); | |
const url = `https://playoverwatch.com/${region}/career/${platform}/${battletag}`; | |
return fetch(url).then(response => response.text()) | |
.then(doc => { | |
const $ = cheerio.load(doc); | |