[PRL] Of course our programming language can do this

Viera K. Proulx vkp at ccs.neu.edu
Wed Aug 2 14:32:59 EDT 2006


On Aug 2, 2006, at 2:05 PM, Doug Orleans wrote:

>
> I don't think it's as painful in Java as Spolsky makes it sound.  And
> I don't think he thinks the whole point is the lack of syntactic sugar
> in Java, but that there's a fundamental difference between objects and
> functions.  But I'm no expert on what Spolsky thinks, I've only read
> the few things that Mitch has forwarded to PRL...
>

You really like to write this??? - and explain it to freshmen???

interface Selector<T>{
   boolean select(T t);
}

boolean andMap(ArrayList alist, Selector<T> s){
    int i = 0;
    int size = alist.size();
    while (i < size){
      if (!(s.select(alist.get(i))))
        return false;
      i = i + 1;
    }
    return true;
}

and the use it like this:

boolean cheapBooks = andMap(myBookList,
                             new Selector<Book>(){
                                   boolean select(Book b){
                                     return b.price < 10;
                                   }
                              });

You still need to define an interface that represents the functions -  
at lest to provide the signature (even if generic) with the return type.

-- Viera  



  



More information about the PRL mailing list