Live data from Hacker News

Method Combinators in CoffeeScript

github.com

21–26 of 26 posts

Re: Method Combinators in CoffeeScript

#21

A little nitpick, which may be a misconception deserving clarification: > Our decorators work just like Python method decorators, only we don't need any magic syntax for them because CoffeeScript, like JavaScript, already has this idea that functions can return functions and there's nothing particularly magic about defining a method, it's just an expression that evaluates to a function. Python also has this idea (cal…

You're speaking to the first part of my claim, but not the second, namely that JavaScript and CoffeeScript don't separate the idea of a function and a method, which is what allows you to use first-class functional combinators as decorators.

> JavaScript and CoffeeScript don't separate the idea of a function and a method

Neither does Python, at definition time. All the difference is in the processing performed by the class constructor (`type`) when the class object is created. Before that, it's a bog-standard function.

> which is what allows you to use first-class functional combinators as decorators.

Which is exactly like Python, the original decorators[0][1] predate the syntactic extension by several versions, the original syntax for applying them was:

    def some_method(cls):
        pass
    some_method = classmethod(some_method)
And you could use the exact same syntax to define non-method functions (though not this decorator, of course, as it doesn't make sense for functions)

[0] http://docs.python.org/library/functions.html?highlight=clas...

[1] http://docs.python.org/library/functions.html?highlight=clas...

Re: Method Combinators in CoffeeScript

#22
post #14

Earlier quoted context omitted.

That makes no sense at all. The lambda complaint is mostly a syntax complaint, it is not a complaint about not having anonymous functions. It even has the ability to invoke __call__ on instances, allowing you to create your own "functions" that are actually objects, if you like, which is somewhat unusual. There's no point in having an anonymous function decorator. You'd simply inline it into the body itself right on…

I don't know about those other complaints, I don't personally have any complaints about Python. It doesn't have multi-line lambdas, that's a design trade-off like an iPhone not having a slide-out keyboard. But it does have first-class functions. https://en.wikipedia.org/wiki/Python_syntax_and_semantics#Fi... What I was speaking to is the desire to write: class SomeExampleModel: def setHeavyweightProperty: triggers('c…

> What I was speaking to is the desire to write:

    setHeavyweightProperty = triggers('cache:dirty')(
        lambda self, property, value: 'code')
is the same thing as

    @triggers('cache:dirty')
    def setHeavyweightProperty(self, property, value):
        # code
but with the limitations of lambdas (no statements allowed)

> First, the multi-line anonymous lambda being used as the target of the decorator.

Python has no issue with that, it's just hard to do it because Python's lambdas can only contain expressions which can be rather limited in a statements-heavy language.

> Second, a function being called with another function as its argument as an expression within an instance method definition.

That's because your "instance method definition" is not syntactically correct, the capability itself exists. A "method definition" is nothing more than a function defined within a class scope. In fact you can do things like that:

    >>> def method_outside(self):
    ...     return self.wedding
    ... 
    >>> class SomeType:
    ...     whose = method_outside
    ... 
    >>> s = SomeType()
    >>> s.wedding = "Amanda"
    >>> s.whose()
    'Amanda'
    >>> method_outside(s)
    'Amanda'
    >>> from collections import namedtuple
    >>> o = namedtuple('N', 'wedding')(wedding="Jim")
    >>> method_outside(o)
    'Jim'
    >>>

Re: Method Combinators in CoffeeScript

#23
post #15
post #14

Earlier quoted context omitted.

That makes no sense at all. The lambda complaint is mostly a syntax complaint, it is not a complaint about not having anonymous functions. It even has the ability to invoke __call__ on instances, allowing you to create your own "functions" that are actually objects, if you like, which is somewhat unusual. There's no point in having an anonymous function decorator. You'd simply inline it into the body itself right on…

Just FYI, he's not talking about the decorator being anonymous, he's talking about the function the decorator is wrapping being anonymous. What he means is that, since python lambda's are one-line, you can't do something like: ifAuthorized("admin", lambda x: // more code here

> What he means is that, since python lambda's are one-line

Technically, Python's lambdas are one-expression not one-line.

Within that constraint you can create as complex lambdas as you want (you'll probably need to create helpers for things like loops, and you may be hindered by Python's limits on recursion, but FWIW Scheme's or Haskell's functions have the exact same limitation, the primary difference being they're nowhere near as statement-heavy as Python).

Re: Method Combinators in CoffeeScript

#24

A little nitpick, which may be a misconception deserving clarification: > Our decorators work just like Python method decorators, only we don't need any magic syntax for them because CoffeeScript, like JavaScript, already has this idea that functions can return functions and there's nothing particularly magic about defining a method, it's just an expression that evaluates to a function. Python also has this idea (cal…

You're speaking to the first part of my claim, but not the second, namely that JavaScript and CoffeeScript don't separate the idea of a function and a method, which is what allows you to use first-class functional combinators as decorators.

I only intended to comment on the first part. Decided against cutting the quote mid-sentence. (Which led to me learning something new from masklinn's comment. =))

Re: Method Combinators in CoffeeScript

#25

Earlier quoted context omitted.

I don't know about those other complaints, I don't personally have any complaints about Python. It doesn't have multi-line lambdas, that's a design trade-off like an iPhone not having a slide-out keyboard. But it does have first-class functions. https://en.wikipedia.org/wiki/Python_syntax_and_semantics#Fi... What I was speaking to is the desire to write: class SomeExampleModel: def setHeavyweightProperty: triggers('c…

> What I was speaking to is the desire to write: setHeavyweightProperty = triggers('cache:dirty')( lambda self, property, value: 'code') is the same thing as @triggers('cache:dirty') def setHeavyweightProperty(self, property, value): # code but with the limitations of lambdas (no statements allowed) > First, the multi-line anonymous lambda being used as the target of the decorator. Python has no issue with that, it's…

Can't edit, but "a function defined within a class scope" should actually be "a function assigned within a class scope", where and how the function object was defined does not actually matter (as the example shows), the only thing which matters is that the object is bound to a name in the class scope.

Re: Method Combinators in CoffeeScript

#26

In LiveScript: https://github.com/quarterto/homoiconic/blob/d6b78812c00eb4d...

Interesting, do like the "implicit currying". And the decorator motivations read better when they are expressed with the <| rather than when they are just "escaped line breaks".
Post reply on HN