Skip to content

Instantly share code, notes, and snippets.

@mikeacjones
Last active January 6, 2021 16:40
Show Gist options
  • Save mikeacjones/06b0b63a66be53a2024767706559524f to your computer and use it in GitHub Desktop.
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.
%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
}
@mikeacjones
Copy link
Author

Example forLoop:

forLoop(0, 10, (s, i) -> s << i, [])

Outputs:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment