Clojure Language 9: For Loop

Source: Internet
Author: User
Tags xml example

Macro for supports Loop

Next we will continue with the preceding XML example to demonstrate how to use for to traverse the sequence returned by XML-seq.

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)

Print a ^ symbol before each sequence element to facilitate resolution.

Add conditional filtering. For example, if the tag is MongoDB

user=> (for [x r :when (= :mongodb (:tag x))] x)({:tag :mongodb, :attrs nil, :content [{:tag :uri, :attrs nil, :content ["localhost"]}]})user=> 

Square brackets after for are a vector, r is the sequence returned by XML-seq, and X is the element in sequence. The next element is automatically obtained for each loop: When (...) is a condition judgment statement. When when (...) returns true, the expression after the vector is executed. Because it is only X, the element of this symbol condition is used as the return value of.

: When will not stop the loop from ending early, but: While is different.

If the value of while (...) is false, the loop exits immediately.

Therefore, an empty list is returned below.

user=> (for [x r :while (= :mongodb (:tag x))] x)()

Because the tag value of the first element is service, which is not the same as MongoDB, the loop is stopped.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.