[英]How do I use Zip in Clojure?
I am very very new to clojure. 我对clojure非常新。 The zip utility looks interesting but I cant seem to use it.
zip实用程序看起来很有趣但我似乎无法使用它。
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. 在
(ns)
,执行(:use "zip")
将要处理:use
作为函数:use
并以"zip"
作为参数调用它(即尝试使用字符串"zip"
作为集合并查找key :use
in it),什么都不做。
clojure.zip
has some functions whose names clash with things in clojure.core
though, so you either have to do something like this: clojure.zip
有一些函数,其名称与clojure.core
东西冲突,所以你要么做这样的事情:
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. 对于后者,您可以根据需要引用
(zip/vector-zip data)
等函数。
See the documentation for require and refer and the page talking about libs . 请参阅require和refer的文档以及有关libs的页面。
I don't know much about clojure, but this little ditty seems to work: 我对clojure知之甚少,但这个小曲子似乎有效:
(require '[clojure.zip :as zip])
(def t '(:a (:b :d) (:c :e :f)))
(def z (zip/zipper rest rest cons t))
(zip/node z)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.