Merge branch 'master' of github.com:hylang/hy into xor
Conflicts: hy/core/language.hy
This commit is contained in:
commit
ec3874377b
1
AUTHORS
1
AUTHORS
@ -63,3 +63,4 @@
|
||||
* Nicolas Pénet <z.nicolas@gmail.com>
|
||||
* Adrià Garriga Alonso <adria@monkingme.com>
|
||||
* Antony Woods <antony@teamwoods.org>
|
||||
* Matthew Egan Odendahl <github.gilch@xoxy.net>
|
||||
|
@ -657,7 +657,7 @@ del
|
||||
=> (setv test (list (range 10)))
|
||||
=> test
|
||||
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||
=> (del (slice test 2 4)) ;; remove items from 2 to 4 excluded
|
||||
=> (del (cut test 2 4)) ;; remove items from 2 to 4 excluded
|
||||
=> test
|
||||
[0, 1, 4, 5, 6, 7, 8, 9]
|
||||
=> (setv dic {"foo" "bar"})
|
||||
@ -1243,35 +1243,35 @@ expression.
|
||||
{1, 3, 5}
|
||||
|
||||
|
||||
slice
|
||||
cut
|
||||
-----
|
||||
|
||||
``slice`` can be used to take a subset of a list and create a new list from it.
|
||||
The form takes at least one parameter specifying the list to slice. Two
|
||||
``cut`` can be used to take a subset of a list and create a new list from it.
|
||||
The form takes at least one parameter specifying the list to cut. Two
|
||||
optional parameters can be used to give the start and end position of the
|
||||
subset. If they are not supplied, the default value of ``None`` will be used
|
||||
instead. The third optional parameter is used to control step between the elements.
|
||||
|
||||
``slice`` follows the same rules as its Python counterpart. Negative indices are
|
||||
``cut`` follows the same rules as its Python counterpart. Negative indices are
|
||||
counted starting from the end of the list. Some example usage:
|
||||
|
||||
.. code-block:: clj
|
||||
|
||||
=> (def collection (range 10))
|
||||
|
||||
=> (slice collection)
|
||||
=> (cut collection)
|
||||
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||
|
||||
=> (slice collection 5)
|
||||
=> (cut collection 5)
|
||||
[5, 6, 7, 8, 9]
|
||||
|
||||
=> (slice collection 2 8)
|
||||
=> (cut collection 2 8)
|
||||
[2, 3, 4, 5, 6, 7]
|
||||
|
||||
=> (slice collection 2 8 2)
|
||||
=> (cut collection 2 8 2)
|
||||
[2, 4, 6]
|
||||
|
||||
=> (slice collection -4 -2)
|
||||
=> (cut collection -4 -2)
|
||||
[6, 7]
|
||||
|
||||
|
||||
|
@ -672,6 +672,21 @@ Returns ``True`` if *x* is odd. Raises ``TypeError`` if
|
||||
False
|
||||
|
||||
|
||||
.. _partition-fn:
|
||||
|
||||
partition
|
||||
---------
|
||||
|
||||
Usage: ``(partition n coll)``
|
||||
|
||||
Chunks coll into tuples of length *n*. The remainder, if any, is not included.
|
||||
|
||||
.. code-block:: hy
|
||||
|
||||
=> (list (partition 3 (range 10)))
|
||||
[(0, 1, 2), (3, 4, 5), (6, 7, 8)]
|
||||
|
||||
|
||||
.. _pos?-fn:
|
||||
|
||||
pos?
|
||||
|
@ -184,7 +184,7 @@ expressions are made of Python lists wrapped in a
|
||||
- ``(cons something some-list)`` is ``((type some-list) (+ [something]
|
||||
some-list))`` (if ``some-list`` inherits from ``list``).
|
||||
- ``(get (cons a b) 0)`` is ``a``
|
||||
- ``(slice (cons a b) 1)`` is ``b``
|
||||
- ``(cut (cons a b) 1)`` is ``b``
|
||||
|
||||
Hy supports a dotted-list syntax, where ``'(a . b)`` means ``(cons 'a
|
||||
'b)`` and ``'(a b . c)`` means ``(cons 'a (cons 'b 'c))``. If the
|
||||
|
@ -20,6 +20,6 @@
|
||||
(for [post (.xpath (get-rss-feed tumblr) "//item/title")]
|
||||
(print post.text)))
|
||||
|
||||
(if (slice argv 2)
|
||||
(if (cut argv 2)
|
||||
(print-posts (get argv 2))
|
||||
(print-posts "this-plt-life"))
|
||||
|
@ -1314,9 +1314,9 @@ class HyASTCompiler(object):
|
||||
col_offset=expr.start_column,
|
||||
targets=del_targets)
|
||||
|
||||
@builds("slice")
|
||||
@builds("cut")
|
||||
@checkargs(min=1, max=4)
|
||||
def compile_slice_expression(self, expr):
|
||||
def compile_cut_expression(self, expr):
|
||||
expr.pop(0) # index
|
||||
val = self.compile(expr.pop(0)) # target
|
||||
|
||||
@ -1600,7 +1600,7 @@ class HyASTCompiler(object):
|
||||
|
||||
# We then pass the other arguments to the function
|
||||
expr[0] = HyExpression(
|
||||
[HySymbol("slice"), tempvar, HyInteger(1)]
|
||||
[HySymbol("cut"), tempvar, HyInteger(1)]
|
||||
).replace(expr[0])
|
||||
|
||||
ret += self.compile(call)
|
||||
|
@ -314,6 +314,10 @@
|
||||
(_numeric-check n)
|
||||
(= (% n 2) 1))
|
||||
|
||||
(defn partition [n coll]
|
||||
"Chunks coll into tuples of length n. The remainder, if any, is not included."
|
||||
(apply zip (* n (, (iter coll)))))
|
||||
|
||||
(defn pos? [n]
|
||||
"Return true if n is > 0"
|
||||
(_numeric_check n)
|
||||
@ -416,7 +420,7 @@
|
||||
"Convert the given value to a string. Keyword special character will be stripped.
|
||||
String will be used as is. Even objects with the __name__ magic will work"
|
||||
(if (and (string? value) (value.startswith *keyword-prefix*))
|
||||
(hyify (slice value 2))
|
||||
(hyify (cut value 2))
|
||||
(if (string? value)
|
||||
(hyify value)
|
||||
(try
|
||||
@ -428,13 +432,12 @@
|
||||
(or (and a (not b))
|
||||
(and b (not a))))
|
||||
|
||||
(def *exports* '[butlast calling-module-name coll? cons cons? cycle
|
||||
dec distinct disassemble drop drop-last drop-while empty? even?
|
||||
every? first filter filterfalse flatten float? fraction gensym
|
||||
identity inc input instance? integer integer? integer-char?
|
||||
interleave interpose iterable? iterate iterator? keyword
|
||||
keyword? last list* macroexpand macroexpand-1 map merge-with
|
||||
name neg? nil? none? nth numeric? odd? pos? range read read-str
|
||||
remove repeat repeatedly rest reduce second some string string?
|
||||
symbol? take take-nth take-while xor zero? zip zip_longest
|
||||
zipwith])
|
||||
(def *exports*
|
||||
'[butlast calling-module-name coll? cons cons? cycle dec distinct disassemble
|
||||
drop drop-last drop-while empty? even? every? first filter filterfalse
|
||||
flatten float? fraction gensym identity inc input instance? integer integer?
|
||||
integer-char? interleave interpose iterable? iterate iterator? keyword
|
||||
keyword? last list* macroexpand macroexpand-1 map merge-with name neg? nil?
|
||||
none? nth numeric? odd? partition pos? range read read-str remove repeat
|
||||
repeatedly rest reduce second some string string? symbol? take take-nth
|
||||
take-while xor zero? zip zip_longest zipwith])
|
||||
|
@ -57,7 +57,7 @@
|
||||
|
||||
(defmacro cdr [thing]
|
||||
"Get all the elements of a thing, except the first"
|
||||
`(slice ~thing 1))
|
||||
`(cut ~thing 1))
|
||||
|
||||
|
||||
(defmacro cond [&rest branches]
|
||||
@ -104,7 +104,7 @@
|
||||
[(empty? args) `(do ~@body)]
|
||||
[(= (len args) 2) `(for* [~@args] ~@body)]
|
||||
[true
|
||||
(let [[alist (slice args 0 nil 2)]]
|
||||
(let [[alist (cut args 0 nil 2)]]
|
||||
`(for* [(, ~@alist) (genexpr (, ~@alist) [~@args])] ~@body))]))
|
||||
|
||||
|
||||
@ -175,7 +175,7 @@
|
||||
(defmacro defmacro/g! [name args &rest body]
|
||||
(let [[syms (list (distinct (filter (fn [x] (and (hasattr x "startswith") (.startswith x "g!"))) (flatten body))))]]
|
||||
`(defmacro ~name [~@args]
|
||||
(let ~(HyList (map (fn [x] `[~x (gensym (slice '~x 2))]) syms))
|
||||
(let ~(HyList (map (fn [x] `[~x (gensym (cut '~x 2))]) syms))
|
||||
~@body))))
|
||||
|
||||
|
||||
@ -221,6 +221,6 @@
|
||||
ret))
|
||||
|
||||
(defreader @ [expr]
|
||||
(let [[decorators (slice expr nil -1)]
|
||||
(let [[decorators (cut expr nil -1)]
|
||||
[fndef (get expr -1)]]
|
||||
`(with-decorator ~@decorators ~fndef)))
|
||||
|
@ -67,7 +67,7 @@
|
||||
(raise (TypeError "Need at least 2 arguments to compare"))
|
||||
(reduce operator.and_
|
||||
(list-comp (op x y)
|
||||
[(, x y) (zip args (slice args 1))]))))
|
||||
[(, x y) (zip args (cut args 1))]))))
|
||||
(defn < [&rest args]
|
||||
"Shadow < operator for when we need to import / map it against something"
|
||||
(comp-op operator.lt args))
|
||||
|
@ -291,18 +291,18 @@ def test_ast_bad_get():
|
||||
cant_compile("(get 1)")
|
||||
|
||||
|
||||
def test_ast_good_slice():
|
||||
"Make sure AST can compile valid slice"
|
||||
can_compile("(slice x)")
|
||||
can_compile("(slice x y)")
|
||||
can_compile("(slice x y z)")
|
||||
can_compile("(slice x y z t)")
|
||||
def test_ast_good_cut():
|
||||
"Make sure AST can compile valid cut"
|
||||
can_compile("(cut x)")
|
||||
can_compile("(cut x y)")
|
||||
can_compile("(cut x y z)")
|
||||
can_compile("(cut x y z t)")
|
||||
|
||||
|
||||
def test_ast_bad_slice():
|
||||
"Make sure AST can't compile invalid slice"
|
||||
cant_compile("(slice)")
|
||||
cant_compile("(slice 1 2 3 4 5)")
|
||||
def test_ast_bad_cut():
|
||||
"Make sure AST can't compile invalid cut"
|
||||
cant_compile("(cut)")
|
||||
cant_compile("(cut 1 2 3 4 5)")
|
||||
|
||||
|
||||
def test_ast_good_take():
|
||||
|
@ -470,6 +470,18 @@
|
||||
(try (do (odd? None) (assert False))
|
||||
(catch [e [TypeError]] (assert (in "not a number" (str e))))))
|
||||
|
||||
(defn test-partition []
|
||||
"NATIVE: testing the partition function"
|
||||
(setv ten (range 10))
|
||||
(assert-equal (list (partition 3 ten))
|
||||
[(, 0 1 2) (, 3 4 5) (, 6 7 8)])
|
||||
(assert-equal (list (partition 2 ten))
|
||||
[(, 0 1) (, 2 3) (, 4 5) (, 6 7) (, 8 9)])
|
||||
(assert-equal (list (partition 1 ten))
|
||||
[(, 0) (, 1) (, 2) (, 3) (, 4) (, 5) (, 6) (, 7) (, 8) (, 9)])
|
||||
(assert-equal (list (partition 0 ten)) [])
|
||||
(assert-equal (list (partition -1 ten)) []))
|
||||
|
||||
(defn test-pos []
|
||||
"NATIVE: testing the pos? function"
|
||||
(assert-true (pos? 2))
|
||||
|
@ -536,11 +536,11 @@
|
||||
(assert (= (car [1 2 3 4 5]) 1)))
|
||||
|
||||
|
||||
(defn test-slice []
|
||||
"NATIVE: test slice"
|
||||
(assert (= (slice [1 2 3 4 5] 1) [2 3 4 5]))
|
||||
(assert (= (slice [1 2 3 4 5] 1 3) [2 3]))
|
||||
(assert (= (slice [1 2 3 4 5]) [1 2 3 4 5])))
|
||||
(defn test-cut []
|
||||
"NATIVE: test cut"
|
||||
(assert (= (cut [1 2 3 4 5] 1) [2 3 4 5]))
|
||||
(assert (= (cut [1 2 3 4 5] 1 3) [2 3]))
|
||||
(assert (= (cut [1 2 3 4 5]) [1 2 3 4 5])))
|
||||
|
||||
|
||||
(defn test-take []
|
||||
|
Loading…
Reference in New Issue
Block a user