Scale advocacy

Ali Çehreli acehreli at yahoo.com
Sun Jul 3 13:07:43 PDT 2011


On Sun, 03 Jul 2011 12:44:01 -0400, bearophile wrote:

> A nice and almost funny set of slides (no PDF available, I think) that
> show how to convince your boss to try Scala, it shows only very basic
> things:
> 
> http://scala-boss.heroku.com/#1
> 
> Bye,
> bearophile

D's solution to the problem at slide #28 (http://scala-boss.heroku.com/
#28) is similar to Scala's:

const(Account) findAccount(Customer customer,
                           EnergyType energy,
                           Date date)
{
    bool matching(const(Account) account)
    {
        return ((account.servicePoint.energy == energy)
                &&
                isDateInWindow(date,
                               account.moveInDate,
                               account.moveOutDate));
    }

    return front_or_null(find!matching(customer.accounts));
}

I know that matching() could be a function literal in D as well, but I 
prefer nested functions for code clarity when the criteria is not really 
short.

Also, although returning the range directly would be more D-like, I've 
written the following front_or_null() instead of Scala's getOrElse():

import std.range;

// ...

ElementType!Range front_or_null(Range)(Range range)
{
    return range.empty ? null : range.front;
}

The template constraint has proven to be problematic though. I will open 
a separate thread about that.

Ali


More information about the Digitalmars-d-learn mailing list