Skip to content

Instantly share code, notes, and snippets.

@mjgpy3
mjgpy3 / transition.js
Created May 3, 2017 14:16
Functional state transition
// Try to make mutations explicit
function transition(obj, prop, current, next cb) {
obj[prop] = current;
return cb(function () {
obj[prop] = next;
});
}
@mjgpy3
mjgpy3 / let.fs
Created August 13, 2017 21:25
let In F#
let doSomething a b =
let plusOne = a + 1
let result = plusOne*b
result
let doSomething2 a b =
(fun plusOne ->
(fun result ->
result
@mjgpy3
mjgpy3 / memoization.fs
Created August 28, 2017 04:27
memoization.fs
let memo = ref Map.empty
let rec memoizedFibs n =
if Map.containsKey n !memo
then Map.find n !memo
else
let result =
match n with
| 1L -> 1L
@mjgpy3
mjgpy3 / ways-to-define-fsharp-functions.fs
Created September 2, 2017 03:09
ways-to-define-fsharp-functions.fs
let valueOr someDefault perhaps =
match perhaps with
| Some v -> v
| None -> someDefault
let valueOr2 someDefault =
fun perhaps ->
match perhaps with
| Some v -> v
| None -> someDefault
@mjgpy3
mjgpy3 / lets-write-a-lambda-calc-in-fsharp.fsx
Created September 15, 2017 05:48
lets-write-a-lambda-calc-in-fsharp.fsx
(*
Examples:
\x.x -----> \x.x
x -----> error!!!
(\x.x \y.y) -----> \y.y
@mjgpy3
mjgpy3 / lamda-calc-fsharp-option-pure.fsx
Created September 15, 2017 15:41
lamda-calc-fsharp-option-pure.fsx
(*
Examples:
\x.x -----> \x.x
x -----> error!!!
(\x.x \y.y) -----> \y.y
@mjgpy3
mjgpy3 / lamda-calc-fsharp-custom-type.fsx
Created September 15, 2017 17:51
lamda-calc-fsharp-custom-type.fsx
(*
Examples:
\x.x -----> \x.x
x -----> error!!!
(\x.x \y.y) -----> \y.y
@mjgpy3
mjgpy3 / game-of-codes-islands.fsx
Created October 1, 2017 21:58
game-of-codes-islands-fsharp
type Measurements<'a> = {
Water: 'a
Wood: 'a
Coal: 'a
Iron: 'a
Stone: 'a
Obsidian: 'a
Copper: 'a
Gold: 'a
Silver: 'a
@mjgpy3
mjgpy3 / game-of-codes-book-return.fsx
Created October 1, 2017 22:10
game-of-codes-book-return-fsharp
type StepDirection =
| Down
| Up
type SpecifcStair = int
type Index = int
let input = "-+-++---"
let charToStepDir = function
@mjgpy3
mjgpy3 / gocBookReturnEventStore.js
Created October 1, 2017 22:37
game-of-codes-book-return-eventstore
fromStream('book-return-example')
.when({
$init: () => ({
current: 0,
results: []
}),
SteppedDown: (s, e) => {
if (s.current === 0) {
s.results.push(parseInt(e.sequenceNumber)+1);
}