From 7dd54c536b60373a5b215a6f067562add5763064 Mon Sep 17 00:00:00 2001 From: Kodi Arfer Date: Sat, 18 Feb 2017 21:58:54 -0800 Subject: [PATCH] Remove `lambda`, now that `fn` does the same thing --- NEWS | 1 + docs/extra/anaphoric.rst | 2 +- docs/language/api.rst | 16 +++++++++------- hy/cmdline.py | 4 ++-- hy/compiler.py | 1 - hy/core/shadow.hy | 2 +- hy/extra/anaphoric.hy | 4 ++-- tests/compilers/test_ast.py | 6 +++--- tests/native_tests/language.hy | 16 ++++++---------- tests/native_tests/with_decorator.hy | 6 +++--- 10 files changed, 28 insertions(+), 30 deletions(-) diff --git a/NEWS b/NEWS index ddeb859bc..db81d1c79 100644 --- a/NEWS +++ b/NEWS @@ -3,6 +3,7 @@ Changes from 0.12.1 [ Language Changes ] * `let` has been removed. Python's scoping rules do not make a proper implementation of it possible. Use `setv` instead. + * `lambda` has been removed, but `fn` now does exactly what `lambda` did. * Commas and underscores are allowed in numeric literals * with-decorator: Allow a `setv` form as the form to be decorated * xor: If exactly one argument is true, return it diff --git a/docs/extra/anaphoric.rst b/docs/extra/anaphoric.rst index 070f87efd..68cb70948 100644 --- a/docs/extra/anaphoric.rst +++ b/docs/extra/anaphoric.rst @@ -236,7 +236,7 @@ xi Usage ``(xi body ...)`` -Returns a function with parameters implicitly determined by the presence in the body of xi parameters. An xi symbol designates the ith parameter (1-based, e.g. x1, x2, x3, etc.), or all remaining parameters for xi itself. This is not a replacement for lambda. The xi forms cannot be nested. +Returns a function with parameters implicitly determined by the presence in the body of xi parameters. An xi symbol designates the ith parameter (1-based, e.g. x1, x2, x3, etc.), or all remaining parameters for xi itself. This is not a replacement for fn. The xi forms cannot be nested. This is similar to Clojure's anonymous function literals (``#()``). diff --git a/docs/language/api.rst b/docs/language/api.rst index db0e6d80c..21efbcd86 100644 --- a/docs/language/api.rst +++ b/docs/language/api.rst @@ -393,8 +393,8 @@ do ``do`` is used to evaluate each of its arguments and return the last one. Return values from every other than the last argument are discarded. -It can be used in ``lambda`` or ``list-comp`` to perform more complex logic as -shown in one of the following examples. +It can be used in ``list-comp`` to perform more complex logic as shown in one +of the following examples. Some example usage: @@ -1103,13 +1103,15 @@ that ``import`` can be used. (import [sys [*]]) -lambda / fn +fn ----------- -``lambda`` and ``fn`` can be used to define an anonymous function. The parameters are -similar to ``defn``: the first parameter is vector of parameters and the rest is the -body of the function. ``lambda`` returns a new function. In the following example, an -anonymous function is defined and passed to another function for filtering output. +``fn``, like Python's ``lambda``, can be used to define an anonymous function. +Unlike Python's ``lambda``, the body of the function can comprise several +statements. The parameters are similar to ``defn``: the first parameter is +vector of parameters and the rest is the body of the function. ``fn`` returns a +new function. In the following example, an anonymous function is defined and +passed to another function for filtering output. .. code-block:: clj diff --git a/hy/cmdline.py b/hy/cmdline.py index 3638f6e8a..87dbe672e 100644 --- a/hy/cmdline.py +++ b/hy/cmdline.py @@ -164,11 +164,11 @@ def ideas_macro(): ;;; filtering a list w/ a lambda -(filter (lambda [x] (= (% x 2) 0)) (range 0 10)) +(filter (fn [x] (= (% x 2) 0)) (range 0 10)) ;;; swaggin' functional bits (Python rulez) -(max (map (lambda [x] (len x)) ["hi" "my" "name" "is" "paul"])) +(max (map (fn [x] (len x)) ["hi" "my" "name" "is" "paul"])) """)]) diff --git a/hy/compiler.py b/hy/compiler.py index d79f88347..3049aeb4a 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -2300,7 +2300,6 @@ def compile_set(self, expression): col_offset=expression.start_column) return ret - @builds("lambda") @builds("fn") @checkargs(min=1) def compile_function_def(self, expression): diff --git a/hy/core/shadow.hy b/hy/core/shadow.hy index d216b177a..f5f9b4c2d 100644 --- a/hy/core/shadow.hy +++ b/hy/core/shadow.hy @@ -65,7 +65,7 @@ "Helper for shadow comparison operators" (if (< (len args) 2) (raise (TypeError "Need at least 2 arguments to compare")) - (reduce (lambda [x y] (and x y)) + (reduce (fn [x y] (and x y)) (list-comp (op x y) [(, x y) (zip args (cut args 1))])))) (defn < [&rest args] diff --git a/hy/extra/anaphoric.hy b/hy/extra/anaphoric.hy index ac5c80e58..2bbfc9cd0 100644 --- a/hy/extra/anaphoric.hy +++ b/hy/extra/anaphoric.hy @@ -135,9 +135,9 @@ "Returns a function with parameters implicitly determined by the presence in the body of xi parameters. An xi symbol designates the ith parameter (1-based, e.g. x1, x2, x3, etc.), or all remaining parameters for xi itself. - This is not a replacement for lambda. The xi forms cannot be nested. " + This is not a replacement for fn. The xi forms cannot be nested. " (setv flatbody (flatten body)) - `(lambda [;; generate all xi symbols up to the maximum found in body + `(fn [;; generate all xi symbols up to the maximum found in body ~@(genexpr (HySymbol (+ "x" (str i))) [i (range 1 diff --git a/tests/compilers/test_ast.py b/tests/compilers/test_ast.py index b5fab3a4c..9bbed0ca9 100644 --- a/tests/compilers/test_ast.py +++ b/tests/compilers/test_ast.py @@ -225,13 +225,13 @@ def test_ast_bad_defclass(): def test_ast_good_lambda(): "Make sure AST can compile valid lambda" - can_compile("(lambda [])") - can_compile("(lambda [] 1)") + can_compile("(fn [])") + can_compile("(fn [] 1)") def test_ast_bad_lambda(): "Make sure AST can't compile invalid lambda" - cant_compile("(lambda)") + cant_compile("(fn)") def test_ast_good_yield(): diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index 6057b017a..d4ac7e5d2 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -75,7 +75,7 @@ (except [e [TypeError]] (assert (in "Can't assign to a builtin" (str e))))) (try (eval '(defn get [] (print "hello"))) (except [e [TypeError]] (assert (in "Can't assign to a builtin" (str e))))) - (try (eval '(defn lambda [] (print "hello"))) + (try (eval '(defn fn [] (print "hello"))) (except [e [TypeError]] (assert (in "Can't assign to a builtin" (str e)))))) @@ -133,10 +133,6 @@ (defn test-alias-names-in-errors [] "NATIVE: tests that native aliases show the correct names in errors" - (try (eval '(lambda)) - (except [e [Exception]] (assert (in "lambda" (str e))))) - (try (eval '(fn)) - (except [e [Exception]] (assert (in "fn" (str e))))) (try (eval '(setv 1 2 3)) (except [e [Exception]] (assert (in "setv" (str e))))) (try (eval '(def 1 2 3)) @@ -343,11 +339,11 @@ "level"))) -(defn test-lambda [] - "NATIVE: test lambda operator" - (setv square (lambda [x] (* x x))) +(defn test-fn [] + "NATIVE: test fn operator" + (setv square (fn [x] (* x x))) (assert (= 4 (square 2))) - (setv lambda_list (lambda [test &rest args] (, test args))) + (setv lambda_list (fn [test &rest args] (, test args))) (assert (= (, 1 (, 2 3)) (lambda_list 1 2 3)))) @@ -1424,7 +1420,7 @@ (defmacro identify-keywords [&rest elts] `(list (map - (lambda (x) (if (is-keyword x) "keyword" "other")) + (fn (x) (if (is-keyword x) "keyword" "other")) ~elts))) (defn test-keywords-and-macros [] diff --git a/tests/native_tests/with_decorator.hy b/tests/native_tests/with_decorator.hy index c93907a73..90891e982 100644 --- a/tests/native_tests/with_decorator.hy +++ b/tests/native_tests/with_decorator.hy @@ -1,6 +1,6 @@ (defn test-decorated-1line-function [] (defn foodec [func] - (lambda [] (+ (func) 1))) + (fn [] (+ (func) 1))) (with-decorator foodec (defn tfunction [] (* 2 2))) @@ -9,7 +9,7 @@ (defn test-decorated-multiline-function [] (defn bazdec [func] - (lambda [] (+ (func) "x"))) + (fn [] (+ (func) "x"))) (with-decorator bazdec (defn f [] (setv intermediate "i") @@ -30,7 +30,7 @@ (defn test-decorated-setv [] (defn d [func] - (lambda [] (+ (func) "z"))) + (fn [] (+ (func) "z"))) (with-decorator d (setv f (fn [] "hello"))) (assert (= (f) "helloz")))