Skip to content

Instantly share code, notes, and snippets.

View softmax.js
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++)
View Poller.java
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 / transform.dwl
Created Oct 11, 2017
DataWeave prime number generator
View transform.dwl
%dw 2.0
output application/json
import some from dw::core::Arrays
fun isPrime(num: Number): Boolean = do {
// try primes <= 16
if (num <= 16)
(num == 2) or (num == 3) or (num == 5) or (num == 7) or (num == 11) or (num == 13)
else if ( // cull multiples of 2, 3, 5 or 7
View infix-fn-call.dwl
[1,2] map (item, index) -> (item * 10 + index)
@menduz
menduz / pi.dwl
Last active Nov 6, 2017
Calculate PI using DataWeave
View pi.dwl
/**
* 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 Nov 6, 2017
Standard deviation in DataWeave
View stddev.dwl
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 Nov 6, 2017
DataWeave SMA
View sma.dwl
%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 Nov 7, 2017
DataWeave Y Combinator
View Y_Combinator.dwl
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 Nov 10, 2017
FindIndex in DataWeave
View findIndex.dwl
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 Nov 11, 2017
TDD in DataWeave
View QueryString.dwl
%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
You can’t perform that action at this time.