Created
November 22, 2017 10:08
-
-
Save cassc/71e638aecdab482a9ba540e78ce0407e to your computer and use it in GitHub Desktop.
com.rpl.specter-example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(use 'com.rpl.specter) | |
;; 过滤元素 | |
(filter odd? [1 2 3 4 5 6]) ;; (1 3 5) | |
(select [ALL odd?] [1 2 3 4 5 6]) ;; [1 3 5] | |
;; 选择奇数位的元素 | |
(select [INDEXED-VALS (comp odd? first) LAST] [:a :b :c :d :e]) ;; [:b :d] | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;; 复杂选择 | |
;; 此示例输入是一条常见的hiccup数据,其中包含网站列表,每个网站有名称、网址、成立日期及简介内容。 | |
;; 我们希望将此输入转换为clojure数据结构,即提取名称、网址、成立日期及简介再转换为map队列输出 | |
(def root {:content | |
[{:type :element, | |
:attrs nil, | |
:tag :h3, | |
:content | |
[{:type :element, | |
:attrs | |
{:title "Permanent Link to Bcelebrated", | |
:href | |
"http://www.thedigitalbeyond.com/online-services-list/bcelebrated/", | |
:rel "bookmark"}, | |
:tag :a, | |
:content ["Bcelebrated"]}]} | |
"\n" | |
{:type :element, | |
:attrs | |
{:href "http://www.bcelebrated.com", | |
:name "Link to Bcelebrated"}, | |
:tag :a, | |
:content ["www.bcelebrated.com"]} | |
{:type :element, | |
:attrs nil, | |
:tag :p, | |
:content | |
[{:type :element, | |
:attrs nil, | |
:tag :strong, | |
:content ["Founded: "]} | |
"July, 2009"]} | |
{:type :element, | |
:attrs nil, | |
:tag :p, | |
:content | |
["Bcelebrated enables members to create a multi-media website that will become their autobiographical memorial site when the time comes."]} | |
{:type :element, | |
:attrs nil, | |
:tag :h3, | |
:content | |
[{:type :element, | |
:attrs | |
{:title "Permanent Link to Boxego", | |
:href | |
"http://www.thedigitalbeyond.com/online-services-list/boxego/", | |
:rel "bookmark"}, | |
:tag :a, | |
:content ["Boxego"]}]} | |
"\n" | |
{:type :element, | |
:attrs | |
{:href "http://www.boxego.com", | |
:name "Link to Boxego"}, | |
:tag :a, | |
:content ["www.boxego.com"]} | |
{:type :element, | |
:attrs nil, | |
:tag :p, | |
:content | |
[{:type :element, | |
:attrs nil, | |
:tag :strong, | |
:content ["Founded: "]} | |
"2013"]} | |
{:type :element, | |
:attrs nil, | |
:tag :p, | |
:content | |
["Boxego is a private journal that can be shared privately and socially, now and in the future."]}]}) | |
;; 取标题 h3 | |
(select [:content ALL (pred #(= :h3 (:tag %))) :content ALL :content FIRST] root) ;; ["Bcelebrated" "Boxego"] | |
;; 取网站链接 | |
(select [:content ALL (pred #(= :a (:tag %))) :attrs :href] root) ;; ["http://www.bcelebrated.com" "http://www.boxego.com"] | |
;; 取时间与文章描述 | |
(select [:content ALL (pred #(= :p (:tag %))) :content LAST] root) ;; ["July, 2009" "Bcelebrated enables members to create a multi-media website that will become their autobiographical memorial site when the time comes." "2013" "Boxego is a private journal that can be shared privately and socially, now and in the future."] | |
;; 同时取上面三类内容,使用multi-path同时选择多个路径 | |
(def paths [[(pred #(= :h3 (:tag %))) :content ALL :content FIRST] | |
[(pred #(= :a (:tag %))) :attrs :href] | |
[(pred #(= :p (:tag %))) :content LAST]]) | |
(select [:content ALL (apply multi-path paths)] root) ;; ["Bcelebrated" "http://www.bcelebrated.com" "July, 2009" "Bcelebrated enables members to create a multi-media website that will become their autobiographical memorial site when the time comes." "Boxego" "http://www.boxego.com" "2013" "Boxego is a private journal that can be shared privately and socially, now and in the future."] | |
;; 下面我们来看如何转换成我们期望的map集合的结构。这里要用到transform | |
(->> | |
(select [:content ALL (apply multi-path paths)] root) | |
(partition 3) | |
(transform [ALL] (fn [[title url description]] {:title title :url url :description description}))) ;; ({:title "Bcelebrated", :url "http://www.bcelebrated.com", :description "July, 2009"} {:title "Bcelebrated enables members to create a multi-media website that will become their autobiographical memorial site when the time comes.", :url "Boxego", :description "http://www.boxego.com"}) | |
;; recursion | |
(def tree [1 [2 [[3]] 4] [[5] 6] [7] 8 [[9]]]) | |
(def TREE-VALUES | |
(recursive-path [] p | |
(if-path vector? | |
[ALL p] | |
STAY))) | |
(select TREE-VALUES [1 [2 [3 4] 5] [[6]]]) ;; [1 2 3 4 5 6] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment