当前位置:首页 > 开发 > 编程语言 > 编程 > 正文

clojure defn的参数解构

发表于: 2013-04-16   作者:Aaron5   来源:转载   浏览次数:
摘要: 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
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 defn的参数解构

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
原文地址:http://blog.sina.com.cn/s/blog_46d0a3930100d5pt.html WebKit的结构与解构 从指定一个H
正在学习 Clojure,为了方便跑demo,把 Clojure RT 抠出来,用 rSyntaxTextarea 组件弄了下语法高亮
《Clojure编程》 基本信息 作者: (美)Chas Emerick(蔡司埃默里克) Brian Carper(布赖恩卡珀) Chris
英文原 getting started with clojure,翻译:开源中国 我将尝试教一些人(主要是Python开发者,在OS
目录: 一. 新浪的布局特点 二. 内容细节的特点 三. 其中相关的一些基础技术点 1. 常见布局方法 2.
前言 一.开发工具 - 前端四大利器 1. WebStorm 1). 岂今为止,业界公认的前端开发利器。优点: 2).
大学美术史(选修)的第一堂课上,那老师开门见山的说:知道大家认真听课的不多,下节课能来多少也
初识推荐 每天访问亚马逊的时候它不仅会在首页根据你的名字问候你,同时也会在展示一些推荐你购买的
安装IDEA,略过 选择File-Settings-plugins: 3. 选择Browse repositories 4. 找到La Clojure,右键
定义了一个类后,我们就可以new任何数量的对象。此时,将产生托管堆和栈上的内存分配,堆上将开辟一
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号