Key takeaways
In Clojure,
filter
function is a higher-order function used to modify collection by selecting elements from an existing one based on a specified predicate.The
filter
function takes two parameters: a predicate function and a collection to filter.Common use cases include filtering file types (e.g., extracting
.txt
files) and cleaning data by removing invalid ornil
values.
The Clojure is a dynamically typed functional programming language. Clojure is well known for its sophisticated method of handling data. The filter
function is one of its most effective tools for modifying collections. We'll dive deep into filter
, exploring its syntax, applications, and real-world examples.
filter
In Clojure, filter
is a higher-order function, which means it has two arguments:
Another function
A collection
By choosing elements from an existing collection that meet a specified predicate, a filter
creates a new collection.
(filter predicate collection)
predicate
is a function that takes an element from the collection and returns true
or false
based on a condition.
collection
is the input collection from which we will filter elements.
In this example, filter
uses the even?
predicate to select even numbers from the numbers
collection.
(def numbers [1 2 3 4 5 6 7 8 9 10])(def even-numbers (filter even? numbers))(println even-numbers)
Line 1: def
is used to define a new variable and numbers
is the name of the variable. Here, [1 2 3 4 5 6 7 8 9 10]
is a vector containing the numbers 1 to 10.
Line 3: even-numbers
is the new variable and (filter even? numbers)
applies the filter
function to numbers
, keeping only the elements for which the even?
predicate returns true (i.e., the even numbers).
Line 5: println
is a function that prints the given value to the console and even-numbers
is the value being printed, which is the list of even numbers from the numbers
vector.
The power of filter
becomes evident when applied to real-world problems. Let's explore some common use cases where filter
shines.
Imagine you have a list of files and you want to extract only the text files. This is where filter
comes in handy.
(def files ["document.txt" "image.jpg" "spreadsheet.xlsx" "readme.md"])(def is-text-file? (fn [file] (.endsWith file ".txt")))(def text-files (filter is-text-file? files))(println text-files)
Line 1: ["document.txt" "image.jpg" "spreadsheet.xlsx" "readme.md"]
is a vector containing the names of four files.
Line 2: is-text-file?
is the new variable and (fn [file] (.endsWith file ".txt"))
is an anonymous function (lambda) that takes a file name as an argument (file
) and returns true if the file name ends with ".txt".
Line 3: text-files
is the variable and (filter is-text-file? files)
applies the filter
function to files
, keeping only the elements for which the is-text-file?
function returns true (i.e., the files ending with ".txt").
Line 5: println
is a function that prints the given value to the console and text-files
is the value being printed, which is the list of files that end with ".txt".
When working with data, it's common to have invalid or null values. filter
can help you clean your data by removing these unwanted elements.
(def data [1 2 3 nil 5 nil 7 8 nil])(def valid-data (filter identity data))(println valid-data)
Line 1: data
is the variable name and [1 2 3 nil 5 nil 7 8 nil]
is a vector containing numbers and nil
values.
Line 2: valid-data
is the variable and (filter identity data)
applies the filter
function to data
, keeping only the elements for which the identity
function returns true (i.e., non-nil values). The identity
function returns its argument, so it effectively removes nil
values.
Line 4: println
is a function that prints the given value to the console and valid-data
is the value being printed, which is the list of non-nil values from the data
vector.
In this case, the identity
function is used as the predicate, effectively removing all nil
values from the data
collection.
Free Resources