Techniqueadvanced
Point-free style
Also called tacit programming, pointfree style, point-free programming, pointless style, eta reduction.
Define a function as a composition of other functions, with partial application and function names in place of lambdas, so that the definition never names the argument that it works on.
Purpose
In point-free style, a definition names the functions that make up a function, but not the value that the function gets. Wikipedia describes it as a paradigm "in which function definitions do not identify the arguments"1 on which they operate. "Instead the definitions merely compose other functions, among which are combinators that manipulate the arguments."1
The HaskellWiki compares two definitions of the same function:
sum = foldr (+) 0is point-free.sum' xs = foldr (+) 0 xsnames the argumentxs.
"These functions perform the same operation, however, the former is more compact, and is considered cleaner."2 The style "helps the writer (and reader) think about composing functions (high level), rather than shuffling data (low level)."2
The points are the values, not the dot of composition. "The term originated in topology, a branch of mathematics which works with spaces composed of points, and functions between those spaces."2 "In Haskell, our 'space' is some type, and 'points' are values."2
Steps
- Remove a wrapper. When a function only passes its argument to one other function, use that function. In the lambda calculus, η-conversion "converts between λx.fx and f whenever x does not appear free in f."3 So
lambda s: str.upper(s)becomesstr.upper, and|i| i.to_string()becomesToString::to_string4. - Fix the first arguments. Give a function some of its arguments now and the rest later.
foldr (+) 0givesfoldrthe function(+)and the start value0, and the list comes later2. In Python,functools.partialreturns an object that "when called will behave like func called with the positional arguments args and keyword arguments keywords."5 - Join the steps with composition. The HaskellWiki says that "it is clearer to write let fn = f . g . h than to write let fn x = f (g (h x))."2 See Function composition.
- Use a library function in place of a small lambda. Python
operator.itemgetterwill "Return a callable object that fetches item from its operand using the operand’s getitem() method."6 Java method references "are compact, easy-to-read lambda expressions for methods that already have a name."7 - Stop when the next step only moves arguments. When a definition needs
flipor other combinators to put the arguments in order, a named argument is clearer.
Example
The function total_price adds the prices of a list of items. The first version names the list. The second version is point-free. It uses the compose of the Wikipedia article1, which applies the functions from left to right.
from functools import partial, reduce
from operator import itemgetter
def compose(*functions):
return partial(reduce, lambda x, f: f(x), functions)
def total_price_pointful(items):
return sum(map(lambda item: item[1], items))
total_price = compose(partial(map, itemgetter(1)), sum)
items = [("pen", 3), ("book", 12), ("bag", 25)]
print(total_price_pointful(items))
print(total_price(items))
print(list(map(str.upper, ["red", "green"])))
40
40
['RED', 'GREEN']
partial(map, itemgetter(1)) is step 2 and step 4: it fixes the first argument of map, and itemgetter(1) replaces the lambda. compose is step 3. str.upper in the last line is step 1.
When to use
Use the style for short pipelines of named steps, such as a sort key, a map over a list or a chain of transformations. A function name in place of a lambda is often the clearest form. Unix pipes use the same idea: "Unix scripting uses the paradigm with pipes."1
Do not use it when the definition needs combinators only to move arguments, or when a name for the argument tells the reader what the value is. "The lack of argument naming gives point-free style a reputation of being needlessly obscure"1.
Pitfalls
- Extra arguments. A function in place of a lambda gets all the arguments that the caller gives. JavaScript
mapgives three. MDN shows a call that mapsparseIntover three strings of digits: "the actual result is [1, NaN, NaN]", because "Array.prototype.map passes 3 arguments: the element, the index, and the array."8parseIntreads the index as the radix:
This code runs but gives a wrong answer on purpose.
console.log(["1", "2", "3"].map(parseInt));
console.log(["1", "2", "3"].map((s) => parseInt(s, 10)));
[ 1, NaN, NaN ]
[ 1, 2, 3 ]
- A method without its object. In JavaScript,
obj.methodused as a value does not keepobj. MDN says: "A common mistake for new JavaScript programmers is to extract a method from an object, then to later call that function and expect it to use the original object as its this"9. Usebind, which "creates a new function that, when called, calls this function with its this keyword set to the provided value"9:
This code runs but gives a wrong answer on purpose.
class Counter {
constructor() {
this.count = 0;
}
add() {
this.count += 1;
}
}
const counter = new Counter();
try {
[1, 2, 3].forEach(counter.add);
} catch (e) {
console.log(e.name);
}
[1, 2, 3].forEach(counter.add.bind(counter));
console.log(counter.count);
TypeError
3
- Definitions that nobody can read. The tool that makes Haskell code point-free shows the limit. The HaskellWiki says: "It can, of course, get out of hand"2. For
\a b c -> a*b+2+c, the tool gives((+) .) . flip flip 2 . ((+) .) . (*)2. - Composition order.
f . g . hin Haskell runshfirst, because it is the same asf (g (h x))2. Thecomposeabove runs the functions from left to right. Java has both orders:andThen"Returns a composed function that first applies this function to its input, and then applies the after function to the result."10 Check the order of each helper.
Tools that do it
| Language | A function by name | Fix the first arguments | Compose |
|---|---|---|---|
| Python | str.upper, operator.itemgetter |
functools.partial |
a helper such as compose |
| JavaScript, TypeScript | a function name, method.bind(obj) |
bind with arguments |
a helper or a chain of calls |
| Java | method references, String::length |
a lambda | Function.andThen, Function.compose |
| C# | a method group, as in obj.DoWork |
a lambda | a lambda |
| C++ | a function name | std::bind_front |
a lambda |
| Rust | a path, as in ToString::to_string |
a closure | a closure |
| Go | a method expression T.M or a method value x.M |
a function literal | a function literal |
| F# | a function name | partial application | >> and << |
| Bash | a command name | the arguments of the command | a pipeline |
Notes on the table:
- In C#, "When you instantiate a delegate by using a named method, the method is passed as a parameter"11.
- In Go, "T.M is a function that is callable as a regular function with the same arguments as M prefixed by an additional argument that is the receiver of the method."12
- In C++,
std::bind_frontmakes a wrapper with the first arguments of the target "bound to args"13. - In F#, "The composition operator >> takes two functions and returns a function"14.
- In Bash, "The output of each command in the pipeline is connected via a pipe to the input of the next command."15
- For Haskell, a tool converts expressions to point-free form. "A stand-alone command-line version is available at HackageDB (package pointfree)."2
References
- Tacit programming, Wikipedia, the introduction; Python; Functional programming; Unix pipeline
- Pointfree, HaskellWiki, the introduction; But pointfree has more points; Tool support
- Lambda calculus, Wikipedia, Reduction
- Advanced Functions and Closures, The Rust Programming Language, Function Pointers, Listing 20-30
- functools, The Python Standard Library, functools.partial
- operator, The Python Standard Library, operator.itemgetter; operator.methodcaller
- Method References, The Java Tutorials, the introduction; Kinds of Method References
- Array.prototype.map(), MDN Web Docs, Using parseInt() with map()
- Function.prototype.bind(), MDN Web Docs, the introduction; Creating a bound function
- Interface Function, Java SE 25 API, andThen
- Delegates with Named vs. Anonymous Methods, C# Programming Guide, the introduction
- The Go Programming Language Specification, Method expressions
- std::bind_front, std::bind_back, C++ reference, cppreference.com, the description
- Functions, F# language reference, Function Composition and Pipelining
- Pipelines, Bash Reference Manual, 3.2.3 Pipelines
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag KWFBHJ Edit this page History Report an error Markdown