Skip to content

Instantly share code, notes, and snippets.

View copperwall's full-sized avatar
j chillin

Chris Opperwall copperwall

j chillin
View GitHub Profile
@copperwall
copperwall / huffman.rkt
Created March 21, 2017 04:24
SICP 2.3.4 Huffman Coding Exercises
#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))
@copperwall
copperwall / simple-generator.php
Created May 17, 2017 02:18
Super Simple Generator
<?php
function simpleGenerator() {
yield 1;
yield 2;
}
foreach(simpleGenerator() as $val) {
echo "$val\n";
}
@copperwall
copperwall / lazy-generator.php
Created May 17, 2017 16:55
Lazy Evaluation
<?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);
}
}
@copperwall
copperwall / range-generator.php
Last active May 17, 2017 18:17
Generator-backed range function
<?php
function range(int $start, int $end, int step = 1): Iterator {
while ($start < $end) {
yield $start;
$start += $step;
}
}
function infinite() {
<?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))
@copperwall
copperwall / iterator_map.php
Last active March 18, 2018 17:46
A array_map alternative for iterables
<?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 {
@copperwall
copperwall / iterator_filter.php
Last active March 18, 2018 17:46
A array_filter alternative for iterables
<?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).
*/
@copperwall
copperwall / iterator_reduce.php
Last active July 28, 2020 21:01
A array_reduce alternative for iterables
<?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
<?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.
*/
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);