Last active
June 9, 2021 22:21
-
-
Save WhoAteDaCake/8d754cd3fd609aa508a8ce05f48c813a to your computer and use it in GitHub Desktop.
Reasonml snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type stateVal = int; | |
type stateMod = stateVal => stateVal; | |
type stateModList = ref(list(stateMod)); | |
type stateObj = { | |
. | |
getState: unit => stateVal, | |
addModifier: stateMod => unit, | |
}; | |
let updateState = List.fold_left((a, f) => f(a)); | |
let modif = n => n + 1; | |
let createState = initialState => { | |
let state = ref(initialState); | |
let modifiers: stateModList = ref([]); | |
{ | |
pub getState = () => updateState(state^, modifiers^); | |
pub addModifier = modf => { | |
modifiers := List.append(modifiers^, [modf]); | |
(); | |
} | |
}; | |
}; | |
let initialState: stateVal = 1; | |
let state: stateObj = createState(initialState); | |
state#addModifier(modif); | |
state#addModifier(modif); | |
state#addModifier(modif); | |
Js.log(state#getState()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type stateVal = int; | |
type stateMod('a) = 'a => 'a; | |
type stateModList('a) = ref(list(stateMod('a))); | |
type stateObj('a) = { | |
. | |
getState: unit => 'a, | |
addModifier: stateMod('a) => unit, | |
addModifiers: list(stateMod('a)) => unit | |
}; | |
let updateState = List.fold_left((a, f) => f(a)); | |
let modif = n => | |
if (n mod 2 === 0) { | |
n + 20; | |
} else { | |
n + 3; | |
}; | |
let createState = (initialState: 'a) => { | |
let state = ref(initialState); | |
let modifiers: stateModList('a) = ref([]); | |
{ | |
pub getState = () => updateState(state^, modifiers^); | |
pub addModifier = modf => { | |
modifiers := List.append(modifiers^, [modf]); | |
(); | |
}; | |
pub addModifiers = modfL => { | |
modifiers := List.append(modifiers^, modfL); | |
(); | |
} | |
}; | |
}; | |
let initialState: stateVal = 1; | |
let state: stateObj(stateVal) = createState(initialState); | |
state#addModifiers([modif, modif, modif]); | |
Js.log(state#getState()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type state = int; | |
type stateMod = (state) => state; | |
type stateModList = array(stateMod); | |
let updateState = List.fold_left((a, f) => f(a)); | |
let modif = n => n + 1; | |
let modifiers = [modif, modif]; | |
Js.log(updateState(1, modifiers)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment