Skip to content

Instantly share code, notes, and snippets.

{
"message": "Hello world!"
}
@menduz
menduz / __inputs__payload.json
Last active December 2, 2017 19:52
Untitled project
{
"message": "Hello world!"
}
@menduz
menduz / Brainfuck.dwl
Created November 17, 2017 21:46
Brainfuck implementation written in DataWeave
%dw 2.0
output application/dw
import mergeWith from dw::core::Objects
type Stream<T> = {|
data: Array<T>,
position: Number
|}
type Machine = {|
@menduz
menduz / do-while.dwl
Created November 17, 2017 21:08
Do while
%dw 2.0
output application/json
fun while<T>(a, b: (T) -> Boolean) = do {
var r = a()
fun innerWhile(x) = do {
var r = a(x)
---
b(r) match {
case true -> [r ~ innerWhile(r)]
@menduz
menduz / QueryString.dwl
Last active November 11, 2017 17:07
TDD in DataWeave
%dw 2.0
output application/json
// See the documentation for this functions
import decodeURI, decodeURIComponent, encodeURI, encodeURIComponent from dw::core::URL
// Step 1) Enter http://rebrand.ly/dwl-2-playground
// Step 2) Copy and paste this code in the transformation editor
// You now will see the results in the right pane
// Step 3) Implement the parse(str) function
// Once you make a test pass, that test will dissapear
@menduz
menduz / findIndex.dwl
Last active November 10, 2017 19:41
FindIndex in DataWeave
fun indexOf<T>(array: Array<T>, elem: T, carry: Number = 0): Number =
array match {
case [] -> -1
case [head ~ tail] ->
if(head == elem)
carry
else
findIndex(tail, elem, carry + 1)
}
@menduz
menduz / Y_Combinator.dwl
Created November 7, 2017 00:42
DataWeave Y Combinator
fun Y(f) = do {
var x =
((h) -> (arguments) -> using(g = f(h(h))) g(arguments))
(((h) -> (arguments) -> using(g = f(h(h))) g(arguments)))
---
f(x)
}
@menduz
menduz / sma.dwl
Created November 6, 2017 23:15
DataWeave SMA
%dw 2.0
output application/json
fun sma(list: Array<Number>, window: Number) =
0 to (sizeOf(list) - window) map ($ to $ + window - 1) map avg(list[$])
---
sma([90, 40, 45, 100, 101], 2)
@menduz
menduz / stddev.dwl
Created November 6, 2017 14:18
Standard deviation in DataWeave
fun stdDev(values: Array<Number>): Number = do {
var average = avg(values)
var squareDiffs = values map do {
var diff = $ - average
---
diff * diff
}
---
sqrt(avg(squareDiffs))
}
@menduz
menduz / pi.dwl
Last active November 6, 2017 00:29
Calculate PI using DataWeave
/**
* This function returns a stream. We first populate the stream with a head value,
* and a lazy tail.
* In this case, we tell the engine the tail is a function call.
* That function call will be not be executed UNTIL the value of that call is needed.
*/
fun leibnizTerm(n: Number = 0): Array<Number> =
[4 / (n + 1) - 4 / (n + 3) ~ leibnizTerm(n + 4)]
// ^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^
// head lazy tail