Skip to content

Instantly share code, notes, and snippets.

View ulve's full-sized avatar

Olov Johansson ulve

  • Future Ordering
  • Umeå
View GitHub Profile
@ulve
ulve / JSVerify.js
Created February 22, 2018 12:00
Propertyp based testing in JavaScript
const jsc = require("jsverify");
const _ = require("lodash");
const add = (a, b) => a + b;
const mult = (a, b) => a * b;
const sub = (a, b) => a - b;
const addIsCommunicative = jsc.checkForall(
jsc.integer,
jsc.integer,
@ulve
ulve / getusers.fs
Created January 21, 2018 14:30
Postgres from f#
// Using the super easy to use https://github.com/Zaid-Ajaj/Npgsql.FSharp
open System
open Npgsql.FSharp
type User = {
UserId : int
UserName: string
}
@ulve
ulve / Usage.elm
Last active December 29, 2017 18:49
MonadWriter in elm
half : Int -> Writer Int String
half n =
tell ("halved " ++ (toString n)) >>= \_ -> return (n // 2)
use =
half 8 >>= half |> runReader
-- => (2, ["halved 4", "halved 8"])
@ulve
ulve / Classic.elm
Created December 29, 2017 11:08
MonadReader in elm
type alias Config =
{ httpEndpoint : String
, portNumber : Int
}
type alias Person =
{ name : String
, address : String
}
@ulve
ulve / SmallestFreeNumber.js
Created November 13, 2017 09:05
Smalles free bucklescript
function minfree(xs) {
var _a = 0;
var _n = List.length(xs);
var _xs = xs;
while(true) {
var xs$1 = _xs;
var n = _n;
var a = _a;
var b = (a + 1 | 0) + (n / 2 | 0) | 0;
var match = List.partition((function(b){
@ulve
ulve / SmallestFreeNumber.re
Last active November 11, 2017 13:10
Find the smallest free number in a list
let rec minfrom = (a, n, xs) => {
let b = a + 1 + n/2;
let (us, vs) = List.partition(fun (x) => x < b, xs);
let m = List.length(us);
if(n == 0)
a
else if(m == b - a)
minfrom(b, n - m, vs)
else
@ulve
ulve / StateMonad.ts
Created October 23, 2017 10:40
StateMonad in TypeScript
class State {
private constructor(protected state) {}
Map = f =>
new State(s => {
var prev = this.state(s);
return { value: f(prev.value), state: prev.state };
});
Join = () =>
@ulve
ulve / monadreader.ts
Last active October 23, 2017 06:37
MonadReader
class Reader<E, A> {
private constructor(protected k) {}
Run = (e: E): A => this.k(e);
Bind = <B>(f: (a: A) => Reader<E, B>): Reader<E, B> =>
new Reader(e => f(this.k(e)).Run(e));
Map = <B>(f: (a: A) => B): Reader<E, B> => new Reader(e => f(this.k(e)));
static Ask = <E, A>(): Reader<E, A> => new Reader(x => x);
static Asks = <E, A>(f: (a: E) => A): Reader<E, A> => new Reader(f);
static Unit = <E, A>(f: A): Reader<E, A> => new Reader(() => f);
}
@ulve
ulve / monadwriter.ts
Last active October 18, 2017 11:37
MonadWriter
class MonadWriter<T, V> {
private v:T;
private log:V[];
constructor(v:T, log:V[] | V) {
this.v = v;
if(log instanceof Array)
this.log = log;
else
this.log = [log];
@ulve
ulve / future.ts
Last active October 8, 2017 19:03
Future
// simple
const future = f => ({
fork: g => f(g),
map: g => future(k => f(l => k(g(l)))),
flatMap: g => future(k => f(l => g(l).apply(k)))
})
const getHttp = callback => {
const id = Math.floor(Math.random() * 5000 + 1000)
setTimeout(() => callback(`hej ${id}`), id)