Skip to content

Instantly share code, notes, and snippets.

@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 August 10, 2022 19:24
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
@mrange
mrange / 0_README.md
Last active April 22, 2022 13:36
Inferring C# from JSON with T4

C# from JSON in T4

A microblog with minimal prose and maximum amount of code

Inspired by the release notes in VS about "copy JSON to C#" I tinkered with making a T4 template to do so.

The T4 template uses some new C# language feautures + System.Text.Json so it won't work with T4 bundled with Visual Studio (still on .NET framework).

However it works fine from command line but you need to use a build of dotnet that support raw string literals. The version on my machine is 6.0.300-preview.22204.3

@mrange
mrange / infer.fs
Last active April 9, 2022 08:42
Inferring C# from JSON
module InferSchema =
open System
open System.Text.Json
// The Model for an inferred schema
type InferredSchema =
| Undefined
| Boolean
| Integer
| Float
@mrange
mrange / func.glsl
Last active March 21, 2022 07:29
GLSL Useful functions
void test(float a) {
// sin produces a value between -1.0 and 1.0 that is periodic depending on the input
float u = sin(a);
// Often it is useful to make the value from sin to 0.0 and 1.0
// When u is -1.0 v will be 0.0 and when us is 1.0 v will be 1.0
float v = 0.5+0.5*u;
// mix is a useful function that allows you interpolate from the first input to the second depending on the third
// The third should be in the range 0.0 and 1.0
@mrange
mrange / Program.cs
Created March 15, 2022 19:48
SQL Info Message Capture
using Microsoft.Data.SqlClient;
using System.Collections.Concurrent;
using var conn = new SqlConnection("");
using var capturer = conn.CaptureSqlInfoMessages();
Console.WriteLine("Hello, World!");
static class Extensions
{
@mrange
mrange / Program.cs
Last active February 23, 2022 09:09
C# Nullable
class NullableWoes
{
public void F(int x) => Console.WriteLine(x);
// Works, overloads F
public void F(int? x) => Console.WriteLine(x);
// Works, overloads F
public void F(string x) => Console.WriteLine(x);
// Does not work
public void F(string? x) => Console.WriteLine(x);
@mrange
mrange / Program.fs
Last active February 5, 2022 07:55
HTML Generator F#6
open System.Text
type HtmlGenerator = StringBuilder -> int -> unit
module Html =
module Details =
let inline indent i (sb : StringBuilder) =
sb.AppendLine () |> ignore
sb.Append (' ', i)
let inline append (t : string) (sb : StringBuilder) =
sb.Append t
@mrange
mrange / App.fsproj
Last active January 19, 2022 12:50
F# Fractals
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<Compile Include="Program.fs" />