Skip to content

antoniogarrote/clj-tuples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Suppport for pattern matching in Clojure.

== Installation

- The project uses Leiningen to manage the build.
- The project is published at Clojars

To build from source, after installing leiningen execute:

$lein deps
$lein compile-java
$lein compile
$lein jar

== Use

There are two main macros for pattern matching: 'match' and 'case'.

The macro 'match' accepts a pattern a value and an action, matches the pattern against the value,
extracts the component values and executes the action form:

;; (tuples/match pattern value action)
>(clj-tuples/match (a b (c d) e) (1 2 (3 4) 5) (+ b e))
7


The macro 'case' does pattern matching of several patterns and actions agains a single value:

;; (tuples/case value (pattern1 action1) (pattern2 action2) ... (patternn actionn))
>(def *test* '(:ok 2))
>(clj-tuples/case *test*
              (s 1) (do (println (str "the state is not ok: " s)) s)
              (s 2) (do (println (str "the state is ok: " s)) s)
              t     (println (str "unknown value")))
the state is ok: :ok
:ok

You can use symbols starting with '_' for the parts of the value you are not interested:

(clj-tuples/match (_ _ x) (1 2 3) x)
>3
(clj-tuples/match (_a _b x) (1 2 3) x)
>3

== Tests

There are some tests included in the project. You can run them using Leiningen:

$lein test

== Changelog

- version 0.1   (09/28/2009): First version.
- version 0.2   (09/29/2009): Added support for matching vectors.
- version 0.3   (09/29/2009): Initial support for matching maps.
- version 0.3.1 (10/02/2009): Fixed error in exception handling. Now errors not involving
                              pattern matching are not cought.
-version 0.3.2  (10/05/2009): Fixed error with comparing patterns consisting of lists with
                              different number of elements.
-version 0.3.3  (12/08/2009): Packaged for leiningen.