java.lang.Object
provided.listFW.visitors.FoldLAlgo
- All Implemented Interfaces:
IListAlgo
Performs the fold-left (forward accumulation) algorithm using the given IAccumulator
The general forward accumulation algorithm would not take an accumulator as a top-level
input parameter, instead the accumulator would be hidden inside the algorithm.
But since the fold process, makes the folding process invariant and the accumulator the
variant piece, the accumulator is thus necessarily an input parameter here.
- Author:
- swong
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
-
Field Details
-
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 Details
-
FoldLAlgo
public FoldLAlgo()
-
-
Method Details
-
emptyCase
Description copied from interface:IListAlgo
Operates onMTList
, the empty list. SinceIEmptyList
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 perhapshost.execute(..., ...)
.
-
nonEmptyCase
Description copied from interface:IListAlgo
Operates onNEList
, a non-empty list. The host list now has a first and a rest. So the code here usually involves whathost.getFirst()
andhost.getRest()
can do to accomplish the task at hand.host.getFirst()
is simply a dataObject
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 anIList
! What can anIList
do?execute
an alogrithmIListAlgo
with some input. WhatIListAlgo
can that be? The current algorithm that is being executed is as good a candidate as any otherIListAlgo
. In Java, we use the key wordthis
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 callhost.getRest().execute(this, something involving inp)
.
- Specified by:
nonEmptyCase
in interfaceIListAlgo
- Parameters:
host
- theNEList
that is executing this algorithmaccs
- a variable number of input parameters that can be used for any purpose.- Returns:
- result from calling this method. The type of the result is problem-specific and may be null.
-