Skip to content

Commit

Permalink
Remove lambda, now that fn does the same thing
Browse files Browse the repository at this point in the history
  • Loading branch information
Kodiologist committed Feb 19, 2017
1 parent 580552a commit 7dd54c5
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 30 deletions.
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/extra/anaphoric.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 (``#()``).

Expand Down
16 changes: 9 additions & 7 deletions docs/language/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions hy/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]))
""")])

Expand Down
1 change: 0 additions & 1 deletion hy/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion hy/core/shadow.hy
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions hy/extra/anaphoric.hy
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions tests/compilers/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
16 changes: 6 additions & 10 deletions tests/native_tests/language.hy
Original file line number Diff line number Diff line change
Expand Up @@ -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))))))


Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))))


Expand Down Expand Up @@ -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 []
Expand Down
6 changes: 3 additions & 3 deletions tests/native_tests/with_decorator.hy
Original file line number Diff line number Diff line change
@@ -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)))
Expand All @@ -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")
Expand All @@ -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")))
Expand Down

0 comments on commit 7dd54c5

Please sign in to comment.