First-Class & Higher-Order Functions
Understand functions as values, how to pass them around, and create powerful higher-order functions.
Functions: First-Class Citizens
In Clojure, functions are "first-class citizens." This means they are treated just like any other value, such as numbers or strings.
You can:
- Assign them to variables.
- Pass them as arguments to other functions.
- Return them as results from other functions.
This powerful concept is fundamental to functional programming!
Assigning Functions to Names
Let's see how we can treat functions as values by assigning them to a name using def or let. Think of it as giving a nickname to a function.
Try running this example:
(ns coddykit.core
(:gen-class))
(defn greet [name]
(str "Hello, " name "!"))
(defn -main [& args]
(println "--- Output ---")
(def my-greeting greet) ; Assign 'greet' function to 'my-greeting'
(println (my-greeting "Alice")))All lessons in this course
- First-Class & Higher-Order Functions
- Immutability & Persistent Data
- Lazy Sequences & Performance
- Transducers for Composable Transformations