`

clojure defn的参数解构

阅读更多
In Clojure 1.2, you can destructure the rest argument just like you would destructure a map. This means you can do named non-positional keyword arguments. Here is an example:

user> (defn blah [& {:keys [key1 key2 key3]}] (str key1 key2 key3))
#'user/blah
user> (blah :key1 "Hai" :key2 " there" :key3 10)
"Hai there10"
user> (blah :key1 "Hai" :key2 " there")
"Hai there"
user> (defn blah [& {:keys [key1 key2 key3] :as everything}] everything)
#'user/blah
user> (blah :key1 "Hai" :key2 " there")
{:key2 " there", :key1 "Hai"}
Anything you can do while destructuring a Clojure map can be done in a function's argument list as shown above. Including using :or to define defaults for the arguments like this:

user> (defn blah [& {:keys [key1 key2 key3] :or {key3 10}}] (str key1 key2 key3))
#'user/blah
user> (blah :key1 "Hai" :key2 " there")
"Hai there10"
But this is in Clojure 1.2. Alternatively, in older versions, you can do this to simulate the same thing:

user> (defn blah [& rest] (let [{:keys [key1 key2 key3] :or {key3 10}} (apply hash-map rest)] (str key1 key2 key3)))
#'user/blah
user> (blah :key1 "Hai" :key2 " there")
"Hai there10"
and that works generally the same way.

And you can also have positional arguments that come before the keyword arguments:

user> (defn blah [x y & {:keys [key1 key2 key3] :or {key3 10}}] (str x y key1 key2 key3))
#'user/blah
user> (blah "x" "Y" :key1 "Hai" :key2 " there")
"xYHai there10"
These are not optional and have to be provided.

You can actually destructure the rest argument like you would any Clojure collection.

user> (defn blah [& [one two & more]] (str one two "and the rest: " more))
#'user/blah
user> (blah 1 2 "ressssssst")
"12and the rest: (\"ressssssst\")"
You can do this sort of thing even in Clojure 1.1. The map-style destructuring for keyword arguments only came in 1.2 though.

分享到:
评论

相关推荐

    clojure.options:改进了Clojure函数的可选参数处理

    该库提供了对带有可选参数的Clojure函数的改进处理。 该库提供了defn+opts宏,该宏定义了带有可选参数的函数。 语法类似于Clojure的defn之一,只允许使用一个函数体。 支持可选参数的文档字符串。 当defn+opts函数...

    Clojure学习教程.pdf

    可以使用可变参数来定义接受任意数量参数的函数,如 `(defn hello-count [name & args] (str "Hello " name ", you passed " (count args) " extra args"))`。 宏(Macro): Clojure中的宏是用于生成代码的代码,...

    Clojure入门教程.pdf

    - **函数定义**:使用`defn`关键字定义函数,支持匿名函数和高阶函数。 #### 三、函数式编程概念 - **纯函数**:同一组输入始终产生相同输出的函数,不依赖于外部状态,也无副作用。 - **不可变数据**:Clojure中的...

    Clojure入门教程- Clojure – Functional Programming for the JVM中文版

    - **高阶函数**: 接受函数作为参数或返回函数作为结果的函数被称为高阶函数。Clojure支持高阶函数,这极大地增强了其灵活性和表达能力。 - **不可变数据**: 在Clojure中,数据一旦创建就无法修改,这对于并发编程尤...

    Clojure编程乐趣]+clojure_programming.pdf

    Clojure是一种基于Lisp家族的函数式编程语言,它运行在Java虚拟机(JVM)上,同时也支持JavaScript和其他平台。Clojure的设计目标是提供一个高效、并发、可移植的环境,适合解决现代软件开发中的复杂问题。在这个...

    Practical Clojure.pdf

    根据提供的文件内容,我们可以提取出以下关于Clojure语言的知识点: Clojure是一种编程语言,它给作者留下了深刻的印象,并且被视为一种多用途的、全面的编程语言。作者在开始学习Clojure时,体会到了编程的乐趣,...

    clojure电子书

    《Clojure电子书》集合包含了三本关于Clojure编程的重要书籍和一个Leiningen的Windows安装程序,这对于学习和深入理解Clojure语言至关重要。Clojure是一种基于Lisp的函数式编程语言,它运行在Java虚拟机(JVM)上,...

    Clojure电子书合集1(12本)

    [2009] Programming Clojure.(Stuart Halloway).[1934356336].pdf [2010] Functional Programming with Clojure - Simple Concurrency on the JVM.(Tim Berglund, Matthew McCullough).[193650202X].pdf [2010] ...

    Programming Clojure 英文电子版

    ### 编程Clojure:全面解析与学习指南 #### 一、Clojure语言概述 《Programming Clojure》是一本深入探讨Clojure编程语言的书籍,该书由Stuart Halloway编写,出版于2009年3月,由Pragmatic Bookshelf出版社发行。...

    Professional.Clojure.1119267277

    Clear, practical Clojure for the professional programmer Professional Clojure is the experienced developer's guide to functional programming using the Clojure language. Designed specifically to meet ...

    clojure1.3.0及资料

    Clojure是一种基于Lisp的函数式编程语言,它运行在Java虚拟机(JVM)上,充分利用了Java的生态系统。Clojure的设计目标是提供一种静态类型的、并发的、内存安全的语言,同时保持Lisp的简洁性和灵活性。在这个压缩包...

    clojure eclipse

    Clojure的核心理念是函数式编程,这意味着代码被视为数据处理,函数可以作为参数传递,也可以返回新的函数。这与面向对象编程有很大的区别,函数式编程更注重状态不变性和无副作用,有助于编写可预测和易于测试的...

    Clojure Data Analysis Cookbook

    ### Clojure 数据分析实战指南 #### 一、书籍概述与背景 《Clojure 数据分析实战指南》是一本针对数据分析师和技术开发人员的实用手册。本书由 Eric Rochester 编写,旨在帮助读者通过超过110个实用案例深入了解...

    programming-clojure-3rd

    《Programming Clojure 第三版》是一本深入探讨Clojure编程语言的专业书籍,旨在帮助开发者全面理解和掌握这门基于Lisp的现代函数式编程语言。Clojure是由Rich Hickey设计的,它运行在Java虚拟机(JVM)上,同时也...

    Clojure电子书合集2(13本)

    [2013] Functional Programming Patterns in Scala and Clojure - Write Lean Programs for the JVM.(Michael Bevilacqua-Linn).[1937785475].pdf+epub.rar [2014] Clojure Cookbook - Recipes for Functional ...

Global site tag (gtag.js) - Google Analytics