[plt-scheme] Functional Abstraction in Java

From: Robby Findler (robby at eecs.northwestern.edu)
Date: Wed Jul 1 13:32:50 EDT 2009

You can do the same thing as in HtDP by passing in a object with a
single method that does what the function would have done.

A more OO solution is to put a getXdays method in a common (abstract)
superclass and then call getXIndex and have the two classes override
getXIndex with getSundayIndex and getSaturdayIndex. This only works if
the two classes can share a common superclass.

This is called the "hook and template" design pattern, I believe.


On Wed, Jul 1, 2009 at 12:23 PM, Andrei Estioco<chadestioco at gmail.com> wrote:
> Hello. My apologies if I am asking this in the wrong mailing list (this
> being a Java question). But since I encountered this concept in HtDP, I
> thought of asking around here. (See Chapter 21 of HtDP). See I have a
> function private int getSaturdays(int x, int fordays){ int Weeks =
> getWeeks(x, fordays); int SaturdayCounter = 0; int i = 1; while(i <=
> hartalWeeks){ if(getSaturdayIndex(i) % x == 0){ SaturdayCounter++; i++; }
> else{ i++; } } return SaturdayCounter; } and another one private int
> getSundays(int x, int fordays){ int Weeks = getWeeks(x, fordays); int
> SundaysCounter = 0; int i = 1; while(i <= hartalWeeks){ if(getSundayIndex(i)
> % x == 0){ SundayCounter++; i++; } else{ i++; } } return SundayCounter; } As
> obvious, they do the same process, their sole difference being a function
> call (the one in the if line). Now, if I were to do this in Scheme, I'll
> just add another argument to my function which will be called in the if line
> (as in Chapter 21). However, I do not know how to do this in Java.
> I know how to sort in Java using the Comparator interface and all that OO
> stuff. I thought of using that style here but then I was wondering if I
> could do this without OO. Is it possible?
> Thanks in advance,
> --
> Chad Estioco
> BS Computer Science (ongoing)
> University of the Philippines-Diliman
> ==============================
> http://www.geocities.com/lokisky_walker
> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme

Posted on the users mailing list.