Skip to content

Instantly share code, notes, and snippets.

View bmitc's full-sized avatar
💭
BEAMing, F#ing, and Racketeering

bmitc

💭
BEAMing, F#ing, and Racketeering
View GitHub Profile
@bmitc
bmitc / dual.wake
Last active August 16, 2023 07:00
Implementation of dual numbers and automatic differentiation in Wake
package dual
# This is an implementation of automatic differentiation in the Wake programming language
# using dual numbers. A dual number data type is implemented and then arithmetic is implemented
# for dual numbers. Once that is done, a few trigonometric functions are implemented. From there,
# functions can be freely defined by compositions of these operators and functions and then either
# evaluated or automatically differentiated.
#
# The automatic-ness of the differentiation comes from the dual numbers. It is a fact that for a
# dual number, a + b*e, where e is defined by being an element such that e^2 = 0, then
@bmitc
bmitc / IEx and Mix on Windows.md
Last active January 6, 2023 07:14
Configure Elixir's IEx and Mix on Windows

Introduction

Elixir's IEx and Mix tooling, on Windows, comes with some batch scripts (.bat) and PowerShell scripts (.ps1) that handle these tooling applications. For example, you can run

PS > (Get-Command mix.ps1).Path
C:\Program Files (x86)\Elixir\bin\mix.ps1

to see where these scripts are located, if they are in your path already. The Elixir Winows installer does put these in your path if you select it (I think there's a selection at least, from memory).

So out of the gate, you need to run iex.bat and mix.bat to run these commands for Elixir, which can be problematic. See the steps below to get rid of this requirement.

@bmitc
bmitc / GLFWWindowTest.fs
Last active July 11, 2024 02:54
GLFW window with SkiaSharp rendering in F#
(*
The following creates a GLFW window and then draws to the window's OpenGL context using SkiaSharp,
which are .NET bindings for Skia. The bindings shown that look like `glfw<name>` are F# bindings to
GLFW and directly match the GLFW function names. So this example could even be used as a blueprint
for using GLFW and Skia together in other languages.
*)
open System
open SkiaSharp
@bmitc
bmitc / unionAsString.fsx
Created June 19, 2021 05:13
Convert discriminated union values to and from strings
open Microsoft.FSharp.Reflection
let toString (x: 'T) =
match FSharpValue.GetUnionFields(x, typeof<'T>) with
| case, _ -> case.Name
let fromString<'T> (s: string) =
match FSharpType.GetUnionCases typeof<'T> |> Array.filter (fun case -> case.Name = s) with
| [|case|] -> Some(FSharpValue.MakeUnion(case, [||]) :?> 'T)
| _ -> None