Skip to content

Instantly share code, notes, and snippets.

@mariusGundersen
mariusGundersen / Example4.cs
Created October 21, 2018 11:18
Duct-typed extension methods Example 4
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
// Example 4: await IEnumerable<Task<T>>
// Just a dummy async function
public static Task<string> GetSomethingAsync(int number) => Task.FromResult($"Something {number}");
// This extension method allows us to await many tasks
public static TaskAwaiter<T[]> GetAwaiter<T>(this IEnumerable<Task<T>> manyTasks)
@mariusGundersen
mariusGundersen / Example3.cs
Last active October 21, 2018 11:12
Duct-typed extension methods Example 3
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
// Example 3: await Lazy<Task<T>>
// Just a dummy async function
public static Task<string> GetSomethingAsync(int number) => Task.FromResult($"Something {number}");
// We need a lazy variable to work with
var lazySomething = new Lazy<Task<string>>(() => GetSomethingAsync(10));
@mariusGundersen
mariusGundersen / Example2.cs
Created October 21, 2018 10:58
Duct-typed extension methods, example 2
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
// Example 2: await anything
public static TaskAwaiter<T> GetAwaiter<T>(this T nonAwaitable)
=> Task.FromResult(nonAwaitable).GetAwaiter();
// Now we can write code like this
await "Hello world";
await 100;
@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 / weakEventListeners.js
Last active November 18, 2016 04:00
Weak event listeners
var component1 = {
//this method will receive the event and process it
handleEvent: function(event){
//this is only for debugging
console.log("debugging!", event.data);
//the event causes the state of the component to change
@mariusGundersen
mariusGundersen / moduleImports.js
Created June 9, 2014 20:05
Potential module import syntax in ES6
module _ from "underscore";
import _ from "underscore";
import module _ from "underscore";
import {each, map, find} from "underscore";
import "underscore";
import default _ from "underscore";
const _ = System.import("underscore");
import "underscore" as _;
import {_} from "underscore";
import {each as forEach} from "underscore";