Last active
January 6, 2021 16:40
-
-
Save mikeacjones/06b0b63a66be53a2024767706559524f to your computer and use it in GitHub Desktop.
Just some odds and ends functions that I may or may not use. Some of these are just personal experiments.
This file contains 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
%dw 2.0 | |
fun getEdges(left: Array, right: Array, isEdgeFn: (l: Object, r: Object) -> Boolean) = | |
left reduce ((leftItem, mAccum = { li: 0, edges: [] }) -> | |
(right reduce ((rightItem, iAccum = { li: mAccum.li, ri: 0, edges: mAccum.edges }) -> | |
{ | |
li: iAccum.li, | |
ri: iAccum.ri + 1, | |
edges: iAccum.edges ++ [([iAccum.li, iAccum.ri]) if (isEdgeFn(leftItem, rightItem))] | |
} | |
)) - "li" ++ { li: mAccum.li + 1 } | |
) | |
var queue = (q = [], item = null) -> | |
{ | |
queue: q, | |
item: item, | |
dequeue: () -> queue(q[1 to -1] default [], q[0]), | |
enqueue: (item) -> queue(q << item, null) | |
} | |
var forLoop = (i: Number, max: Number, stateFn: (s: Any, i: Number) -> Any, state = {}) -> | |
if (i < max) forLoop(i + 1, max, stateFn, stateFn(state, i)) | |
else state | |
var while = (conditionFn: (s: Any) -> Boolean, whileFn: (s: Any) -> Any, state = {}) -> | |
if (conditionFn(state)) while(conditionFn, whileFn, whileFn(state)) | |
else state | |
var doWhile = (conditionFn: (s: Any) -> Boolean, whileFn: (s: Any) -> Any, state = {}) -> | |
do { | |
var newState = whileFn(state) | |
--- | |
if (conditionFn(newState)) doWhile(conditionFn, whileFn, newState) | |
else newState | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example forLoop:
Outputs: