Skip to content

Instantly share code, notes, and snippets.

View bisen2's full-sized avatar

Ben Isenhart bisen2

  • Garmin
  • Portland, ME
View GitHub Profile
// the function definitions you were given
let comp(f, g)(x) = g(f(x))
let h(f)(x) = f(x), comp(f,f)(x)
// the function definitions you are passing in on the last line
let add1(x) = x + 1
let times3(x) = x * 3
// So the function that is built on the last line is this
let a(x) = h(comp(add1, times3))(x)
// We can define a little helper function to clear up the code
@bisen2
bisen2 / ArithmeticLogicUnit_CompExpr.fsx
Created January 5, 2022 22:21
A computational expression embedding Arithmetic Logic Unit (ALU) programming (see https://adventofcode.com/2021/day/24) in F#
(*
A computational expression embedding Arithmetic Logic Unit (ALU) programming (see https://adventofcode.com/2021/day/24) in F#
This doesn't actually help with solving the Advent of Code challenge, but I thought it would be fun to
play with writing DSLs in computational expressions
Note: the `mod` instruction had to be renamed to `mod'` to avoid conflicting with the reserved keyword `mod`
*)
/// Type representing allowed variable names
@bisen2
bisen2 / Platform.fs
Created June 7, 2022 23:00
F# source and generated Rust for fable-raytracer demo
module Platform
#if FABLE_COMPILER_RUST
open Fable.Core
module Performance =
[<Erase; Emit("std::time::Duration")>]
type Duration =
abstract as_millis: unit -> uint64 // actually u128
@bisen2
bisen2 / FSCompilerInCS.csproj
Created August 3, 2022 19:22
Demo of embedding the F# compiler into a C# application
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
@bisen2
bisen2 / scratch.md
Last active May 22, 2023 17:52
F# discord problem

From conversation on F# discord: https://discord.com/channels/196693847965696000/1109160345316098058

So first off, I converted the input type to list<'a * Set<'b>> instead of list<'a * list<'b>> for now to make some of the implementation clearer, but we can change the types back later if you want.

Okay, so with any complex problem we don't know how to solve, we should break it down into smaller pieces that we can solve. So at the root of this, we need to be able to compare two elements of the input list and if their sets share any elements, return the first part of each element. So if we write a function to do this, it is going to need to return an option<'a * 'a>, since for some inputs we want to return a 'a * 'a, but for other inputs we won't.

let compareElements
    (this: 'a, these: Set<'b>)
    (that: 'a, those: Set<'b>)
    : option<'a * 'a> =
@bisen2
bisen2 / query_perf.md
Last active August 17, 2023 20:39
F#'s query CE vs Seq functions vs Linq functions

Benchmark Results:

Method N Mean Error StdDev Median
QueryCE 1000 45.73 us 0.351 us 0.311 us 45.74 us
QueryEquivalent 1000 45.87 us 0.450 us 0.421 us 45.87 us
Seq 1000 12.17 us 0.090 us 0.080 us 12.18 us
Linq 1000 12.72 us 0.170 us 0.350 us 12.63 us
QueryCE 10000 454.09 us 4.722 us 4.417 us 452.38 us
QueryEquivalent 10000 448.92 us 3.715 us 3.293 us 449.28 us
Seq 10000 115.37 us 0.618 us 0.578 us 115.45 us
open System
type MeasurementObservable<'data> () =
let observers = ResizeArray<IObserver<'data>>()
member _.report (value: 'data) =
observers
|> Seq.iter (fun obs -> obs.OnNext value)
interface IObservable<'data> with