Skip to content

Instantly share code, notes, and snippets.

@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.
@mrange
mrange / Program.cs
Created April 17, 2022 17:25
Play mp3 using windows MCI
using System.Runtime.InteropServices;
using System.Text;
static class Program
{
// Using MCI Win32 API
[DllImport("winmm.dll")]
static extern Int32 mciSendString(string command, StringBuilder? buffer, int bufferSize, IntPtr hwndCallback);
// This is needed for MCI from my testing, it does makes sense
@mrange
mrange / 0_README.md
Last active June 3, 2024 08:06
Simple physics in F# and WPF

Simple physics in F# and WPF

This is an example on how to use WPF and F# to do some simple physics 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.

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.

How to run

@mrange
mrange / 0_README.md
Last active April 17, 2022 16:11
Simple 2D graphics in F# and WPF

Simple 2D graphics in F# and WPF

This is an example on how to use WPF and F# to draw simple graphics intended to help devs interested in F# to experiment with a simple yet not trivial app.

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.

How to run

  1. WPF requires a Windows box, maybe I get around to do a Avalonia demo later.
  2. Install dotnet: https://dotnet.microsoft.com/en-us/download