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
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"; |
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 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); |
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
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); |
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
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){ |
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
// 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})){ |
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
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); |
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
export default class AsyncQueue { | |
constructor() { | |
this.items = []; | |
this.receivers = []; | |
} | |
push(value) { | |
if(this.receivers.length == 0){ | |
this.items.push(value); | |
}else{ |
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
#!/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" |
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
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' | |
}; |
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
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))); | |
} |