Merge pull request #642 from microamp/issue-639

Fix #639: Shadow '+' to handle string/list concatenation
This commit is contained in:
Morten Linderud 2014-08-31 22:52:46 +02:00
commit fbfb51fb61
3 changed files with 22 additions and 7 deletions

View File

@ -26,9 +26,12 @@
(defn + [&rest args]
"Shadow + operator for when we need to import / map it against something"
(if (= (len args) 0)
0
(sum args))) ; shortcut here.
(let [[count (len args)]]
(if (zero? count)
(raise (TypeError "Need at least 1 argument to add/concatenate"))
(if (= count 1)
(get args 0)
(reduce operator.add args)))))
(defn - [&rest args]

View File

@ -12,6 +12,7 @@ from .native_tests.when import * # noqa
from .native_tests.with_decorator import * # noqa
from .native_tests.core import * # noqa
from .native_tests.reader_macros import * # noqa
from .native_tests.shadow import * # noqa
from .native_tests.with_test import * # noqa
from .native_tests.contrib.anaphoric import * # noqa
from .native_tests.contrib.loop import * # noqa

View File

@ -1,11 +1,22 @@
(defn test-shadow-addition []
"NATIVE: test shadow addition"
(let [[x +]]
(assert (= (x) 0))
(assert (try
(x)
(catch [TypeError] True)
(else (throw AssertionError))))
(assert (= (x 1 2 3 4) 10))
(assert (= (x 1 2 3 4 5) 15))))
(assert (= (x 1 2 3 4 5) 15))
; with strings
(assert (= (x "a")
"a"))
(assert (= (x "a" "b" "c")
"abc"))
; with lists
(assert (= (x ["a"])
["a"]))
(assert (= (x ["a"] ["b"] ["c"])
["a" "b" "c"]))))
(defn test-shadow-subtraction []