Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / Example5.cs
Created October 21, 2018 12:09
Duct-type extension methods Example 5
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
// Example 5: await tuple of Tasks
// Just some dummy async functions
public static Task<string> GetSomethingAsync(int number) => Task.FromResult($"Something {number}");
public static Task<int> GetAnotherThingAsync(string text) => Task.FromResult(text.Length);
// This extension method lets us await a tuple with two values
@mariusGundersen
mariusGundersen / Example6.cs
Created October 21, 2018 12:18
Duct-typed extension methods Example 6
// Example 6: Add a list
// This is just a dummy method that returns some ints
public static IEnumerable<int> GetAnotherListOfInts()
=> new[] { 10, 11, 12, 13 };
// With this extension method...
public static void Add<T>(this List<T> list, IEnumerable<T> items)
=> list.AddRange(items);
@mariusGundersen
mariusGundersen / Example7.cs
Last active October 21, 2018 12:53
Duct-typed extension methods Example 7
// Example 7: add a dictionary
// These are just some dummy methods for the example to work
public static IDictionary<string, int> GetAfricanCountries() => new Dictionary<string, int>{
{ "Nigeria", 190_886_311 },
{ "Ethiopia", 104_957_438 },
{ "Egypt", 97_553_151 }
};
public static IDictionary<string, int> GetEuropeanCountries() => new Dictionary<string, int>{
{ "Russia", 146_864_513 },
@mariusGundersen
mariusGundersen / Example8.cs
Last active October 21, 2018 12:53
Duct-typed extension methods Example 8
// Example 8: add a new instance
// This is just a dummy class that we can use in the example
public class City
{
public City(string name, int population, string country)
{
Name = name;
Population = population;
Country = country;
@mariusGundersen
mariusGundersen / Example9.cs
Created October 21, 2018 12:49
Duct-typed extension methods Example 9
// Example 9: multi or single dimensional arrays?
// We define this extension method
public static void Add<T>(this List<T> list, params T[] items)
=> list.AddRange(items);
// And then we can make a flattened matrix
var matrix = new List<double>
{
{ 1, 0, 0 },