Skip to content

Instantly share code, notes, and snippets.

@mrange
mrange / program.cs
Created May 16, 2024 05:39
collection literals C#
// Gives an array of 3 elements
IEnumerable<ulong> x0 = Enumerable.Concat<ulong>([1], Unbounded());
ulong[] y0 = x0.Take(3).ToArray();
// Crashes with out of memory after some time
IEnumerable<ulong> x1 = [1, ..Unbounded()];
ulong[] y1 = x0.Take(3).ToArray();
IEnumerable<ulong> Unbounded()
@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)