Skip to content

Commit

Permalink
remove and retire remaining usages of iterator pipelines
Browse files Browse the repository at this point in the history
  • Loading branch information
kspalaiologos committed Jul 15, 2023
1 parent 9897748 commit f0aef41
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 146 deletions.
128 changes: 0 additions & 128 deletions src/main/java/palaiologos/kamilalisp/atom/IteratorPipeline.java

This file was deleted.

20 changes: 13 additions & 7 deletions src/main/java/palaiologos/kamilalisp/runtime/graph/RS.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package palaiologos.kamilalisp.runtime.graph;

import org.jgrapht.traverse.DepthFirstIterator;
import org.jgrapht.traverse.RandomWalkVertexIterator;
import palaiologos.kamilalisp.atom.*;

Expand All @@ -8,17 +9,22 @@
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

public class RS extends PrimitiveFunction implements SpecialForm {
public class RS extends PrimitiveFunction implements Lambda {
@Override
public Atom apply(Environment env, List<Atom> args) {
assertArity(args, 3);
GraphWrapper w = Evaluation.evaluate(env, args.get(0)).getUserdata(GraphWrapper.class);
GraphWrapper w = args.get(0).getUserdata(GraphWrapper.class);
Atom start = args.get(1);
List<Atom> ops = args.get(2).getList();
Callable op = args.get(2).getCallable();
Atom init = args.size() > 3 ? args.get(3) : Atom.NULL;
Iterator<Atom> it = new RandomWalkVertexIterator<>(w.getGraph(), start);
Iterable<Atom> iterable = () -> it;
Stream<Atom> stream = StreamSupport.stream(iterable.spliterator(), false);
return IteratorPipeline.evaluate(env, ops, stream);
while (it.hasNext()) {
Atom a = it.next();
List<Atom> result = op.apply(env, List.of(a, init)).getList();
init = result.get(0);
if (result.get(1).coerceBool())
break;
}
return init;
}

@Override
Expand Down
35 changes: 24 additions & 11 deletions src/main/java/palaiologos/kamilalisp/runtime/graph/TOS.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package palaiologos.kamilalisp.runtime.graph;

import org.jgrapht.traverse.DepthFirstIterator;
import org.jgrapht.traverse.TopologicalOrderIterator;
import palaiologos.kamilalisp.atom.*;

Expand All @@ -9,24 +10,36 @@
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

public class TOS extends PrimitiveFunction implements SpecialForm {
public class TOS extends PrimitiveFunction implements Lambda {
@Override
public Atom apply(Environment env, List<Atom> args) {
if(args.size() == 3) {
GraphWrapper w = Evaluation.evaluate(env, args.get(0)).getUserdata(GraphWrapper.class);
GraphWrapper w = args.get(0).getUserdata(GraphWrapper.class);
Callable comparator = Evaluation.evaluate(env, args.get(1)).getCallable();
List<Atom> ops = args.get(2).getList();
Callable op = args.get(2).getCallable();
Atom init = args.size() > 3 ? args.get(3) : Atom.NULL;
Iterator<Atom> it = new TopologicalOrderIterator<>(w.getGraph(), (o1, o2) -> Evaluation.evaluate(env, comparator, List.of(o1, o2)).getInteger().intValueExact());
Iterable<Atom> iterable = () -> it;
Stream<Atom> stream = StreamSupport.stream(iterable.spliterator(), false);
return IteratorPipeline.evaluate(env, ops, stream);
while (it.hasNext()) {
Atom a = it.next();
List<Atom> result = op.apply(env, List.of(a, init)).getList();
init = result.get(0);
if (result.get(1).coerceBool())
break;
}
return init;
} else if(args.size() == 2) {
GraphWrapper w = Evaluation.evaluate(env, args.get(0)).getUserdata(GraphWrapper.class);
List<Atom> ops = args.get(2).getList();
GraphWrapper w = args.get(0).getUserdata(GraphWrapper.class);
Callable op = args.get(1).getCallable();
Atom init = args.size() > 2 ? args.get(3) : Atom.NULL;
Iterator<Atom> it = new TopologicalOrderIterator<>(w.getGraph(), Comparator.naturalOrder());
Iterable<Atom> iterable = () -> it;
Stream<Atom> stream = StreamSupport.stream(iterable.spliterator(), false);
return IteratorPipeline.evaluate(env, ops, stream);
while (it.hasNext()) {
Atom a = it.next();
List<Atom> result = op.apply(env, List.of(a, init)).getList();
init = result.get(0);
if (result.get(1).coerceBool())
break;
}
return init;
} else
throw new RuntimeException("Invalid arity");
}
Expand Down

0 comments on commit f0aef41

Please sign in to comment.