Skip to content

Instantly share code, notes, and snippets.

@mariusGundersen
mariusGundersen / Example1.cs
Last active October 21, 2018 10:55
duck-type-extensions-example1
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
// Example 1: await strings
public static TaskAwaiter<string> GetAwaiter(this string text)
=> Task.FromResult(text).GetAwaiter();
// Now it doesn't complain when we await the string
await "Hello world";
@mariusGundersen
mariusGundersen / example.js
Created January 29, 2018 19:36
(Async) Iterator pipe
const result = pipe([1, 2, 3])
.pipe(through)
.pipe(i => some('steps', i))
.pipe(toAsync)
.pipe(delayed(100))
.then(concat)
function* through(iterator){
for(const item of iterator){
yield* new Array(item).fill(item);
@mariusGundersen
mariusGundersen / example.js
Created December 27, 2017 13:59
Async iterator stream
async function run(){
console.log('start');
const result = await source()
::map(t => t*2)
::take(30)
::buffer(10)
::map(t => Promise.delay(Math.random()*10, t))
::filter(t => t%4 == 0)
::reduce((a, b) => a+b, 0);
console.log('done', result);
@mariusGundersen
mariusGundersen / demo.js
Created December 22, 2017 15:40
async-iterator-stream
async function* source(){
let n = 1;
while(true){
yield n++;
await new Promise(res => setTimeout(res, 100));
}
}
async function* map(func){
for await(const chunk of this){
@mariusGundersen
mariusGundersen / 0-generator.js
Created August 21, 2017 14:52
for-of-with proposal
// Given the following generator that will walk a tree structure depth-first
// for each subtree it will go deeper unless the consumer returns `false`
function* depthFirst(tree, prefix=''){
for(const [key, value] of Object.entries(tree)){
const path = `${prefix}/${key}`;
if(typeof value === 'string'){
yield {path, value}
}else{
if(false !== (yield {path})){
@mariusGundersen
mariusGundersen / example.js
Created December 30, 2015 02:09
DSL with operator overloading in ES2015
class Vector{
constructor(x, y){
this.x = x;
this.y = y;
}
[Symbol['+']](that){
return new Vector(this.x + that.x, this.y + that.y);
}
[Symbol['-']](that){
return new Vector(this.x - that.x, this.y - that.y);
@mariusGundersen
mariusGundersen / AsyncQueue.js
Last active November 9, 2015 21:39
AsyncQueue example
export default class AsyncQueue {
constructor() {
this.items = [];
this.receivers = [];
}
push(value) {
if(this.receivers.length == 0){
this.items.push(value);
}else{
#!/usr/bin/env bash
# MIT © Sindre Sorhus - sindresorhus.com
# git hook to run a command after `git pull` or `git checkout` if a specified file was changed
# Run `chmod +x post-checkout` to make it executable then put it into `.git/hooks/`.
changed_files="$(git diff-tree -r --name-only --no-commit-id HEAD@{1} HEAD)"
check_run() {
echo "$changed_files" | grep --quiet "$1" && echo " * changes detected in $1" && echo " * running $2" && eval "$2"
function toHex(rgb){
var a = /rgb\((\d+),\s*(\d+),\s*(\d+)\)/.exec(rgb).reverse();
a.pop();
return '#'+a.map(a => parseInt(a, 10).toString(16)).join('');
}
var variables = {
'#747474': '#FF0000',
'#f8b912': '#00FFBB'
};
function Lazy(list, ...steps){
this.list = list;
this.steps = steps;
}
Lazy.prototype.map = function(f){
return new Lazy(this.list, ...this.steps, iteration
=> ( for (entry of iteration) f(entry)));
}