News for hackers. Stuff that matters.
Re:Show HN: `diff arc0 arc3.1` by jdc on Friday October 16, 2015 @12:01AM (#10397396)
That may not be the best example of macros since you can do the same thing in Python:

    def awhen(func, callback):
      val = func()
      if val:
        callback(val)
Re:Show HN: `diff arc0 arc3.1` by mikaelj on Friday October 16, 2015 @02:09AM (#10397716)
If we were to name your operator "aif" instead (since there is no "when" in Python), and compare if and aif:

  ## 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.

Re:Show HN: `diff arc0 arc3.1` by d0m on Friday October 16, 2015 @04:15PM (#10401735)
I hear you but not really because you have to wrap everything in lambdas. Also, you didn't add a whole new variable in the workflow "it" but passed it as a function.

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 ;-)

Re:Show HN: `diff arc0 arc3.1` by voltagex_ on Friday October 16, 2015 @12:46AM (#10397521)
There's probably also a way to write a decorator that does the same thing.
Re:Show HN: `diff arc0 arc3.1` by d0m on Friday October 16, 2015 @04:30PM (#10401814)
By the way, I love Python and use it every day, so I'm not trying to get into a language war.

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.