简体   繁体   中英

How do I use Zip in 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?

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 (ie 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 .

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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM