Skip to content

Instantly share code, notes, and snippets.

@alexandramartinez
Created March 20, 2024 19:21
Show Gist options
  • Save alexandramartinez/1c398eb611c569746ce885cee0e848a4 to your computer and use it in GitHub Desktop.
Save alexandramartinez/1c398eb611c569746ce885cee0e848a4 to your computer and use it in GitHub Desktop.
%dw 2.0
output application/json
import time from dw::util::Timer
var items = (1 to 10000) as Array map {
id: $$,
criteria: randomInt(100),
yearOfBirth: 1900 + randomInt(123)
}
fun mapAndFilter() =
items map {
($),
isValid: $.criteria < 50, // needed for filter
additionalField: "something",
years: now().year - $.yearOfBirth
} filter ($.isValid)
fun onlyReduce() = items reduce (item, acc=[]) ->
if (item.criteria < 50)
acc + {
(item),
isValid: item.criteria < 50, // can be removed
additionalField: "something",
years: now().year - item.yearOfBirth
}
else acc
fun filterAndMap() =
items filter ($.criteria < 50) map {
($),
isValid: $.criteria < 50, // can be removed
additionalField: "something",
years: now().year - $.yearOfBirth
}
---
{
mapAndFilter: time(() -> mapAndFilter()) then $.end - $.start,
onlyReduce: time(() -> onlyReduce()) then $.end - $.start,
filterAndMap: time(() -> filterAndMap()) then $.end - $.start
}
mapObject ((value, key, index) ->
(key): (value as String)[2 to -2] as Number
) orderBy $
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment