Last active
April 30, 2022 19:14
-
-
Save Superstar64/44a1e1ed4f6e07cc8bd2c543d6b7e759 to your computer and use it in GitHub Desktop.
Semantic Editor Combinators in javascript
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
// see http://conal.net/blog/posts/semantic-editor-combinators | |
const o = (...f) => { | |
if(f.length === 0) { | |
return x => x; | |
} else { | |
return x => f[0](o(...f.slice(1))(x)); | |
} | |
}; | |
const get = i => f => o => { | |
const op = Object.assign({},o); | |
op[i] = f(op[i]); | |
return op; | |
}; | |
const index = i => f => a => { | |
const ap = a.slice(); | |
ap[i] = f(ap[i]); | |
return ap; | |
}; | |
const map = f => a => a.map(x => f(x)); | |
const result = f => g => x => f(g(x)); | |
const argument = g => f => x => f(g(x)); | |
const example = o(index(0), get("x"), map)(x => +x) ( [{ x : ["1","2"], y : 3},3] ); | |
console.log(example); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment