DEV Community

Lukas Polak
Lukas Polak

Posted on • Edited on • Originally published at lukaspolak.com

JavaScript currying 2 - Tacit programming (Point-free style)

Tacid programming (i.e., Point-free style) is a programming paradigm, where function definition does not refer to the function arguments. Function declaration, on the other hand, requires any formal parameters to be declared. Every point-free function has its closure scope. The closure is created at the function creation time - when the function is invoked. Every form of the curried function is a form fo higher-order-function

const add = (a) => (b) => a + b;
const incrementByOne = add(1); // partially applied function
incrementByOne(9); // => 10
Enter fullscreen mode Exit fullscreen mode

When we create incrementByOne with function call add(1), the a parameter from add function get fixed to 1 inside the returned function that gets assigned to incrementByOne function. When we call incrementByOne function with b parameter fixed to 9 function application completes, and returns the sum of 1 and 9.

// Another example of point-free style function
const g = (n) => n + 1;
const f = (n) => n * 2;
const h = (x) => f(g(x));
h(20); // => 42
Enter fullscreen mode Exit fullscreen mode

Top comments (3)

Collapse
 
huzaifams profile image
HuzaifaMS

I tried this code piece and I found that a and b are of type any in typescript. Is there anyway to get the code to work with type annotations.

Collapse
 
lukaspolak profile image
Lukas Polak

Yes Muhammad, you can use this

const add = (a: number) => {
  return (b: number): number => {
    return a + b;
  };
};
Enter fullscreen mode Exit fullscreen mode
Collapse
 
huzaifams profile image
HuzaifaMS

Thank you. I appreciate you taking the time to reply.

May Peace and Blessings be upon you and your family.