Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use Zip in Clojure?

Tags:

clojure

I am very very new to clojure. The zip utility looks interesting but I cant seem to use it.

I tried

;; ZIP
(:use "zip")
(def data '[[a * b] + [c * d]])
(def dz (zip/vector-zip data))

But I am getting

java.lang.Exception: No such namespace: zip 

How do yo use external libraries?

like image 458
unj2 Avatar asked Jul 23 '09 01:07

unj2


2 Answers

You may be confusing two different ways to import code. You can do it this way:

user> (use 'clojure.zip)

Or while you're declaring a namespace in a source file:

(ns foo
  (:use clojure.zip))

The second version is a macro that is expanded into the first.

Outside of (ns), doing (:use "zip") is going to treat :use as a function and call it with "zip" as its parameter (i.e. try to use the string "zip" as a collection and look up the key :use in it), which does nothing.

clojure.zip has some functions whose names clash with things in clojure.core though, so you either have to do something like this:

user> (use '(clojure [zip :rename {next next-zip replace replace-zip remove remove-zip}]))

Or preferably this:

user> (require '(clojure [zip :as zip]))

With the latter you can refer to functions like (zip/vector-zip data) as you wish.

See the documentation for require and refer and the page talking about libs.

like image 124
Brian Carper Avatar answered Nov 19 '22 22:11

Brian Carper


I don't know much about clojure, but this little ditty seems to work:

(require '[clojure.zip :as zip]) 
(def t '(:a (:b :d) (:c :e :f))) 
(def z (zip/zipper rest rest cons t)) 
(zip/node z) 
like image 2
jsight Avatar answered Nov 20 '22 00:11

jsight