Skip to content

Instantly share code, notes, and snippets.

type KeyedLookupT<TKey, TValue> = {
get: (key: TKey) => TValue | undefined
set: (key: TKey, value: TValue) => KeyedLookupT<TKey, TValue>
entries: () => TValue[]
}
const KeyedLookup = {
create: <TKey extends object | string, TValue>() : KeyedLookupT<TKey, TValue> => {
type MapValueT = { type: 'TValue', value: TValue}
type InnerMapT<TKey, TValue> = {
@andybrackley
andybrackley / TestObservables.cs
Created July 8, 2022 15:51
Observable Scan Implementation.
public static IObservable<T> MyScan<T>(this IObservable<T> stream, T init, Func<(T acc, T curr), T> resultCalc)
{
return Observable.Create<T>(obs =>
{
var result = init;
var dispose =
stream.Subscribe(
x => {
result = resultCalc((result, x));
obs.OnNext(result);
// https://leetcode.com/problems/add-two-numbers/submissions/
public class Solution {
public ListNode AddTwoNumbers(ListNode l1, ListNode l2) {
ListNode head = null;
ListNode current = null;
int carry = 0;
while(l1 != null || l2 != null) {
var v1 = (l1?.val) ?? 0;
// https://leetcode.com/problems/two-sum/submissions/
public class Solution {
public int[] TwoSum(int[] nums, int target) {
var dict = new Dictionary<int, (int num, int index)> ();
for(int i = 0; i < nums.Length; ++i) {
var current = nums[i];
var required = target - current;
if(dict.TryGetValue(required, out var avail)) {
@andybrackley
andybrackley / fillContentSpace.html
Created February 1, 2020 21:29
Web Page with Header and Footer. The Main Content fills the remaining space
<!DOCTYPE html>
<head>
<!-- Layouts -->
<style type="text/css">
html, body {
margin: 0;
height: 100%;
}
body {
@andybrackley
andybrackley / index.html
Created January 23, 2020 18:54
Web Page with fixed header and footer. Title is vertically centered with navBar aligned to the right-hand margin
<!DOCTYPE html>
<head>
<!-- Layouts -->
<style type="text/css">
.fixed-topleft {
position: fixed;
top: 0px;
left: 0px;
width: 100%;
}
@andybrackley
andybrackley / FizzBuzz.fs
Created January 18, 2020 13:27
An alternative approach to the FizzBuzz problem that doesn't use modules
module Program =
type Fizz = Fizz
type Buzz = Buzz
let fizzBuzzLines =
let fizzPattern = Seq.initInfinite (fun _ -> [ None; None; Some Fizz ] ) |> Seq.concat
let buzzPattern = Seq.initInfinite (fun _ -> [ None; None; None; None; Some Buzz ] ) |> Seq.concat
let fizzBuzzPattern =
buzzPattern
|> Seq.zip fizzPattern
@andybrackley
andybrackley / race_paces.elm
Created May 19, 2016 19:26
Elm version of my F# race paces calculations
import Html exposing (text)
import Time exposing (Time)
type UnitType = Metric | Imperial
type Speed = Mph Float | Kph Float
type Pace = MinPerMile Float | MinPerKm Float
type Distance = Miles Float | Kms Float
speedConversionFactor = 0.621371192
module MergeSort =
let rec sort listToSort =
let rec merge lhs rhs =
match (lhs, rhs) with
| [], [] -> []
| l, [] -> l
| [], r -> r
| x::xs, y::ys -> if x < y then x :: (merge xs rhs) else y :: (merge lhs ys)
match listToSort with
@andybrackley
andybrackley / IObservable_SampleSubscribe.fs
Last active December 3, 2015 06:39
Subscribing to an Observable sequence and performing an action ignoring all but latest value
#r @"C:\Users\Andy\Documents\Programming\Projects\F#\ScratchPad\packages\Rx-Core.2.2.5\lib\net45\System.Reactive.Core.dll"
#r @"C:\Users\Andy\Documents\Programming\Projects\F#\ScratchPad\packages\Rx-Interfaces.2.2.5\lib\net45\System.Reactive.Interfaces.dll"
#r @"C:\Users\Andy\Documents\Programming\Projects\F#\ScratchPad\packages\Rx-Linq.2.2.5\lib\net45\System.Reactive.Linq.dll"
#r @"C:\Users\Andy\Documents\Programming\Projects\F#\ScratchPad\packages\FSharp.Control.Reactive.3.2.0\lib\net40\FSharp.Control.Reactive.dll"
open FSharp.Control.Reactive
open System.Reactive.Disposables
open System.Reactive.Linq
open System.Reactive.Subjects