Skip to content

Instantly share code, notes, and snippets.

@menduz
menduz / softmax.js
Created June 2, 2017 16:41
Softmax
function softmax(output) {
var maximum = output.reduce(function(p,c) { return p>c ? p : c; });
var nominators = output.map(function(e) { return Math.exp(e - maximum); });
var denominator = nominators.reduce(function (p, c) { return p + c; });
var softmax = nominators.map(function(e) { return e / denominator; });
var maxIndex = 0;
softmax.reduce(function(p,c,i){if(p<c) {maxIndex=i; return c;} else return p;});
var result = [];
for (var i=0; i<output.length; i++)
package com.mulesoft.bat;
/*
*
* Copyright 2013 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
@menduz
menduz / infix-fn-call.dwl
Created October 20, 2017 16:10
POST-dollar-sign
[1,2] map (item, index) -> (item * 10 + index)
@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
@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 / 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 / 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 / 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 / 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 / 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)]