Expression closures - use-cases for shortcut lambda syntax(blocks)
Igor Bukanov
igor at mir2.org
Wed Mar 21 09:30:36 PDT 2007
On 21/03/07, Vassily Gavrilyak <gavrilyak at gmail.com> wrote:
> Please take a minute and write this simple Ruby code in ES.
> Please do not refactor it, suppose we do not need any of logic from
> this function anywhere else and we do not want to bloat our class with
> miriads of methods.
> So. just one function.
>
> def doSomethingWithNewPeople
> selectSQL ="SELECT code, name from people where status='new'"
> insertSQL = "INSERT INTO young(name,age) values (?,?)"
> connection.transaction do |tx|
> connection.prepare(selectSQL) do |selectStatement|
> connection.prepare(insertSQL) do |insertStatement|
> selectStatement.executeQuery do |record|
> name, age = record
> if name ~ /.*Joe/ and age % 2 then
> insertStatement.execute(name, age)
> end
> end
> end
> end
> end
> end
Some refactoring is inevitable as Ruby provides its own idioms, but
here is its counterpart in JS:
function doSomethingWithNewPeople()
{
let selectSQL ="SELECT code, name from people where status='new'"
let insertSQL = "INSERT INTO young(name,age) values (?,?)"
connection.transaction(function(tx) {
let selectStatement = connection.prepare(selectSQL);
let insertStatement = connection.prepare(insertSQL);
for (let [name, age] in selectStatement.executeQuery()) {
if (name.match(/.*Joe/) && age % 2)
insertStatement.execute(name, age);
}
});
}
Regards, Igor
More information about the Es4-discuss
mailing list