liveBook · Manning

Some-fn

As explained in Clojure: The Essential Reference

some-fn is a Clojure function that returns a function combining multiple predicates and returning the first truthy predicate result.

some-fn combines one or more one-argument predicates into a function that applies them to supplied values and returns the first truthy result. Evaluation short-circuits when a predicate succeeds, while nil or false results continue the search. section 2.2.9

Use some-fn when several alternative tests should be treated as one predicate:

  • Pass predicate functions as arguments; the returned function accepts zero or more values. section 2.2.9
  • Expect any return type: Clojure truthiness determines whether evaluation stops, but the successful value itself is returned. section 2.2.9
  • Avoid predicates requiring more than one argument, and expect ArityException when some-fn receives no predicates. section 2.2.9

Combining email checks

Define spam? with (some-fn any-unwanted-word? any-link? any-blacklisted-sender?). Applying it to tokenized email returns nil for clean input and "click" for a matching message, allowing when-let to raise a diagnostic exception.

How Clojure: The Essential Reference covers it

How deep it goes
Explains contracts, truthiness, short-circuiting, return values, and arity exceptions, without descending into implementation internals.
Examples
Demonstrates spam detection by combining unwanted-word, link, and blacklisted-sender checks.
What it assumes
Assumes familiarity with functions, predicates, truthiness, and higher-order function calls.
What you can do after
Apply it via Clojure function composition to preserve the first matching predicate result.
How it connects
Contrasts with every-pred and replaces repeated predicate logic that might otherwise be mistaken for the macro and.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.