Publicidad:
Terra
La Coctelera

un rato de sol

i will work harder, ja ja ja, no ahora en serio.

28 Septiembre 2009

Pattern matching support in clojure

Lisp is loved and hated because of its syntax, but the fact is that the homogenous treatement of data and code in the language has allowed Lisp to evolve and incorporate into the language those features considered necessary by their users without modifications in the core language.
Clojure, as any other incarnation of Lisp, has inherited this extensibility capacities and takes advantage of it in different ways.
This post is introduces a library for adding pattern matching support in the Clojure in about 100 lines of code.

You can download the library from my github repository: http://github.com/antoniogarrote/clj-tuples

The library adds two constructs 'match' and 'case' to test patterns against values, and execute clojure forms associated to a succesful match.

These are some examples of how to use the library, any user of Haskell or Erlang will recognize the syntax inmediately:

 user>(tuples/match true true nil)
 nil
 
 user>(tuples/match true false nil)
 matching exception
 
 user>(tuples/match a true a)
 true
 
 
 user>(tuples/match a true (not a))
 false
 
 user>(def v '(1 2 (3 4) 5))
 #'user/v
 
 user>(tuples/match (z u (s1 s2) t) v (println s1))
 3
 
 user>;you can use _ if you are not interested in a part of the value
 nil
 user>(tuples/match (_ _ s _) v s)
 (3 4)
 
 user>(def *extracted* 
           (tuples/match (z u (s1 s2) t) (1 2 (3 4) 5) s2) s2))
 #'user/*extracted*
 user>*extracted*
 4
 
 user>(tuples/match (3 u (s1 s2) t) (1 2 (3 4) 5) (println s2))
 matching exception
 
 user>(def vu '(2 2))
 #'user/vu
 
 user> (tuples/case (vu 2)
                    (z 1) (println (str "a " z))
                    (z 2) (println (str "b " z))
                    t     :error)
 b (2 2)
 
 user>(tuples/case (vu 3)
                   (z 1) (println (str "a " z))
                   (z 2) (println (str "b " z))
                   t     :error)
 :error
 

The library is not meant to be complete, it only works with lists for doing deep matching but is a good example of how Clojure can be extended.

Hope someone finds it useful!

servido por Antonio 6 comentarios compártelo

6 comentarios · Escribe aquí tu comentario

jneira

jneira dijo

beautiful library, im going to watch it closely ;-) ..

29 Septiembre 2009 | 03:09 PM

replica handbags

replica handbags dijo

Cnreplicas.com was established in 2003. We only sell the top grade replica bags, some of them are genuine replica handbags, yet the price is far lower than the authentic designers want you to pay. We lead you to a genuine pool of bags : collections in wide range: replica bags, clutches bag, tote bags, purses and wallets, the hottest brands you can find like Louis Vuitton Handbags

10 Noviembre 2009 | 08:35 AM

xcvb

xcvb dijo

aion gold directions of aion power levelingShri Govind Maheshwari, Shri Naveen buy aion gold.

18 Diciembre 2009 | 05:04 AM

safdsafd

safdsafd dijo

come !come my dear friends .come here buy fake rolex .cheap fake rolex at here .heave the replica rolex

3 Enero 2010 | 01:02 PM

safdsafd

safdsafd dijo

come !come my dear friends .come here buy fake rolex .cheap fake rolex at here .heave the replica rolex

3 Enero 2010 | 01:02 PM

generic propecia

generic propecia dijo

The library adds two constructs 'match' and 'case' to test patterns against values, and execute clojure forms associated to a succesful match.

2 Febrero 2010 | 05:49 PM

Escribe tu comentario


Sobre mí

Avatar de Antonio

un rato de sol

Barcelona/Salamanca, España
ver perfil »
contacto »
Trabajador del metal y del acero, en la gloriosa XING AG, escribo software con el que poder ganarme el jornal. En mi tiempo libre sigo tecleando código de bonitos colores a medio camino entre lo sublime y lo terrible. Últimamente me gustan mucho los gatos.

Fotos

Antonio Garrote Hernández todavía no ha subido ninguna foto.

¡Anímale a hacerlo!

Buscar

suscríbete

Selecciona el agregador que utilices para suscribirte a este blog (también puedes obtener la URL de los feeds):

¿Qué es esto?

Crea tu blog gratis en La Coctelera