﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-永恒瞬间-随笔分类-Clojure</title><link>http://www.blogjava.net/cdredfox/category/49262.html</link><description>一个小小程序员的信口开河</description><language>zh-cn</language><lastBuildDate>Fri, 29 Jul 2011 17:02:26 GMT</lastBuildDate><pubDate>Fri, 29 Jul 2011 17:02:26 GMT</pubDate><ttl>60</ttl><item><title>Clojure Collections 集合类型简要介绍</title><link>http://www.blogjava.net/cdredfox/archive/2011/07/30/355375.html</link><dc:creator>永恒瞬间</dc:creator><author>永恒瞬间</author><pubDate>Fri, 29 Jul 2011 17:01:00 GMT</pubDate><guid>http://www.blogjava.net/cdredfox/archive/2011/07/30/355375.html</guid><wfw:comment>http://www.blogjava.net/cdredfox/comments/355375.html</wfw:comment><comments>http://www.blogjava.net/cdredfox/archive/2011/07/30/355375.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/cdredfox/comments/commentRss/355375.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/cdredfox/services/trackbacks/355375.html</trackback:ping><description><![CDATA[<div>&nbsp; &nbsp; Clojure语言本身自已有提供了四种集合类型，List,Vector,Set,Map.同时它也可以使用Java的集合类型砂，但是这种方式目前并不推荐使用，因为Clojure集合类型和Java语言的集合类型还是有很大区别的，主要体现在 集合内容不可改变，集合的内容可以多种类型以及持久化。持久化意味着当你在对一个集合的操作都会产生一个新的集合对象（如：插入，删除......)，而老的集合对象依然是存在的。<br /><br /><strong>&nbsp; &nbsp;List</strong><br />&nbsp; &nbsp;List的可以使用如下方式定义:<br />&nbsp; &nbsp; &nbsp;(def list1 '("aa" "bb" "cc"))<br />&nbsp; &nbsp; &nbsp;(def list1 (list "aa" "bb" "cc"))<br />&nbsp; &nbsp; &nbsp;(def list1 (quote ("aa" "bb" "cc")))<br />&nbsp; &nbsp;以上三种方式都可以定义一个List,和Java中一样List是有序的。<br /><br /><strong>&nbsp; &nbsp;Vector</strong><br />&nbsp; &nbsp;vector可以使用如下方式定义：<br />&nbsp; &nbsp; &nbsp;(def v ["v1" "v2"])<br />&nbsp; &nbsp; &nbsp;(def v (vector "v1" "v2"))<br />&nbsp; &nbsp;同样vector也是有序的，在Clojure的方法参数定义就是使用的Vector,比如定义一个类以于Java的public void say(String name)用Clojure则使用如下的方式<br />(defn say [name])<br />&nbsp; &nbsp;&nbsp;<br /><strong>&nbsp; &nbsp;Set</strong><br />&nbsp; &nbsp;set可以使用如下方式定义:<br />&nbsp; &nbsp;(def s {"aa" "bb" "cc"})<br />&nbsp; &nbsp;(def s #{"aa" "bb" "cc"})<br />&nbsp; &nbsp;(def s (hash-set "aa" "bb" "cc"))<br />&nbsp;<br />&nbsp; &nbsp; Set分可有序set和无序set,上面示例中的"hash-set"就是代表 无序set,而有序set则可用 sorted-set来定义.<br />&nbsp; &nbsp; 可以用函数 contains? 来检测一个set中是否存在某个元素 (contains? s "aa") 如果s中存在"aa"元素，则返回true,否则返回false.<br />&nbsp; &nbsp; contains?函数可以使用在Set和Map无素上。<br />&nbsp; &nbsp; 同Java中一样。Set中的元素是唯一的。<br /><br /><strong>&nbsp; &nbsp; Map</strong><br />&nbsp; &nbsp; map可以使用如下方式定义:<br />&nbsp; &nbsp; (def m {"k1" "v1","k2" "v2"})<br />&nbsp; &nbsp; (def m (hash-map&nbsp;"k1" "v1","k2" "v2"))<br />&nbsp; &nbsp; 同样，map也分为有序和无序的，如果想申明有序的map则使用 sorted-map即可。<br />&nbsp; &nbsp; 可以使用函数keys,vals得到键集合和值集合.(keys m)<br />&nbsp; &nbsp; get函数可以根据key取得相应的val值。(get m "k1") 返回的是"v1"<br />&nbsp; &nbsp; get也可以用在 其它的集合中，比如从Vector中取元素的值.(get v 1) 代表取出集合v中的第2个无素。注意，集合的编号是从0开始的。<br /><br /><br />&nbsp; &nbsp;本部分主要简单介绍了一下Clojure中的集合的基本用法。后续会继续介绍更多的一些东西。<br /><br />&nbsp;<br />&nbsp;&nbsp;</div><img src ="http://www.blogjava.net/cdredfox/aggbug/355375.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/cdredfox/" target="_blank">永恒瞬间</a> 2011-07-30 01:01 <a href="http://www.blogjava.net/cdredfox/archive/2011/07/30/355375.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>