Pattern matching for map content

139 views
Skip to first unread message

Michael Jaaka

unread,
Oct 25, 2011, 5:20:13 AM10/25/11
to clo...@googlegroups.com
Hi!

Pattern matching is fine for sequence or vector destruction.
Is is possible to destruct map and make pattern machting?

For example I would like to make constraint for to some query service.
It would be done as map for example:  { :name "Tom" :surname "Jakarta" :birthDate (Date.) } or { :personalId "12312312132" }
Then I would like to dispatch this Value Object:

(defn service
  ([ {:keys [name surname]} ] (println "searching by personal data"))
 ([{:keys [personalId]} ] (println "searching by id")))


Why using map instead of specific type of VO?
Because I don't want to decleare types of VO which doesn't bring any value to domain problem.
Maybe matchure or other lib already implements it?


Ben Smith-Mannschott

unread,
Oct 25, 2011, 5:32:15 AM10/25/11
to clo...@googlegroups.com

Clojure provides only destructuring, not pattern matching. (It's my
impression that Rich is not a fan of mixing destructuring with control
flow implied by pattern matching.)

If you want pattern matching, matchure seems like just what you need.
For your particular example, take a look at matchure's defn-match.

https://github.com/dcolthorp/matchure

// Ben

Alex Ott

unread,
Oct 25, 2011, 9:33:48 AM10/25/11
to clo...@googlegroups.com
it's better to use https://github.com/clojure/core.match

> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@googlegroups.com
> Note that posts from new members are moderated - please be patient with your first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

--
With best wishes,                    Alex Ott
http://alexott.net/
Tiwtter: alexott_en (English), alexott (Russian)
Skype: alex.ott

Ben Smith-Mannschott

unread,
Oct 25, 2011, 10:02:50 AM10/25/11
to clo...@googlegroups.com
On Tue, Oct 25, 2011 at 15:33, Alex Ott <ale...@gmail.com> wrote:
> it's better to use https://github.com/clojure/core.match

Thanks, I'd forgotten about core.match.

// ben

Michael Jaaka

unread,
Oct 25, 2011, 10:50:00 AM10/25/11
to clo...@googlegroups.com
Thx, this is exactly what I need.
Reply all
Reply to author
Forward
0 new messages