public class FoldLAlgo extends java.lang.Object implements IListAlgo
Modifier and Type | Field and Description |
---|---|
private IListAlgo |
helper
Helper visitor
Arguable, this visitor above is unnecessary, but idea here was
to stick to the general architecture of any forward accumulation
algorithm, which foldl represents.
|
Constructor and Description |
---|
FoldLAlgo() |
Modifier and Type | Method and Description |
---|---|
java.lang.Object |
emptyCase(MTList host,
java.lang.Object... acc)
Operates on
MTList , the empty list. |
java.lang.Object |
nonEmptyCase(NEList host,
java.lang.Object... acc)
Operates on
NEList , a non-empty list. |
private IListAlgo helper
public java.lang.Object emptyCase(MTList host, java.lang.Object... acc)
IListAlgo
MTList
, the empty list. Since
IEmptyList
has only one method, execute()
,
there is not much the host list can do here. So the code for the empty
case usually involves:inp
, and perhaps
host.execute(..., ...)
.public java.lang.Object nonEmptyCase(NEList host, java.lang.Object... acc)
IListAlgo
NEList
, a non-empty list. The host list now has
a first and a rest. So the code here usually involves what
host.getFirst()
and host.getRest()
can do to
accomplish the task at hand.host.getFirst()
is simply a data Object
that the host list holds. It is problem-specific, and thus what it can
do depends on the problem the current algorithm is trying to solve.
host.getRest()
in contrast is an IList
!
What can an IList
do? execute
an alogrithm
IListAlgo
with some input. What IListAlgo
can
that be? The current algorithm that is being executed is as good a
candidate as any other IListAlgo
. In Java, we use the key
word this
to reference the current receiver of the
method call. Having the rest of host (recursively) execute the current
algorithm is expressed in Java as:
host.getRest().execute(this, ...)
.To summarize, the code for the non-empty case usually involves:
host.getFirst()
, and the recursive call
host.getRest().execute(this, something involving inp)
.nonEmptyCase
in interface IListAlgo
host
- the NEList
that is executing this algorithmacc
- a variable number of input parameters that can be used for any purpose.