Skip to content

Instantly share code, notes, and snippets.

@divs1210
Last active March 4, 2024 13:03
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 divs1210/462b9a6c49b49439316b05ab037f53c0 to your computer and use it in GitHub Desktop.
Save divs1210/462b9a6c49b49439316b05ab037f53c0 to your computer and use it in GitHub Desktop.
Transducer expansion
function mapping(f) {
return function(rf) {
return function(acc, x) {
return rf(acc, f(x))
}
}
}
function filtering(f) {
return function(rf) {
return function(acc, x) {
if(f(x))
return rf(acc, x)
else
return acc
}
}
}
function comp(f, g) {
return function(x) {
let x2 = g(x)
let x3 = f(x2)
return x3
}
}
let xf = comp(
mapping(inc),
filtering(isEven)
)
// =>
let xf = function(rf) {
let rf2 = filtering(isEven)(rf)
let rf3 = mapping(inc)(rf2)
return rf3
}
// =>
let xf = function(rf) {
let rf2 = function(acc, x) {
if(isEven(x))
return rf(acc, x)
else
return acc
}
let rf3 = function(acc, x) {
return rf2(acc, inc(x))
}
return rf3
}
// =>
let xf = function(rf) {
function(acc, x) {
acc = acc
x = inc(x)
if(isEven(x))
return rf(acc, x)
else
return acc
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment