A Clojure macro for defining functions with Erlang/Elixir-style parameter pattern matching.
defun is a Clojure macro that enables defining functions with parameter pattern matching, similar to Erlang or Elixir. It allows developers to write multiple function clauses that match different argument patterns, eliminating the need for explicit conditional statements like `if` or `cond`. The macro works in both Clojure and ClojureScript, leveraging Clojure's core.match library for pattern matching capabilities.
Clojure and ClojureScript developers who want to write more expressive, declarative functions using pattern matching, especially those familiar with Erlang or Elixir's function definition style.
Developers choose defun because it brings the elegance and clarity of Erlang-style pattern matching to Clojure, reducing boilerplate code for multi-arity functions and making recursive definitions more intuitive, all while maintaining compatibility with existing Clojure ecosystems.
A macro to define clojure functions with parameter pattern matching just like erlang or elixir.
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Enables writing multiple function clauses that match argument patterns directly, reducing boilerplate from if or cond statements, as shown in the say-hi example.
Leverages Clojure's core.match to support literals, sequences, vectors, maps, and more, allowing complex matching logic, demonstrated in test examples.
Makes recursive functions intuitive with pattern-matched base cases, such as in count-down and accum, eliminating manual condition checks.
Supports private functions with defun-, anonymous functions with fun, and local functions with letfun, mirroring Clojure's standard constructs for versatility.
The macro expands to core.match code, which is slower than native Clojure function dispatch, as benchmarking shows accum-defun is about twice as slow as accum-defn.
Requires core.match library, adding an extra dependency that can lead to version conflicts or increased project maintenance complexity.
Only applies to defining functions, not general pattern matching in other code contexts, which might restrict utility compared to more integrated solutions.