Skip to content

Instantly share code, notes, and snippets.

View dbrattli's full-sized avatar
👨‍💻
Python ❤️ F#

Dag Brattli dbrattli

👨‍💻
Python ❤️ F#
View GitHub Profile
@dbrattli
dbrattli / gist:a61e7de3ca5c0269f436
Last active August 29, 2015 14:23
Python help for RxPY flat_map_latest
>>> from rx import Observable
>>> help(Observable.flat_map_latest)
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* */
/* Simple node js module to get distance between two coordinates. */
/* */
/* Code transformed from Chris Veness example code - please refer to his website for licensing */
/* questions. */
/* */
/* */
/* Latitude/longitude spherical geodesy formulae & scripts (c) Chris Veness 2002-2011 */
/* - www.movable-type.co.uk/scripts/latlong.html */
@dbrattli
dbrattli / lol.py
Last active December 28, 2015 21:09
List out of lambda, Python versions
empty_list = None
def prepend(el, lst):
def func(selector):
return selector(el, lst)
return func
def head(lst):
def selector(h, t):
return h
@dbrattli
dbrattli / actions.py
Last active May 12, 2016 05:51
Closing over loop variable considered harmful
actions=[]
for x in range(10):
actions.append(lambda: print(x))
for act in actions:
act()
@dbrattli
dbrattli / SyncBlockingForever.fs
Last active October 7, 2018 08:30
SyncBlockingForever
// A (synchronous) function that might block forever
let later () =
System.Console.ReadLine()
@dbrattli
dbrattli / Callback.fs
Last active October 7, 2018 09:40
Callback
// Callback handler
let myCallback x =
printfn "Got %A" x
// Asynchronous function taking a callback handler
let asyncFun x cb =
let result = x * 10
cb result
// Call function with provided callback handler
@dbrattli
dbrattli / Pythagoras.fs
Created October 7, 2018 10:38
Pythagoras
let add a b = a + b
let square x = x * x
let pythagoras a b = sqrt (add (square a) (square b))
let result = pythagoras 10.0 20.0
@dbrattli
dbrattli / PythagorasCPS.fs
Created October 7, 2018 10:42
Pythagoras CPS
let addCps a b cont : unit =
cont (a + b)
let squareCps x cont : unit =
cont (x * x)
let sqrtCps x cont : unit =
cont (sqrt x)
// Pythagoras rewritten in CPS
@dbrattli
dbrattli / Times10CpsCurried.fs
Last active October 7, 2018 19:46
Times 10 Curried Continuation
// Return a function that takes a callback that takes the result.
let times10 (x: int) : ((int -> unit) -> unit) =
let thenDo (cb : int -> unit) =
cb (x * 10)
thenDo
let cb result =
printfn "%A" result
let thenDo = times10 42
@dbrattli
dbrattli / AsyncValue.fs
Last active October 11, 2018 20:45
Async Value
// An asynchronous value
let valAsync = async { return 42 }