Skip to content

Instantly share code, notes, and snippets.

@kaineer
Created November 2, 2024 12:22
Show Gist options
  • Save kaineer/e84f9d2bfdbf3840cfb5e2788e550eab to your computer and use it in GitHub Desktop.
Save kaineer/e84f9d2bfdbf3840cfb5e2788e550eab to your computer and use it in GitHub Desktop.
const context = ['sin', 'cos', 'exp']
.map((name) => ('const ' + name + ' = Math.' + name + '; '))
.join('');
export const buildFunction = (fn: string): (x: number) => number => {
const fullFn = '(x) => { ' + context + ' return (' + fn + '); }';
return eval(fullFn);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment