Clojure parameter decomposition, binding forms (destructuring)

Source: Internet
Author: User

The let parameter decomposition of clojure is clearly described in this document. For example

(let [[a b c & d :as e] [1 2 3 4 5 6 7]]  [a b c d e]) ->[1 2 3 (4 5 6 7) [1 2 3 4 5 6 7]]

This let can be used as a value assignment in other languages. For example, the int I = 0 in Java also supports multi-variable assignment. For example, Perl's my ($ A $ B) = @ A (I don't know if it's right, that's what it means ). However, clojure changes a lot. In the above example, the final value assignment result is-> shown later.

(let [[a b & c :as str] "asdjhhfdas"]  [a b c str]) ->[\a \s (\d \j \h \h \f \d \a \s) "asdjhhfdas"]

The same is true for strings. clolure does not contain seq.

(let [{a :a, b :b, c :c, :as m :or {a 2 b 3}}  {:a 5 :c 6}]  [a b c m]) ->[5 3 6 {:c 6, :a 5}]

The above deconstruct map,: or means that if it is not found, use the default, B is obviously not in {: A 5: c 6}, so the final value is 3.


Deconstruct of function parameters:

In general, the function will specify parameters when defining them. For example, the following parameters, according to the number of parameters displayed when you call this function, I write here to receive a maximum of two parameters, according to The clojure document description, you can have up to 20 parameters (only in this explicit declaration mode ).

(defn- fixargs ([] 0) ([_] 1) [_ _] 2))

If you execute (fixargs: A: B: C), an error is returned. Of course, I can define a function like this: no matter how many parameters you input, this function will display the number. But at least two parameters are required. An error is returned if there are less than two parameters. If you delete a B, you can accept any (including none) number.

(defn- anyargs    [a b & c]    (+ 2 (count c)))

If you occasionally see the following types of function parameter definitions, you may be confused, but clojure can repl, so you will know the results after testing.

(defn- hashp-p [a & {:b b}]    [a b])

(Hash-P 1: B 2) --------------> [1 2]

That is to say, you pass in three parameters, a married the first 1, and the remaining two are: B 2. It can be seen that clojure makes up the remaining two to form a map, the corresponding deconstruct is 2.

If you think [& C] will automatically convert the vector into a map, it will be wrong again. Let's try again.

(Defn F-N [& C] (: B c); then call (F-N: B 2) ----------> Nil

Clojure's code has no rules at all. After getting used to it, it actually follows a very simple and unified principle, which is form, evaluate, and so on. (Of course, as mentioned in clojure, clojure holds a pragmatic attitude. Therefore, it introduces VaR, ref, atom, and agent to solve the corresponding problems in a natural way)

Clojure parameter decomposition, binding forms (destructuring)

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.