liveBook · Manning

Partial application

As explained in Julia as a Second Language

Partial application is a functional programming technique that fixes some arguments of a function to produce a new function requiring fewer arguments.

Partial application fixes some arguments of a function and returns a new function that accepts the remaining arguments. In Julia, this turns reusable configuration into callable functions: fixing a delimiter creates a splitter, while fixing a predicate creates a filter-ready function. section 15.3.3

The technique fits naturally with closures and the pipe operator. A closure remembers fixed values, and the resulting one-argument function can participate in a readable data flow such as splitting, mapping, and joining. section 15.3.2 section 15.8

Build a camel-case pipeline

Define single-argument methods that fix the delimiter and mapping function, then compose them with |>: import Base: split, map split(dlm::Char) = s -> split(s, dlm) map(fn::Function) = xs -> map(fn, xs) function camel_case(s) s |> split('_') |> map(uppercasefirst) |> join end The same pattern appears in endswith(".png"), which returns a reusable predicate for filter, and in endswith(".png")("truck.png"), which returns true. section 15.3.3

Create reusable cipher functions

Fix a Caesar-cipher shift to obtain an encryptor or decryptor: encrypt = caesar_encrypt(1) encrypt("ABC") # "BCD" A partially applied function can then be placed directly in a pipeline: "HELLO" |> caesar_encrypt(2) |> caesar_decrypt(2) returns "HELLO". Substitution ciphers use the same design, fixing a character mapping instead of a shift. section 15.7.1 section 15.7.2

Listing 15.5 Camel case with partial application
import Base: split, map                    #1
 
split(dlm::Char) = s -> split(s, dlm)      #2
map(fn::Function) = xs -> map(fn, xs)      #3
 
function camel_case(s)
    s |> split('_') |> map(uppercasefirst) |> join
end

Inspect the complete camel-case implementation, including the methods that fix arguments and return closures. section 15.3.3

How Julia as a Second Language covers it

How deep it goes
Defines the technique and implements it with Julia methods and closures, but stops short of runtime or compiler internals.
Examples
Demonstrates camel-case conversion, filename filtering, Caesar encryption, and substitution encryption through reusable functions.
What it assumes
Assumes basic Julia functions and arguments while teaching closures, pipelines, and higher-order function use through examples.
What you can do after
Apply it via Julia closures and methods to build reusable pipeline stages, predicates, encryptors, and decryptors.
How it connects
Connects partial application with closures, higher-order functions, anonymous functions, pipelines, map, filter, reduce, and object-oriented design.

Also explained in

Compare how 10 books explain partial application →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Julia as a Second Language. The section links point to the parts of the book it draws on.