Clojure函数的分解参数,同时保留原始值。 [英] Destructure parameter of a Clojure function while keeping the original value.
本文介绍了Clojure函数的分解参数,同时保留原始值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您可以分解功能参数,但仍然可以使用原始参数吗?我现在的操作方式是在函数体内使用 let
表单,但是我想知道是否存在更简洁的方法。
Can you destructure a function parameter but still have the original available for use? The way I'm doing it now is just using a let
form inside the function body, but I wondering if there was a terser way of doing it.
推荐答案
类似:as
的方法也适用于函数:
Seems like :as
works for functions too:
具有个向量
(defn test [[x y :as v]]
{:x x :y y :v v})
(test [1 2 3 4])
=> {:x 1 :y 2 :v [1 2 3 4]}
有 hash-map
(defn test2 [{x :x y :y :as m}]
{:x x :y y :m m})
(test2 {:x 1 :y 2 :z 3})
=> {:x 1 :y 2 :m {:x 1 :y 2 :z 3}}
请参阅以下出色的博客文章: http://blog.jayfields.com/2010/07/clojure-destructuring。 html
See this terrific blog post: http://blog.jayfields.com/2010/07/clojure-destructuring.html
这篇关于Clojure函数的分解参数,同时保留原始值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文