This file contains hidden or 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
delegate dynamic D(D d); | |
static async Task Main() | |
{ | |
D omega = f => f(f); | |
var h = (D g) => (dynamic x) => g(g)(x); | |
var Y = (dynamic f) => omega(g => f(h(g))); | |
var fact = (dynamic f) => (dynamic n) => n <= 1 ? 1 : n * f(n - 1); |
This file contains hidden or 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; | |
using System.Linq; | |
static class Program | |
{ | |
public static IEnumerable<TAcc> ScanBy<T, TKey, TAcc>(this IEnumerable<T> source, Func<T, TKey> keyFunc, Func<TKey, T, TAcc> seedFunc, Func<TAcc, T, TAcc> aggregateFunc) | |
{ | |
TAcc acc = default!; | |
TKey key = default!; |
This file contains hidden or 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.Linq; | |
public static class Index | |
{ | |
public static Index<TKey, TValue> Create<TKey, TValue>() | |
where TKey : notnull | |
{ | |
var keyMap = new Dictionary<TKey, HashSet<TValue>>(); | |
Action<TKey, TValue> add = (key, value) => |
This file contains hidden or 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 Nessos.Effects; | |
using Nessos.Effects.Handlers; | |
public class ConsolePrintEffect : Effect | |
{ | |
public string? Message { init; get; } | |
} |
This file contains hidden or 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
let dropWhile : ('a -> bool) -> list<'a> -> list<'a> = fun pred xs -> | |
let (_, ys) = List.foldBack (fun x (xs, ys) -> let xs' = x :: xs in if pred x then xs', ys else xs', xs') xs ([], []) | |
ys | |
// example 1 | |
[1..10] |> dropWhile (fun x -> x <= 5) // [6..10] | |
// example 2 | |
[1..10] |> dropWhile (fun x -> false) // [1..10] |
This file contains hidden or 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
type Append<T, TArray> = TArray extends [infer TCurrent, ...infer Rest] ? | |
TCurrent extends any[] ? [[T, ...TCurrent], ...Append<T, Rest>] : never | |
: [] | |
type PowerSet<TArray extends any[]> = | |
TArray extends [infer TCurrent, ...infer Rest] ? [...Append<TCurrent, PowerSet<Rest>>, ...PowerSet<Rest>] : [[]] | |
type Test = PowerSet<[1, 2, 3]> // [[1, 2, 3], [1, 2], [1, 3], [1], [2, 3], [2], [3], []] |
This file contains hidden or 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
let fact = `(f => n => (n === 0) ? 1 : n * f(n - 1))` | |
function Y(g : string) : (any) => any { | |
let f = `(f => ${g}(x => eval(f)(f)(x)))` | |
return eval(f)(f) | |
} | |
console.log(Y(fact)(10)) |
This file contains hidden or 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
Expression<Func<Expression, Func<int, int>>> f = | |
self => x => ((Expression<Func<Expression, Func<int, int>>>)self).Compile()(self)(x + 1); | |
var x = f.Compile()(f)(0); |
This file contains hidden or 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
static async IAsyncEnumerable<T> UnFold<T, TAcc>(Func<TAcc, Task<(bool Next, IAsyncEnumerable<T> Values, TAcc Acc)>> f, TAcc seed) | |
{ | |
var acc = seed; | |
var result = default((bool Next, IAsyncEnumerable<T> Values, TAcc Acc)); | |
do | |
{ | |
result = await f(acc); | |
await foreach (var value in result.Values) | |
yield return value; |
This file contains hidden or 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
// https://blog.neteril.org/blog/2017/04/26/maybe-computation-expression-csharp/ | |
[AsyncMethodBuilder(typeof(MaybeAsyncMethodBuilder<>))] | |
interface Option<T> { } | |
// Could use the closed type hierarchy Roslyn feature | |
// to be an approximation of a discriminated union | |
// https://github.com/dotnet/csharplang/issues/485 | |
sealed class None<T> : Option<T> { public static readonly None<T> Value = new None<T>(); } | |
sealed class Some<T> : Option<T> | |
{ |
NewerOlder