def awhen(func, callback):
val = func()
if val:
callback(val) def awhen(func, callback):
val = func()
if val:
callback(val) ## if
val = func()
if val: # standard form
code(val) # of if
## aif
aif(func, code) # not obvious that it does the same thing
# and limits you to using a function defined
# elsewhere, or Python's crippled one-line lambda.
Whereas in Lisp, you'd do: ;; regular when
(let (val (func))
(when val ; standard form
(func val))) ; of when
;; awhen
(awhen (func) ; standard form of
(func it) ; when, with added bound value
; possibly other
; code goes here
)
Do you see how the two forms of Python look very different, and the two forms of Lisp look the same?If you don't like magic, you could invent an awhen that took an extra parameter that designates the name to bind the result to, e.g. (awhen val test).
So.
Yes - you can achieve the same thing, as in any programming language that is Turing complete.
No - you cannot get the same syntax. And syntax affects readability and familiarity.
You're right that I could have used a better example, such as `when` or whatnot, but that's the first thing that came to me ;-)
The difference between using a decorator and a lisp macro is that using a decorator is "using the syntax of the language to solve your problem", while a macro is "adding a new syntax".
I do think there are way in Python (or in C++) to use meta-programming to tweak the language but it always feel very hacky. I.e. I'd hate to jump into a python code-base that isn't standard, such as using @awhen decorator instead of a normal if ;-) What about you? But in Lisp, there's no difference in the syntax.. and adding missing features to the language instead of repeating the same pattern is the standard way to code.