Skip to content

Instantly share code, notes, and snippets.

@menduz
Created December 18, 2017 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save menduz/2dfb1ec8e9a5e8b33ff585edb93cbcdc to your computer and use it in GitHub Desktop.
Save menduz/2dfb1ec8e9a5e8b33ff585edb93cbcdc to your computer and use it in GitHub Desktop.
blogpost-act-1
fun generator(start: Number = 0): Array<Number> = [start ~ generator(start + 1)]
---
// This script will pick 100 numbers from
// the generator, that is also being filtered
(generator(1000) filter isEven($))[0 to 99]
%dw 2.0
fun myInfix(lhs: String, rhs: String): String = "$lhs $rhs"
---
"Hello" myInfix "world!"
[1, 2, 3] map (val, ix) -> { value: val, index: ix }
// Is the same as
map([1, 2, 3], (val, ix) -> { value: val, index: ix })
// for clarity, we removed the index argument of the mapping function
fun map<I,O>(list: Array<I>, fn: (elem: I) -> O): Array<O> =
list match {
// When we receive an empty list, we return an empty list.
// The execution finishes there.
case [] -> []
// When we receive at least one element in the list,
// we take off the head and let a lazy tail in the stack
// Then we construct an array with the transformed head element and a lazy tail
// Since the construction and deconstruction of tails are lazy operations,
// this whole process behaves as a stream.
case [head ~ tail] -> [fn(head) ~ map(tail, fn)]
}
%dw 2.0
fun myInfix(lhs: String, rhs: String): String = "$lhs $rhs"
fun myInfix(lhs: Number, rhs: Number): Number = lhs + rhs
---
[
"Hello" myInfix "world!",
1 myInfix 2
]
1 to 100 map { valueX: $ } filter isEven($.valueX) groupBy floor($.valueX / 4)
// 1 to 100
// map { valueX: $ }
// filter isEven($.valueX)
// groupBy floor($.valueX / 4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment