Skip to content

Instantly share code, notes, and snippets.

@pelotom
Last active December 30, 2020 11:18
Show Gist options
  • Select an option

  • Save pelotom/98e0de19adfc164f9248 to your computer and use it in GitHub Desktop.

Select an option

Save pelotom/98e0de19adfc164f9248 to your computer and use it in GitHub Desktop.
function λ(strings, ...args) {
const splat = strings.raw[0].split('_')
let buf = '', table = {}
for (let i = 0; i < splat.length - 1; i++) {
const varName = '___' + i
buf += splat[i] + varName
table[varName] = args[i]
}
buf += splat[splat.length - 1]
const keys = Object.keys(table)
return new (Function.prototype.bind.apply(Function, [null, ...keys, 'return ' + buf]))
}
/*
> λ`(_ + _) * _`(4, 2, 7)
42
> [{x: 'foo'}, {x: true}, {x: 3}].map(λ`_.x`)
["foo", true, 3]
*/
@nitsanavni
Copy link
Copy Markdown

can we make this safer by making it type-safe with typescript?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment