From: "Florian Groß" Date: 2005-08-07T02:53:06+09:00 Subject: Re: new block notation (was: Re: ruby-dev summary 26468-26661) Yukihiro Matsumoto wrote: > |Why not go with lambda(arg-list) { body } or fun(arg-list) { body } > |or def(arg-list) { body } then? I think that the syntax would point > |out that this is more than a method that turns a block into an object > |and I think that it would be a simple solution. > > Introducing a new reserved word possibly break existing code. Besides > that, there's no good candidate for a new keyword: lambda is too > awkward, def is not sufficient since we don't define anything here, > and I expect the name fun is used everywhere as local variable names. Should be okay as long as it isn't used as a method name. The 1.9 () on local variables change was undone after all, right? Having fun as a method name sounds bad. Same as having method1, method2 etc.. It could have been used as an accessor, though, but I think self.fun would be save to do as well. I'm not sure I agree with lambda being too awkward. It would be awkward if we didn't also have blocks. And JavaScript uses function and C# delegate both of which are more awkward than lambda. Of course there is also Perl's sub. def might not totally fit (it doesn't bind anything to a name), but it might still be the best choice if no new semi-keywords should be introduced. There's also undef, of course, but that is a more evil choice. :) > "->" might appear weird at first. But it doesn't break any code. > Pure addition to the current syntax. And it's short enough to > encourage functional programming. The last one is my optimistic > estimation. There's an opposite possibility that it would be used > only to obfuscate code. I know that Huffman encoding is better, but I think it does not take into account that human beings are better at remembering words than symbols. OTOH symbols usually stick out more visually, but I don't think I consider that a good thing in the case of lambdas. (->(a, b) { a + b }).call {1: ->(a, b) { puts "Hello World" }} foo(bar: ->(a, b) { a * b }, qux: ->(str) { str.reverse }) funs = [->(a, b) { a - b }, ->(a, b) { a + b}] funs = [ ->(a, b) { a - b }, ->(a, b) { a + b } ] def adder(a) return ->(b) { a + b } end adder = ->(a) do ->(b) { a + b } end search_fun = ->(x: 0, y: 0, width: @width, height: @height, type: Types.All, limit: @objects.size, skip: 0, destructive: false, &block) do block ||= ->(object) { true } found = [] pos = 0 @objects.reject! do |object| if object.x.between?(x, x + width) and object.y.between?(y, y + height) and type & object.type != 0 and block.call(object) then pos += 1 if pos >= skip and pos < limit + skip found << object destructive end end end found end fun(a, b) { a + b }.call {1: fun(a, b) { puts "Hello World" }} foo(bar: fun(a, b) { a * b }, qux: fun(str) { str.reverse }) funs = [fun(a, b) { a - b }, fun(a, b) { a + b }] funs = [ fun(a, b) { a - b }, fun(a, b) { a + b } ] def adder(a) return fun(b) { a + b } end adder = fun(a) do fun(b) { a + b } end search_fun = fun(x: 0, y: 0, width: @width, height: @height, type: Types.All, limit: @objects.size, skip: 0, destructive: false, &block) do block ||= fun(object) { true } found = [] pos = 0 @objects.reject! do |object| if object.x.between?(x, x + width) and object.y.between?(y, y + height) and type & object.type != 0 and block.call(object) then pos += 1 break if pos >= limit + skip if pos >= skip then found << object destructive end end end found end