Clojure language 11: Map Functions

Source: Internet
Author: User

Map functions are quite special. First, let's look at a simple function:

user=> (def f (fn [x] (+ 2 x)))#'user/fuser=> (map f [2 4 7])(4 6 9)

Define an F function, accept a parameter, and then return the result after + 2.

Map accepts two parameters. The first is the F function, and the second is an array. Use the three elements in the array to call the F function in sequence. The result of each call is added to a list and returned.

Map document:

-------------------------clojure.core/map([f coll] [f c1 c2] [f c1 c2 c3] [f c1 c2 c3 & colls])  Returns a lazy sequence consisting of the result of applying f to the  set of first items of each coll, followed by applying f to the set  of second items in each coll, until any one of the colls is  exhausted.  Any remaining items in other colls are ignored. Function  f should accept number-of-colls arguments.nil

There is also a more advanced usage, such:

user=> (def f (fn [x y] (+ x y)))#'user/fuser=> (map f [2 4] [9 0])(11 4)

Here, the F function accepts two parameters, so the number of sets after map must be two. The running result should be:

(F (2 9) (f (4 0 )))

That is, (11 4)

This map is a little similar to the map reduce idea.

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.