Last active
March 4, 2024 13:03
-
-
Save divs1210/462b9a6c49b49439316b05ab037f53c0 to your computer and use it in GitHub Desktop.
Transducer expansion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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