Skip to content

Instantly share code, notes, and snippets.

@mrange
mrange / README.md
Last active April 16, 2024 14:09
[F#] Implementing Coroutines (async/await) using continuations

[F#] Implementing Coroutines (async/await) using continuations

Coroutines or async/await is not a new concept as it was named in 1958 by Melvin Conway. Coroutines had support in languages such as Simula(1962), Smalltalk(1972) and Modula-2(1978) but I think one can argue that coroutines came into mainstream with C# 5(2012) when Microsoft added async/await to C#.

The problem with subroutines

A thread can only execute a single subroutine at a time as a subroutine starts and then runs to completion. In a server environment subroutines can be problematic as in order to service connections concurrently we would usually would start a thread per connection, this was the idiom a few years ago. A thread depending on the operating system and settings needs about 1 MiB of user stack space meaning 1,024 threads needs 1 GiB of for just user stack space. In addition there's the cost of kernel stack space, book-keeping and scheduling of threads. This drives cost of VM/hardwa

@mrange
mrange / program.cs
Created April 2, 2024 18:09
Sieve Of Eratosthenes (C#)
var primes = SieveOfEratosthenes(100);
var primes_ = string.Join(',', primes);
Console.WriteLine(primes_);
int[] SieveOfEratosthenes(int n)
{
bool[] noPrime = new bool[n];
@mrange
mrange / bombs.fs
Created March 10, 2024 14:32
F# Bombs away
open System
open System.Globalization
open System.Windows
open System.Windows.Media
open System.Windows.Threading
open FSharp.Core.Printf
type CellSymbol =
| Empty
@mrange
mrange / RResult.md
Created March 18, 2017 12:32
Railway Oriented Programming and F# Result

Railway Oriented Programming and F# Result

Full source: https://gist.github.com/mrange/aa9e0898492b6d384dd839bc4a2f96a1

Option<_> is great for ROP (Railway Oriented Programming) but we get no info on what went wrong (the failure value is None which carries no info).

With the introduction F# 4.1 we got Result<_, _> a "smarter" Option<_> as it allows us to pass a failure value.

However, when one inspects the signature of Result.bind one sees a potential issue for ROP:

@mrange
mrange / 0_the_easy_way.cs
Last active January 1, 2024 22:12
Examples IEnumerable implementing a fibonacci series
// Example on the "easy" way to generate the fibonacci sequence
// https://en.wikipedia.org/wiki/Fibonacci_number
// Take the first 20 fibonacci numbers and print them
// the "easy" way.
foreach(var n in Fibonacci().Take(20))
{
Console.WriteLine(n);
}
@mrange
mrange / avx_raymarch.fs
Last active December 1, 2023 20:13
Raymarcher in F#
#nowarn "9"
open System
open System.Diagnostics
open System.Numerics
open SixLabors.ImageSharp
open SixLabors.ImageSharp.Processing
open SixLabors.ImageSharp.PixelFormats
open SixLabors.ImageSharp.Advanced
open System.Threading.Tasks
// ----------------------------------------------------------------------------
open System
open System.Threading
open System.Collections.Generic
// ----------------------------------------------------------------------------
type Continuation<'T> = 'T -> unit
type Coroutine<'T> = Continuation<'T> -> unit
// ----------------------------------------------------------------------------
module Coroutine =
let Bind (t : Coroutine<'T>) (fu : 'T -> Coroutine<'U>) : Coroutine<'U> =
@mrange
mrange / tic80.lua
Last active September 27, 2023 11:14
Rotating cube in TIC-80
BPM =80
function v3(x,y,z)
return {x,y,z}
end
model = {
vertices = {
v3(-1,-1,-1),
@mrange
mrange / tic80.lua
Last active September 23, 2023 12:08
My first TIC-80 program
function clamp(value, min, max)
return math.min(math.max(value, min), max)
end
function mix(a,b,x)
return a+(b-a)*x
end
function pmin(a,b,k)
local h = clamp(0.5+0.5*(b-a)/k, 0.0, 1.0)
@mrange
mrange / 1.md
Last active August 2, 2023 16:43

Thinking about Async/Await

A couple of weeks ago I listened to .Net Rocks! #1433 about Visual Studio 17 with Kathleen Dollard. A very good episode interest but with stuck with me was Kathleen's comment that after all these years she sees very smart people still struggle with async. This is my and others experience as well.

A few years ago I wrote a blog post on why I thought async in C# isn't that great. I took it down a bit later even though it was popular it was also divisive and my understanding of async had grown which made me want to rewrite the whole piece into something better and less divisive.

I used to think that the implicit coupling in async to the SynchronizationContext is a major problem for async but today I think that it's a minor problem for most developers. The reason is that if you are writing ASP.NET you will always have the ASP.NET SynchronizationContext and that gives particular but consistent async behavior. If y