Skip to content

Instantly share code, notes, and snippets.

@ImaginaryDevelopment
ImaginaryDevelopment / change.js
Created August 3, 2022 14:01
FreeCodeCamp change calc
let getCidTotal = (cid) =>
cid.map(x => x[1]).reduce((x,y) => x + y);
function payout(owed,dv,v){
// if we have less than a d, or we owe less than a d skip
if(dv === 0.01)
console.log(JSON.stringify({owed,dv,v}));
if(v < dv || owed < dv) return;
var i = 0;
// let devroot = Environment.ExpandEnvironmentVariables("%devroot%")
let debug = true
module Option =
let ofNullOrEmpty =
function
| null -> None
| "" -> None
| x -> Some x
@ImaginaryDevelopment
ImaginaryDevelopment / fib.fs
Created June 22, 2022 16:07
a Fibonacci with unfold up to 4mil for euler
let fib x =
(0,0)
|> Seq.unfold(fun (a,b) ->
match a,b with
| 0, 0 -> Some(1, (0, 1))
| _ ->
let c = a + b
Some(c, (b,c))
)
|> Seq.indexed
// https://www.reddit.com/r/ProgrammerHumor/comments/umbmlt/this_is_hurting_my_ego/
[
"8809", 6
"7111", 0
"2172", 0
"6666", 4
"1111", 0
"3213", 0
"7662", 2
@ImaginaryDevelopment
ImaginaryDevelopment / MadProps.fs
Created April 21, 2022 17:14
Using clause does not dispose in this case
type Foo () =
do
printfn "I'm constructed"
member x.Value
with get() = 0
and set (v:int) = invalidOp "Hello world!"
interface IDisposable with
member x.Dispose() =
printfn "IDisposed!"
@ImaginaryDevelopment
ImaginaryDevelopment / Sample1.fs
Created December 30, 2021 22:02
CrabHelper Guessing game
let inline tryParse f x =
match f x with
| true, v -> Some v
| _ -> None
// this version does not compile:
//let (|Parse|_|) (str: string) : int option = tryParse Int32.TryParse str
let inline tryParseInt (x:string) = x |> tryParse Int32.TryParse
// from https://gist.github.com/ImaginaryDevelopment/952b3a9afc4f2fa3c4631d43f760748a
module GistTemplate.BReusable
open System
open System.Collections.Generic
open System.Diagnostics
let inline guardAgainstNull msg (o:obj) =
if isNull o then
nullArg msg
let (|GuardNull|) msg (x:'T) =
@ImaginaryDevelopment
ImaginaryDevelopment / ReflectionExploration.fsx
Last active December 16, 2021 18:27
F# object reflection exploration
module ResizeArray =
let init i f =
Seq.init i f
|> ResizeArray
let (|IsSeq|_|) (t:Type) =
t.GetInterfaces() |> Seq.choose(fun t ->
if t.IsGenericType && t.GetGenericTypeDefinition() = typedefof<seq<_>> then Some("iseq",t.GenericTypeArguments) else None
)
|> Seq.tryHead
@ImaginaryDevelopment
ImaginaryDevelopment / LogBuilder.fs
Created November 18, 2021 16:11
LogBuilder Computation Expression
let logStep action =
let res = action <| printf "%s... "
printfn "Complete."
res
type LoggingBuilder() =
member this.Bind(x, f) =
logStep x |> f
member this.Zero() = ignore
member this.Return(x) = x
@ImaginaryDevelopment
ImaginaryDevelopment / ohno.ts
Created October 21, 2021 16:05
Code cleaning comparison
onNotifyAcknowledgedPositions() {
const companyState = this.memberState;
if (companyState &&
companyState['BUY'] && companyState['SELL'] &&
(isFalsy(companyState['BUY'].addedRequests) || companyState['BUY'].addedRequests.length === 0) &&
(isFalsy(companyState['BUY'].updatedRequests) || companyState['BUY'].updatedRequests.length === 0) &&
(isFalsy(companyState['BUY'].deletedRequests) || companyState['BUY'].deletedRequests.length === 0) &&
(isFalsy(companyState['BUY'].addedComments) || companyState['BUY'].addedComments.length === 0) &&
(isFalsy(companyState['BUY'].deletedComments) || companyState['BUY'].deletedComments.length === 0) &&