Clojure中的解构参数 [英] Destructuring argument in Clojure

查看:100
本文介绍了Clojure中的解构参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新手在这里很喜欢Clojure 。因此,我有一个HTTP路由:

Newbie enjoying Clojure a lot here. So, I have an HTTP route:

 (POST   "/login"    request   (post-login request))

此处, request是其中包含许多http内容的地图。和登录后功能:

here, "request" is a map with a lot of http things inside. And the "post-login" function:

(defn post-login
  ;; Login into the app
  [{{email "email" password "password"} :form-params session :session :as req}]
    (let [user (modusers/get-user-by-email-and-password email password)]
     ;; If authenticated
     (if-not (nil? (:user user))
      (do
       (assoc (response/found "/")
         :session (assoc session :identity (:user user)) :flash "Bitte schön!"))
       (assoc (response/found "/login") :flash "Etwas stimmt nicht mit deinem Zugriffsprozess"))))

但我不明白 request是如何被破坏的。现在如何在函数中使用 email和 password?以及:as req是什么意思?这个技术有名字吗?

but I don't understand how "request" is being destructuring. How "email" and "password" are now available inside the function?, and what ":as req" means? Is there a name to this "technique"?

推荐答案

如果评论中仍不清楚,这里有一个快速的答案:

If it's still not clear from the comments, here's a quick answer:

您的登录后函数接收一个参数,该参数是表示HTTP请求的映射。该参数在特殊的:as 关键字的帮助下存储在 req 中。
因此(defn post-login [req] ...)(defn post-login [{:as req}])。 ..)完全相同。

Your post-login function receives a single argument which is a map representing HTTP request. This argument is stored in req with the help of the special :as keyword. So (defn post-login [req] ...) and (defn post-login [{:as req}] ...) are exactly the same.

地图解构工作的一种(一种)方法是,在右侧提供地图键以及应该绑定到左侧的东西,例如 [{form-params:form-params:as req}] 。现在, req 映射中的:form-params 键将绑定到 form-params 在函数体内可用。

A (one) way that map destructuring works is that you provide a map key on the right side and the thing to which it should be bound on the left side, e.g. [{form-params :form-params :as req}]. Now the :form-params key in req map will be bound to the form-params symbol available inside the function body.

嵌套解构时,事情变得更加有趣。在您的情况下, [{{email email password password}:form-params:as req} 仅表示:form-params请求中的键将绑定到 {email email密码 password} ,这本身也是一种破坏。最终,:form-params 中的电子邮件 键将绑定到 email 符号和密码相同。
和以前一样,如果您需要访问整个请求(而不是其中的某些参数),则可以通过:as 来获取。

Things get more interesting when you do "nested" destructuring. In your case, [{{email "email" password "password"} :form-params :as req} just means that :form-params key inside the request will be bound to {email "email" password "password"} which again is a destructuring itself. Eventually, the "email" key in :form-params will be bound to the email symbol and the same for password. As before, if you need access to the whole request (not just some params inside it) you may get that via :as.

注意:您可以使用另一种语法(一种快捷方式):

[{{:strs [email password]] :form-params session:session:as req}]

Note: there's an alternative syntax which you can use (a kind of shortcut): [{{:strs [email password]} :form-params session :session :as req}]

这篇关于Clojure中的解构参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆