A phrase that I\'ve noticed recently is the concept of \"point free\" style...
First, there was this question, and also this one.
Then, I discovered here the
Point-free style means that the arguments of the function being defined are not explicitly mentioned, that the function is defined through function composition.
If you have two functions, like
square :: a -> a
square x = x*x
inc :: a -> a
inc x = x+1
and if you want to combine these two functions to one that calculates x*x+1
, you can define it "point-full" like this:
f :: a -> a
f x = inc (square x)
The point-free alternative would be not to talk about the argument x
:
f :: a -> a
f = inc . square
Point free style means that the code doesn't explicitly mention it's arguments, even though they exist and are being used.
This works in Haskell because of the way functions work.
For instance:
myTake = take
returns a function that takes one argument, therefore there is no reason to explicit type the argument unless you just want too.
Here is one example in TypeScript without any other library:
interface Transaction {
amount: number;
}
class Test {
public getPositiveNumbers(transactions: Transaction[]) {
return transactions.filter(this.isPositive);
//return transactions.filter((transaction: {amount: number} => transaction.amount > 0));
}
public getBigNumbers(transactions: Transaction[]) {
// point-free
return transactions.filter(this.moreThan(10));
// not point-free
// return transactions.filter((transaction: any) => transaction.amount > 10);
}
private isPositive(transaction: Transaction) {
return transactions.amount > 0;
}
private moreThan(amount: number) {
return (transaction: Transaction) => {
return transactions.amount > amount;
}
}
}
You can see point-free style is more "fluent" and easier to read.
Just look at the Wikipedia article to get your definition:
Tacit programming (point-free programming) is a programming paradigm in which a function definition does not include information regarding its arguments, using combinators and function composition [...] instead of variables.
Haskell example:
Conventional (you specify the arguments explicitly):
sum (x:xs) = x + (sum xs)
sum [] = 0
Point-free (sum
doesn't have any explicit arguments - it's just a fold with +
starting with 0):
sum = foldr (+) 0
Or even simpler: Instead of g(x) = f(x)
, you could just write g = f
.
So yes: It's closely related to currying (or operations like function composition).
A JavaScript sample:
//not pointfree cause we receive args
var initials = function(name) {
return name.split(' ').map(compose(toUpperCase, head)).join('. ');
};
const compose = (...fns) => (...args) => fns.reduceRight((res, fn) => [fn.call(null, ...res)], args)[0];
const join = m => m.join();
//pointfree
var initials = compose(join('. '), map(compose(toUpperCase, head)), split(' '));
initials("hunter stockton thompson");
// 'H. S. T'
Reference