1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
//package org.acmsl.pocs.lambdafor;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
public class ControlFlow
{
public <C extends Collection<I>, I, R> Collection<R> forloop( final C items, final Function<I, R> lambda)
{
return functionalForLoop(items, lambda);
}
public <C extends Collection<I>, I, R> Collection<R> functionalForLoop( final C items, final Function<I, R> lambda)
{
return items.stream().map(lambda::apply).collect(Collectors.toList());
}
public Collection iterativeForloop( final Collection items, final Function lambda)
{
final List<Object> result = new ArrayList<>();
for (final Object item: items)
{
result.add(lambda.<Object>apply(item));
}
return result;
}
public <C extends Collection<I>, I, R> Collection<R> externallyDrivenForloop(
final ControlFlowDriver driver, final C items, final Function<I, R> lambda)
{
final List<R> result = new ArrayList<>(items.size());
final List<I> list = new ArrayList<>(items);
int position = -1;
while (true)
{
ControlFlowCommand command = driver.waitForCommand();
switch (command)
{
case NEXT:
position++;
break;
case PREVIOUS:
position++;
break;
case RELOAD:
break;
default:
break;
}
if (position < 0)
{
position = 0;
}
else if (position > list.size() - 1)
{
break;
}
result.set(position, lambda.apply(list.get(position)));
}
return result;
}
}
|