Skip to content

Instantly share code, notes, and snippets.

@lagenorhynque
Created January 9, 2020 04:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lagenorhynque/38d1c5d4f1c7bc3f84b4ede5329487df to your computer and use it in GitHub Desktop.
Save lagenorhynque/38d1c5d4f1c7bc3f84b4ede5329487df to your computer and use it in GitHub Desktop.
Walrus operator in Python 3.8 vs if-let macro in Clojure
dev> (if-let [coll (seq [1 2 3])]
(println coll)
(println "EMPTY"))
(1 2 3)
nil
dev> (if-let [coll (seq [])]
(println coll)
(println "EMPTY"))
EMPTY
nil
;; macro expansion
dev> (macroexpand-1
'(if-let [coll (seq [1 2 3])]
(println coll)
(println "EMPTY")))
(clojure.core/let
[temp__5733__auto__ (seq [1 2 3])]
(if
temp__5733__auto__
(clojure.core/let [coll temp__5733__auto__] (println coll))
(println "EMPTY")))
In [1]: if coll := [1, 2, 3]:
...: print(coll)
...: else:
...: print('EMPTY')
...:
[1, 2, 3]
In [2]: if coll := []:
...: print(coll)
...: else:
...: print('EMPTY')
...:
EMPTY
# prior to version 3.8
In [3]: coll = [1, 2, 3]
In [4]: if coll:
...: print(coll)
...: else:
...: print('EMPTY')
...:
[1, 2, 3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment