liveBook · Manning

Functional, stateless philosophy

As explained in Julia as a Second Language

Functional programming is a programming paradigm that emphasizes expressions, function composition, and transformations of data with limited mutable state.

Functional programming is a way to organize code around functions that transform inputs into outputs. In Julia, this often means avoiding changes to input data so the path from one value to the next is easier to follow. “Stateless” describes a preference, not a rule that every function must obey. section 15.2

Writing functions alone does not make a program functional. Common practices include:

  • Passing and storing functions as values, including passing them to higher-order functions. section 15.1
  • Using map, filter, and reduce for collection transformations, and combining functions through chaining or composition. section 15.1 section 15.0
  • Using closures or lambdas where an object with methods might otherwise manage state. section 15.1
Listing 15.1 Converting snake case to camel case
"Turns hello_to_you into HelloToYou"
function camel_case(input::AbstractString)
   join(map(uppercasefirst, split(input, '_')))
end

The `camel_case` function turns `hello_to_you` into `HelloToYou`. Read its nested calls from the inside out: `split` produces pieces, `map` transforms each piece, and `join` assembles the result. Each output becomes the next function’s input. section 15.2

This differs from organizing a solution around objects whose methods change their state. An encryption service can be designed either around cipher objects and their methods or around higher-order functions and closures. Neither style is automatically best for every problem; comparing both makes the design trade-off visible. section 15.7 section 15.0

How Julia as a Second Language covers it

How deep it goes
Defines functional programming pragmatically through concrete practices and data flow, without imposing a single strict definition.
Examples
Illustrates transformation with a snake-case-to-camel-case function and compares functional and object-oriented designs for an encryption service.
What it assumes
Assumes experience programming in another language but introduces functional principles for readers more familiar with object-oriented design.
What you can do after
Evaluate/understand functional and object-oriented approaches to the same Julia problem, including when a functional approach may not fit.
How it connects
Connects higher-order functions, closures, map, filter, reduce, function composition, and object-oriented programming.

Also explained in

Compare how 19 books explain functional, stateless philosophy →

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.