Python decorators are a hack around the lack of proper lambda ;) Just pass the function: decorate -> … Where "decorate" is a fn
Method Combinators in CoffeeScript
11–20 of 26 posts
Re: Method Combinators in CoffeeScript
#12> 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 (called ‘higher-order functions’). The difference is in syntax—function calls require parens in Python, and anonymous functions aren't that well supported. Therefore the need for special syntax construct to make decorator use convenient.
Re: Method Combinators in CoffeeScript
#13Earlier quoted context omitted.
I think that's a pretty poor practice. It's actually counter to central idea of this article; that you should think with the language features. In situations like the one above, if you dont want to lose the context of "this" you can explicitly bind an object to be this and return the new bound function. See underscore's bind(). Its a little more verbose and clunky but it saves you from tossing out modular code.
And of course, in CoffeeScript you would just use the fat arrow: somethingElse = render: => # do some rendering
I just tested and it looks like you can hack it by doing
_this = somethingElse =
render: => @renderMe # @ is somethingElse
but that is definitely a bug.Re: Method Combinators in CoffeeScript
#14Jeremy Ashkenas provides the "tl;dr:" Python decorators are a hack around the lack of proper lambda ;) Just pass the function: decorate -> … Where "decorate" is a fn https://twitter.com/jashkenas/status/235012485009248256
There's no point in having an anonymous function decorator. You'd simply inline it into the body itself right on the spot, anything else would simply be obfuscation for the sake of obfuscation. This only makes sense if you're using a decorator applied elsewhere, at which point all languages require you to have named it, so there's no point complaining about lack of anonymous functions here.
Python has first-class functions. It even has first-class methods, with automatic instance binding. It just doesn't quite work how very functionally-oriented people want it to work. Part of the reason I don't like the kvetching that such people do is that it does seem to convince people that Python is lacking function references. Nope. It just doesn't spell them to everybody's taste (for instance, "it doesn't have Ruby blocks" translates to "it doesn't have block syntax", not "it can't do function references", and this is a taste issue not a fundamental capability issue), and the can rarely have moderately inconvenient scope rules if you want to write to outer scopes.
Decorators are syntax sugar, not a new feature.
Re: Method Combinators in CoffeeScript
#15Jeremy Ashkenas provides the "tl;dr:" Python decorators are a hack around the lack of proper lambda ;) Just pass the function: decorate -> … Where "decorate" is a fn https://twitter.com/jashkenas/status/235012485009248256
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…
ifAuthorized("admin", lambda x:
// more code hereRe: Method Combinators in CoffeeScript
#16A 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…
Re: Method Combinators in CoffeeScript
#17Jeremy Ashkenas provides the "tl;dr:" Python decorators are a hack around the lack of proper lambda ;) Just pass the function: decorate -> … Where "decorate" is a fn https://twitter.com/jashkenas/status/235012485009248256
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…
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('cache:dirty')(
lambda self, property, value:
...something...
My understanding is that Python doesn't like two different things about this. First, the multi-line anonymous lambda being used as the target of the decorator. Second, a function being called with another function as its argument as an expression within an instance method definition.I'm open to reëducation.
Re: Method Combinators in CoffeeScript
#18Earlier quoted context omitted.
And of course, in CoffeeScript you would just use the fat arrow: somethingElse = render: => # do some rendering
Careful, that's not the same as (->).bind(somethingElse). => binds to the current value of this at execution -- in that function, @ will compile to `var _this = this`, not `somethingElse` as we'd expect. I just tested and it looks like you can hack it by doing _this = somethingElse = render: => @renderMe # @ is somethingElse but that is definitely a bug.
Re: Method Combinators in CoffeeScript
#19Re: Method Combinators in CoffeeScript
#20Earlier 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…
The only thing python does not like is multi-line lambdas. You can target a lambda with decorators, just like you can target any callable. I'm not sure if I correctly parsed what you mean by "a function being called with another function as its argument as an expression within an instance method definition", but I'm pretty sure python can do that. Here is some demonstration code (see RaganwaldWidgetViewThree):
https://gist.github.com/3495990
And finally here is the translation of the before/after/etc. method combinators:
https://gist.github.com/3495985
But I might have misunderstood waitLevel, can you explain what it is for?