Skip to content

Instantly share code, notes, and snippets.

@honewatson
Last active August 28, 2018 22:59
Show Gist options
  • Save honewatson/dd7b5098b0dd4e64541b58fbaac3b69d to your computer and use it in GitHub Desktop.
Save honewatson/dd7b5098b0dd4e64541b58fbaac3b69d to your computer and use it in GitHub Desktop.
Recursive Tree Transducer

Copyright 2018 Hone Watson

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

const maybeApply = (test, update) => (targets) =>
[].concat(targets).reduce((accum, target) => {
return accum.concat(test(target) ? update(target) : target);
}, [])
const mapping = f => reducing => (accumulate, input) => reducing(accumulate, f(input));
const filtering = predicate => reducing => (accumulate, input) =>
predicate(input) ? reducing(accumulate, input) : accumulate;
const filteringArray = predicate => reducing => (accumulate, inputs) =>
reducing(accumulate, [].concat(inputs).filter(predicate))
const arrayReducing = (accumulate, result) => accumulate.concat(result);
const arrayReducingClean = (accumulate, result) => accumulate.concat(result ? result : []);
const transduce = (xform, reducing, initial, input) => input.reduce(xform(reducing), initial);
const compose2 = (f, g) => (...args) => f(g(...args))
const compose = (...fns) => fns.reduce(compose2);
const pipe = (...fns) => fns.reduceRight(compose2);
const composeMapping = compose(mapping, compose)
const names = (data) => {
const xform = compose(
composeMapping(
maybeApply(
it => it.name === 'billy',
it => ({
name: it.name + ' jean'
})
),
maybeApply(
it => it.name === 'sandra',
it => [
{
name: it.name + ' dee'
},
{
name: 'danny zuko'
}
]
),
maybeApply(
it => it.data,
it => names(it.data)
)
),
filteringArray(it => it.name),
)
return transduce(xform, arrayReducing, [], data)
}
const data = [
{ name: 'billy' },
{ name: 'sandra' },
{ place: 'bodhi' },
{
data: [
{ name: 'level 2'},
{
data: [
{ name: 'level 3' },
{ bobo: 'queen' },
{ name: 'level 3b' }
]
}
]
}
]
console.log(names(data));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment