Expression closures - use-cases for shortcut lambda syntax (blocks)

Vassily Gavrilyak gavrilyak at gmail.com
Sat Mar 17 06:51:01 PDT 2007


On 3/17/07, Igor Bukanov <igor at mir2.org> wrote:
> Note that Ruby's do-block is not a function but just a body of a loop
> and a roughly corresponds to for-in loop in JS executed over a
> generator.

>>Note that Ruby's do-block is not a function but just a body of a loop
>>and a roughly corresponds to for-in loop in JS executed over a
>>generator.

Well, Ruby blocks are just anonymous functions. They are often used
with loops, but that because they are good here. From Ruby language
author
http://www.artima.com/intv/closures.html

>>Yukihiro Matsumoto: Blocks are basically nameless functions.
>>You may be familiar with the lambda from other languages like Lisp or Python.
>>Basically, you can pass a nameless function to another function, and then that
>> function can invoke the passed-in nameless function. For example, a function
>> could perform iteration by passing one item at a time to the
nameless function.
>>This is a common style, called higher order function style, among
languages that
>> can handle functions as first class objects. Lisp does it. Python does it .
>>Even C does it with function pointers.

ES has anonymous functions too and their semantics is the same as in Ruby.
Only syntax is different.
In Ruby
do ... end is  the  same as {}. By convention, second is used for
one-liners only.
In while loops do can be omitted (just a sugar).

Another example of small DSL in Ruby with blocks from Rails

create_table :products do |t|
  t.column :title, :string
  t.column :description, :text
  t.column :image_url, :string
  # you can add your own code (not data) here, if needed
end

in ES it would be

createTable("products", function(t){
  t.column("title", "string")
  t.column("description", "text")
  t.column("image_url", "string")
  // you can add your own code (not data) here, if needed
})

I understand that this partial use-case is better to describe with
plain data (JSON-like) but Rails gives you a possibility to define
some table data with code too.

The same can be done about GUI or HTML forms, etc.
So, just one more example - DSLs.

BTW, Igor, it seems that you are the only one who use the syntax
exactly as I proposed. Just curios, is it because you know cyrillic
(guessing from your name) and it looks to you like L or lambda letter?

Regards,
Vassily



More information about the Es4-discuss mailing list