Skip to content

Instantly share code, notes, and snippets.

robkuz / lookup.ts
Last active Nov 19, 2021
Building a typesafe Lookup with Constant Objects
View lookup.ts
//I find typescripts enums a bit lacking.
//This is a way to havevery strongly typed bidirectional lookup table
//first we start with a helper type to reverse a record with unique literal value types as property types
type UniqueReverser<T extends Record<keyof T, PropertyKey>> = unknown extends {
[K in keyof T]-?: T[K] extends Omit<T, K>[Exclude<keyof T, K>] ? unknown : never
}[keyof T] ? never : { [P in keyof T as T[P]]: P }
//this will allow to convert an object in this shape and type
const SOURCE = {

Keybase proof

I hereby claim:

  • I am robkuz on github.
  • I am robkuz ( on keybase.
  • I have a public key ASAMZndFj-jdg8qESBMKO7DN7lBUZd4guVtM26w_4WkAdwo

To claim this, I am signing this object:

robkuz / JsonTest.fs
Last active Oct 11, 2018
Json Testing Lib in F#
View JsonTest.fs
open System
open Newtonsoft.Json.Linq
module JsonTest =
let tokenType2String tt =
match tt with
| JTokenType.None -> "None"
| JTokenType.Object -> "Object"
| JTokenType.Array -> "Array"
| JTokenType.Constructor -> "Constructor"
robkuz / codepuzzle.fs
Created Dec 12, 2017
Some Code puzzle
View codepuzzle.fs
Given the following definitions
|>> : map/fmap
v: NonEmptyList<SomeRecord>
SomeRecord: {foo: Option<_>; bar: Option<_>; baz: Option<_>}
isSomeList: SomeRecord -> List<bool, string>
//string: the name of the field
//bool: if Some then true else false
What does this code do?
robkuz / checkDUOrder.fs
Created Sep 23, 2016
Checking if DU Value constructors are "reflected" in the same order of definition
View checkDUOrder.fs
//I'd like to see if the test below is true on different execution environments and compilers
//so if you could please run this code and report back to me at @kuzrob. Thanks
open Microsoft.FSharp.Reflection
type SomeDU<'a,'b, 'c> =
| One of 'a
| Two of 'a * 'b
| Three of 'a * 'b * 'c
robkuz / nested_tuple.fs
Created Jul 21, 2016
F# inferrer on nested tuples
View nested_tuple.fs
type FooProtocol =
abstract member foo: unit -> string
type Foo = Foo of string with
interface FooProtocol with
member () = "foo"
let bar (foo: FooProtocol, _) = ()
let barnested ((foo: FooProtocol, _), y) = ()
let barnestedrev (y, (foo: FooProtocol, _)) = ()
robkuz /
Last active Jun 16, 2016
Emulating Haskell Type classes in F# and creating a bind (>>=) function

Here is someway to support method overloading with F# and to create a bind function.

first create an inline operator

    let inline (>>=) (f: ^U -> ^T) (t:^T)  = 
        let bind' = (^T : (member bind : (^U -> ^T) -> ^T) (t, f))
robkuz / error-handling-refactoring.purs
Created May 25, 2016
refactoring some convoluted error handling code
View error-handling-refactoring.purs
initial code with some really convoluted error handling.
The problem on this is that it must check for 2 different error cases and also return a different result in those error cases
dot :: Matrix -> Matrix -> Either MatrixError Number
dot (Matrix a sa) (Matrix b sb) = if areSimilarVectors sa sb then Right $ dot' a b else failed sa sb
areSimilarVectors s1 s2 = isVector s1 s2 && isSameSize s1 s2
dot' a b = _dot (join a) (join b)
isVector sa sb = fst sa == 1 && fst sb == 1
isSameSize sa sb = snd sa == snd sb
robkuz /
Last active May 12, 2016
API design howto

I am wrapping some matrix library from javascript into Purescript. And I am thinking which signatures my two creation functions for a matrix should have. Their main difference is that in one case you just give a Vector (JS Array) and in the other case you give a 2D Array. In the first case I can easily construct my Matrix. But in the second case I have a error condition to check as the array elemements of of the 2D array (themselves being arrays) all need to be of the same lenght.

data Matrix = Matrix (Array (Array Number))
data Err = Err -- just some error, really not import what kind

make :: (Array (Array Number)) -> Either Err Matrix

robkuz / Main.js
Last active May 6, 2016
Access Global Object from Purescript
View Main.js
"use strict";
// module Main
exports.instGlobal = function(name){
return function (value) {
return global[name];