Clojure language: Sequence

Source: Internet
Author: User

 

Sequence defines sequence as not a general list. In fact, the ISeq interface is implemented. The ISeq interface is defined as follows: [java] package clojure. lang;/*** A persistent, functional, sequence interface * <p/> * ISeqs are immutable values, I. e. neither first (), nor rest () changes * or invalidates the ISeq */public interface ISeq extends IPersistentCollection, Sequential {Object first (); ISeq next (); ISeq more (); ISeq cons (Object o);} For details about sequence operations, refer to the instructions in the official documentation: [plain] Th E Seq interface (first coll) Returns the first item in the collection. callseq on its argument. if coll is nil, returns nil. (rest coll) Returns a sequence of the items after the first. callseq on its argument. if there are no more items, returns a logical sequence for which seq returns nil. (cons item seq) Returns a new seq where item is the first element and seq is the rest. the following Operation defines a sequen Ce, and then return the first element. [Plain] user => (first (map # (print-str %) [1 2 3]) "1" returns a list of other elements except the first element: [plain] user => (rest (map # (print-str %) [1 2 3]) ("2" "3") note, # (print-str %) function converts an element into a string. Lazy sequence is actually filled with elements only when the sequence is actually used. This is called lazy sequence. Previously (the map actually returns the lazy sequence. Only when first is called can sequence containing real data be obtained. The difference between script and REPL is that in the REPL session environment, lazy sequence is invalid because REPL will forcibly fill the sequence and return it. It's just for your convenience. Xml-seq now returns to the xmlhttp: // blog.csdn.net/sheismylife/article/details/8444290xml-seqin the previous article to return to lazy sequence [plain] user => (require 'clojure. xml) nil user => (use 'clojure. xml) nil user => (parse "/home/chenshu/. xml ") {: 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"]} user => (xml-seq (parse "/home/chenshu/. xml ") ({: 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"]} {: tag: uri,: attrs nil,: content ["localhost"]} "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: port_number,: attrs nil ,: content ["7777"]} "7777" {: tag: login_timeout,: attrs nil,: content ["200"]} "200" {: tag: check_timeout ,: attrs nil,: content ["200"]} "200") user => format, and then see [plain] ({: 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"]} {: tag: uri,: attrs nil ,: content ["localhost"]} "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: port_number,: attrs nil,: content ["7777"]} "7777" {: tag: login_timeout,: attrs nil,: content ["200"]} "200" {: tag: check_timeout,: attrs nil,: content ["200"]} "200 ") we can see that the first part of the result returned by xml-seq is the result returned by parse, followed by a separate list of each xml element, which is mongodb and mongodb. uri, socket, socket. port_number, socket. login_timeout and socket. check_timeout.

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.