Clojure语言九:for循环
来源:互联网 发布:大数据分析师考试科目 编辑:程序博客网 时间:2024/05/18 20:35
宏for可以支持循环
下面继续前面的xml的例子,演示如何用for遍历xml-seq返回的sequence.
user=> (for [x r] (println "^" x))(^ {:tag :service, :attrs nil, :content [{:tag :mongodb, :attrs nil, :content [{:tag :uri, :attrs nil, :content [localhost]}]} {:tag :socket, :attrs nil, :content [{:tag :port_number, :attrs nil, :content [7777]} {:tag :login_timeout, :attrs nil, :content [200]} {:tag :check_timeout, :attrs nil, :content [200]}]}]}^ {:tag :mongodb, :attrs nil, :content [{:tag :uri, :attrs nil, :content [localhost]}]}nil ^ {:tag :uri, :attrs nil, :content [localhost]}nil ^ localhostnil ^ {:tag :socket, :attrs nil, :content [{:tag :port_number, :attrs nil, :content [7777]} {:tag :login_timeout, :attrs nil, :content [200]} {:tag :check_timeout, :attrs nil, :content [200]}]}nil ^ {:tag :port_number, :attrs nil, :content [7777]}nil ^ 7777nil ^ {:tag :login_timeout, :attrs nil, :content [200]}nil ^ 200nil ^ {:tag :check_timeout, :attrs nil, :content [200]}nil ^ 200nil nil)每个sequence元素前打印一个^符号便于分辨。
加上条件过滤,比如只取:tag 为:mongodb的
user=> (for [x r :when (= :mongodb (:tag x))] x)({:tag :mongodb, :attrs nil, :content [{:tag :uri, :attrs nil, :content ["localhost"]}]})user=>for 后面的方括号是一个vector,r是xml-seq返回的sequence,x是sequence中的元素,每次循环都自动获取下一个元素,:when(...)是条件判断语句。当when(...)返回true的时候就执行vector后面的表达式,因为这里仅仅是x,所以将这个符号条件的元素作为for的返回值。
:when 不会阻止循环提前结束,但是:while就不同了。
如果:while(...) 计算出结果为false,则循环立刻退出。
所以下面返回空list
user=> (for [x r :while (= :mongodb (:tag x))] x)()因为第一个元素的:tag值是:service,不等于:mongodb,因此循环停止。
- Clojure语言九:for循环
- clojure实战--schema for clojure
- C语言for循环
- C语言 for循环
- C语言for循环
- C语言for循环
- C语言for循环
- little crawler for clojure
- clojure实战——schema for clojure
- c语言的循环之for循环
- Clojure语言一:Symbol
- Clojure语言二:Literal
- Clojure语言三:容器
- Clojure语言五:StructMap
- Clojure语言八:Sequence
- Clojure语言十二: namespace
- 开始学习 Clojure 语言
- [转]Clojure 编程语言
- 学习 Box2D 个人笔记(三)ApplyForce
- 查看Mysql二进制日志
- CentOS 安装VNC 5 详细过程
- mips-gcc options
- 数据库---主从表操作
- Clojure语言九:for循环
- ipsec编译错误
- 优化推广目标关键词
- 环境配置
- SVN分支与合并透析
- 设计模式:门面模式
- 程序员的学习和积累
- 按 字节截取分别以GBK 和 utf-8 编码的 字符串的java程序。
- 日常生活中竹编沙发的选购常识