liveBook · Manning

Higher-order function

As explained in Julia as a Second Language

A higher-order function is a function that takes one or more functions as arguments, returns a function, or both.

A higher-order function takes a function as an argument, returns a function, or both. In Julia, functions can be passed around like other values: `g = sin` lets you call `g(1.0)`. That makes it possible to write one collection-processing function and choose its behavior by passing in another function. section 4.7 section 15.1

Turn angles into a sine table

`map(deg2rad, 0:15:90)` applies `deg2rad` to each angle in the range and returns an array of radians. To get sine values, use `degsin(deg) = sin(deg2rad(deg))`, then `map(degsin, 0:15:90)`. Combining the conversions in one function avoids creating an intermediate array with a second call to `map`. When an existing output array is needed, `map!` writes into it instead. section 4.7

Listing 4.3 Knock-off version of the built-in map function
function transform(fun, xs)
    ys = []                    #1
    for x in xs
        push!(ys, fun(x))      #2
    end
    ys                         #3
 end

A small version of `map` makes the mechanism visible: accept a function and an iterable, call the function for each element, and collect the results. section 4.7

`filter` uses a function differently. It keeps only values for which a predicate returns `true`. For pizza sales, `filter(islarge, sales)` selects large-pizza records; `map(price, ...)` extracts their prices; and `sum(...)` gives the revenue. Filtering, mapping, and reducing can be combined to answer a question without writing a separate loop for every query. section 4.10 section 4.10.1

Map and reduce in one call

In `sinus(x)`, a locally defined `taylor(i)` calculates one Taylor-series term using `x`. The expression `mapreduce(taylor, +, 0:n)` calculates terms for the range and combines them with addition. Defining `taylor` inside `sinus` gives it access to that call's `x`; the example also connects higher-order functions to closures. section 4.11.1 section 15.0

Higher-order functions are a tool for functional design, not a requirement to avoid every loop. They support reuse by separating an operation such as mapping or filtering from the function supplied to it. The same function-centered approach can also be compared with an object-oriented design for an encryption service. section 15.1 section 15.7

How Julia as a Second Language covers it

How deep it goes
Defines the concept, builds a loop-based mapping function, and works through map, filter, and mapreduce without detailing their internals.
Examples
Demonstrates a sine table, pizza-revenue calculations, a loop-based map implementation, and Taylor-series sine.
What it assumes
Assumes familiarity with functions and collections while introducing functions as values and Boolean-returning predicates.
What you can do after
Build it from scratch as a mapping loop, then apply it via Julia's map, filter, and mapreduce.
How it connects
Connects higher-order functions to first-class functions, predicates, closures, functional programming, and object-oriented programming.

Also explained in

Compare how 14 books explain higher-order function →

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.