Skip to content

Instantly share code, notes, and snippets.

@victorkurauchi
Created December 16, 2019 01:48
Show Gist options
  • Save victorkurauchi/e0a217cb543051a9a4d9943f1470a312 to your computer and use it in GitHub Desktop.
Save victorkurauchi/e0a217cb543051a9a4d9943f1470a312 to your computer and use it in GitHub Desktop.
Testing clojures
const secret = (msg) => () => msg;
// partialApply(targetFunction: Function, ...fixedArgs: Any[]) =>
// functionWithFewerParams(...remainingArgs: Any[])
const partialApply = (fn, ...fixedArgs) => {
return function (...remainingArgs) {
return fn.apply(this, fixedArgs.concat(remainingArgs));
};
};
describe('testing clojure', () => {
test('secret', () => {
const msg = 'secret() should return a function that returns the passed secret.';
const theSecret = 'Closures are easy.';
const mySecret = secret(theSecret);
console.log(mySecret);
const actual = mySecret();
const expected = theSecret;
expect(actual).toBe(expected);
});
test('adds 1 + 1 = 2', () => {
const add = (a, b) => a + b;
const result = add(1, 1);
expect(result).toBe(2);
});
test('partialApply() should partially apply functions', () => {
const add = (a, b) => a + b;
const add10 = partialApply(add, 10);
const actual = add10(5);
const expected = 15;
expect(actual).toBe(expected);
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment