Skip to content

Instantly share code, notes, and snippets.

@mrange
mrange / GenAll.tt
Last active June 11, 2022 05:31
GenAll T4
<#@ assembly name ="System.Core" #>
<#@ import namespace ="System.Linq" #>
<#
var model = new []
{
Union(
"BooleanValue"
, Case("False")
@mrange
mrange / Benchmark.fsproj
Last active June 9, 2022 19:28
Testing Fusion vs Fission in F#
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="Program.fs" />
</ItemGroup>
@mrange
mrange / 0_README.md
Last active September 2, 2022 09:56
FsGenAll - Generate all possible values for an F# type

FsGenAll - Generate all possible values for an F# type

writing test code I often use FsCheck. However, sometimes I want to generate each possible value for a test model and check the properties for that.

My typical use case is something like this:

  1. Create an ADT that models what I want to test
  2. Generate all possible values of this test model
  3. Generate test data from the test model
  4. Run the function under test that produces a result
@mrange
mrange / genall.fs
Last active June 6, 2022 05:51
F# Gen All
type 'T PushStream = ('T -> bool) -> bool
// I wrote a blog about PushStream for F# advent 2022
// https://github.com/mrange/PushStream6
// I am appealed to them both because of their simplicity and performance
// When generating values I need a place to my values after creation
// PushStreams turn out to be very convenient
module PushStream =
[<GeneralizableValue>]
let empty<'T> : 'T PushStream = fun r -> true
@mrange
mrange / RecordArray.cs
Last active May 31, 2022 07:32
RecordArray - Reasonable ToString and comparison for sequences in records.
using System.Collections;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Shared
{
partial class RecordArrayConverter<T> : JsonConverter<RecordArray<T>>
{
public RecordArrayConverter() {}
@mrange
mrange / Mandelbrot.fsproj
Created May 15, 2022 07:32
F# Mandelbrot
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="Program.fs" />
</ItemGroup>
@mrange
mrange / 0_README.md
Last active May 15, 2022 13:39
Dynamic JSON in C#

Dynamic JSON in C#

How to run

  1. Install dotnet: https://dotnet.microsoft.com/en-us/download
  2. Create a folder named for example: CsDymamic
  3. Create file in the folder named: CsDyamic.csproj and copy the content of 1_CsDyamic.csproj into that file
  4. Create file in the folder named: Program.cs and copy the content of 2_Program.cs below into that file
  5. Launch the application in Visual Studio or through the command line dotnet run from the folder CsDymamic
@mrange
mrange / 0_README.md
Last active April 24, 2022 17:06
Landing Ship in F#

Landing a rocket

How to run

  1. WPF requires a Windows box
  2. Install dotnet: https://dotnet.microsoft.com/en-us/download
  3. Create a folder named for example: FsLanding
  4. Create file in the folder named: FsLanding.fsproj and copy the content of 1_FsLanding.fsproj below into that file
  5. Create file in the folder named: Program.fs and copy the content of 2_Program.fs below into that file
  6. Launch the application in Visual Studio or through the command line dotnet run from the folder FsLanding
@mrange
mrange / 0_README.md
Last active June 29, 2022 14:29
Simple physics game in F# and WPF

Simple physics game in F# and WPF

This is an example on how to use WPF and F# to do a simple physics game using Verlet Integration.

The program creates a system of particles and constraints. Particles have inertia and is affected by gravity but their motion is also contrained by the constraints.

You control the ship by firing rockets attached to the ship. Use the arrow keys to fire the rockets.

I tried to annotate the source code to help guide a developer familiar with languages like C#. If you have suggestions for how to improve it please leave a comment below.

@mrange
mrange / 0_ReadMe.md
Last active April 21, 2022 14:56
VB + ImageSharp => :(

VB.NET + ImageSharp => :(

When trying out VB + ImageSharp I run into strange compiler errors.

1_ImageSharp.cs works fine in C# but the same program in VB 2_ImageSharp.vb produces the errors:

2>C:\code\2_ImageSharp.vb(7,34): error BC32044: Type argument 'Rgba32' does not inherit from or implement the constraint type '?'.
2>C:\code\2_ImageSharp.vb(7,34): error BC30649: '' is an unsupported type.