Kill kwapply.

Closes #433.
This commit is contained in:
Berker Peksag 2014-01-22 22:38:53 +02:00
parent aefea557cb
commit 3528cc8278
10 changed files with 13 additions and 106 deletions

View File

@ -392,7 +392,7 @@ Parameters may have following keywords in front of them:
=> (defn print-parameters [&kwargs kwargs]
... (for [(, k v) (.items kwargs)] (print k v)))
=> (kwapply (print-parameters) {"parameter-1" 1 "parameter-2" 2})
=> (apply print-parameters [] {"parameter-1" 1 "parameter-2" 2})
parameter-2 2
parameter-1 1
@ -760,32 +760,6 @@ of import you can use.
(import [sys [*]])
kwapply
-------
`kwapply` can be used to supply keyword arguments to a function.
For example:
.. code-block:: clj
=> (defn rent-car [&kwargs kwargs]
... (cond [(in :brand kwargs) (print "brand:" (:brand kwargs))]
... [(in :model kwargs) (print "model:" (:model kwargs))]))
=> (kwapply (rent-car) {:model "T-Model"})
model: T-Model
=> (defn total-purchase [price amount &optional [fees 1.05] [vat 1.1]]
... (* price amount fees vat))
=> (total-purchase 10 15)
173.25
=> (kwapply (total-purchase 10 15) {"vat" 1.05})
165.375
lambda / fn
-----------

View File

@ -396,7 +396,7 @@ The same thing in Hy::
[1 2 None 42]
=> (optional_arg 1 2 3 4)
[1 2 3 4]
=> (kwapply (optional_arg)
=> (apply optional_arg []
... {"keyword1" 1
... "pos2" 2
... "pos1" 3
@ -404,7 +404,7 @@ The same thing in Hy::
...
[3, 2, 1, 4]
See how we use kwapply to handle the fancy passing? :)
See how we use apply to handle the fancy passing? :)
There's also a dictionary-style keyword arguments construction that
looks like:
@ -479,7 +479,7 @@ In Hy:
.. code-block:: clj
(defclass Customer [models.Model]
[[name (kwapply (models.CharField) {"max_length" 255})]
[[name (apply models.CharField [] {"max_length" 255})]
[address (models.TextField)]
[notes (models.TextField)]])

View File

@ -11,7 +11,7 @@
(import [urllib2 [urlopen]])))
(defn get-rss-feed-name [tumblr]
(kwapply (.format "http://{tumblr}.tumblr.com/rss") {"tumblr" tumblr}))
(.format "http://{0}.tumblr.com/rss" tumblr))
(defn get-rss-feed [tumblr]
(.parse etree (urlopen (get-rss-feed-name tumblr))))

View File

@ -12,7 +12,7 @@
(defn get-legislators [state]
(kwapply (.legislators openstates) {"state" state}))
(apply openstates.legislators [] {"state" state}))
(defn get-party-breakdown [state]

View File

@ -3,7 +3,7 @@
(defmacro route-with-methods [name path methods params &rest code]
"Same as route but with an extra methods array to specify HTTP methods"
`(let [[deco (kwapply (.route app ~path)
`(let [[deco (apply app.route [~path]
{"methods" ~methods})]]
(with-decorator deco
(defn ~name ~params

View File

@ -46,6 +46,6 @@
(.disable ~g!hy-pr)
(setv ~g!hy-s (StringIO))
(setv ~g!hy-ps
(.sort-stats (kwapply (.Stats pstats ~g!hy-pr) {"stream" ~g!hy-s})))
(.sort-stats (apply pstats.Stats [~g!hy-pr] {"stream" ~g!hy-s})))
(.print-stats ~g!hy-ps)
(print (.getvalue ~g!hy-s))))

View File

@ -171,25 +171,6 @@
(let ~(HyList (map (fn [x] `[~x (gensym (slice '~x 2))]) syms))
~@body))))
(defmacro kwapply [call kwargs]
"Use a dictionary as keyword arguments"
(let [[-fun (car call)]
[-args (cdr call)]
[-okwargs `[(list (.items ~kwargs))]]]
(while (= -fun "kwapply") ;; join any further kw
(if (not (= (len -args) 2))
(macro-error
call
(.format "Trying to call nested kwapply with {0} args instead of 2"
(len -args))))
(.insert -okwargs 0 `(list (.items ~(car (cdr -args)))))
(setv -fun (car (car -args)))
(setv -args (cdr (car -args))))
`(apply ~-fun [~@-args] (dict (sum ~-okwargs [])))))
(defmacro-alias [defn-alias defun-alias] [names lambda-list &rest body]
"define one function with several names"
(let [[main (first names)]

View File

@ -353,17 +353,6 @@ def test_ast_non_decoratable():
cant_compile("(with-decorator (foo) (* x x))")
def test_ast_non_kwapplyable():
""" Ensure kwapply breaks """
code = tokenize("(kwapply foo bar)")
code[0][2] = None
try:
hy_compile(code, "__main__")
assert True is False
except HyCompileError:
pass
def test_ast_lambda_lists():
"""Ensure the compiler chokes on invalid lambda-lists"""
cant_compile('(fn [&key {"a" b} &key {"foo" bar}] [a foo])')

View File

@ -1,27 +0,0 @@
# Copyright (c) 2013 Nicolas Dandrimont <nicolas.dandrimont@crans.org>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
from .test_ast import can_compile, cant_compile
def test_macro_nested_kwapply():
"Make sure nested kwapply compile correctly"
can_compile("(kwapply (kwapply (foo) bar) baz)")
cant_compile("(kwapply (kwapply (foo)) bar)")

View File

@ -199,20 +199,10 @@
(defn test-kwargs []
"NATIVE: test kwargs things."
(assert (= (kwapply (kwtest) {"one" "two"}) {"one" "two"}))
(assert (= (apply kwtest [] {"one" "two"}) {"one" "two"}))
(setv mydict {"one" "three"})
(assert (= (kwapply (kwtest) mydict) mydict))
(assert (= (kwapply (kwtest) ((fn [] {"one" "two"}))) {"one" "two"}))
(assert (= (kwapply
(kwapply
(kwapply
(kwapply
(kwapply (kwtest) {"x" 4})
mydict)
{"x" 8})
{"x" (- 3 2) "y" 2})
{"y" 5 "z" 3})
{"x" 1 "y" 5 "z" 3 "one" "three"})))
(assert (= (apply kwtest [] mydict) mydict))
(assert (= (apply kwtest [] ((fn [] {"one" "two"}))) {"one" "two"})))
(defn test-apply []
@ -785,8 +775,8 @@
"NATIVE: test &key function arguments"
(defn foo [&key {"a" None "b" 1}] [a b])
(assert (= (foo) [None 1]))
(assert (= (kwapply (foo) {"a" 2}) [2 1]))
(assert (= (kwapply (foo) {"b" 42}) [None 42])))
(assert (= (apply foo [] {"a" 2}) [2 1]))
(assert (= (apply foo [] {"b" 42}) [None 42])))
(defn test-optional-arguments []