liveBook · Manning

First-class object

As explained in Learn Haskell by Example

A first-class function is a function that can be treated as a value, including being passed, returned, named, stored, or composed.

A first-class function is a value that can be passed to another function, returned, named, stored in a data structure, or used to compute a new function. This makes reusable function composition central to declarative programming. section 1.2.2 section 8.3

Compose transformations

The composition operator applies its second function first and its first function second: (.) f g x = f (g x). Thus, f = (+1) . (*3) gives f 10 = 31. Functions can also be composed from a list, allowing a data structure to describe a reusable transformation pipeline. section 8.3

Listing 8.8 Functions to write CSV values to files
toFileContent :: Csv -> [T.Text]
toFileContent Csv {..} =
  let rows = L.map (L.map dataFieldToText)
 $ L.transpose csvColumns                 #1
   in L.map (T.intercalate ",") $         #2
        M.maybe rows (: rows) csvHeader     #3

writeCsv :: FilePath -> Csv -> IO ()
writeCsv path = TIO.writeFile path .
 T.intercalate "\n" . toFileContent      #4

The CSV writer builds an IO action by composing functions that transpose columns, convert fields, intercalate rows, and write the resulting text to a file. section 8.3

How Learn Haskell by Example covers it

How deep it goes
Explains passing, returning, naming, storing, and composing functions, then demonstrates composition in GHCi and CSV output.
Examples
Uses a cake recipe analogy, GHCi composition expressions, composed function lists, and a CSV file-writing pipeline.
What you can do after
Apply function composition and partial application to build concise transformation pipelines, including CSV output actions.
How it connects
Connects first-class functions with functional programming, declarative programming, partial function application, and function composition.

Also explained in

Compare how 7 books explain first-class object →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Haskell by Example. The section links point to the parts of the book it draws on.