Skip to content

Commit

Permalink
Simplify stream call chains
Browse files Browse the repository at this point in the history
  • Loading branch information
Baltoli committed Nov 1, 2023
1 parent deb9d2b commit 9f99530
Show file tree
Hide file tree
Showing 17 changed files with 25 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2034,9 +2034,7 @@ private void convert(

for (Tuple2<Tuple2<Att.Key, String>, ?> attribute :
// Sort to stabilize error messages
stream(att.att())
.sorted(Comparator.comparing(Tuple2::toString))
.collect(Collectors.toList())) {
stream(att.att()).sorted(Comparator.comparing(Tuple2::toString)).toList()) {
Att.Key key = attribute._1._1;
String strKey = key.key();
String clsName = attribute._1._2;
Expand Down Expand Up @@ -2095,7 +2093,7 @@ private void convertStringVarList(
else
throw KEMException.criticalError("No free variable found for " + s, location);
})
.collect(Collectors.toList());
.toList();
String conn = "";
for (KVariable var : variables) {
sb.append(conn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,7 @@ protected List<KApply> makeParents(KLabel parent, boolean ellipses, List<K> allC
List<K> children =
allChildren.stream().filter(t -> !(t instanceof KRewrite)).collect(Collectors.toList());
List<KRewrite> rewrites =
allChildren.stream()
.filter(t -> t instanceof KRewrite)
.map(t -> (KRewrite) t)
.collect(Collectors.toList());
allChildren.stream().filter(t -> t instanceof KRewrite).map(t -> (KRewrite) t).toList();

// see if all children can fit together
Set<Sort> usedCells = Sets.newHashSet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -503,9 +503,7 @@ private static Sort lub(
assert !entries.isEmpty();
entries = new HashSet<>(entries);
Collection<Sort> filteredEntries =
entries.stream()
.filter(s -> s != null && !s.name().equals(SORTPARAM_NAME))
.collect(Collectors.toList());
entries.stream().filter(s -> s != null && !s.name().equals(SORTPARAM_NAME)).toList();
if (filteredEntries.isEmpty()) { // if all sorts are parameters, take the first
return entries.iterator().next();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ public static Set<KLabel> filteredMapConstructors(Module m) {
return stream(m.productions())
.filter(p -> p.att().contains(Att.ASSOC()) && p.att().contains(Att.FILTER_ELEMENT()))
.map(p -> p.klabel().get())
.distinct()
.collect(Collectors.toSet());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public ExpandMacros(
stream(mod.rules())
.filter(r -> isMacro(r.att(), reverse))
.sorted(Comparator.comparingInt(r -> ModuleToKORE.getPriority(r.att())))
.collect(Collectors.toList());
.toList();
macros =
allMacros.stream()
.filter(r -> getLeft(r, reverse) instanceof KApply)
Expand Down
13 changes: 3 additions & 10 deletions kernel/src/main/java/org/kframework/compile/checks/CheckAtt.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ public void checkUnrecognizedModuleAtts() {
"Unrecognized attributes on module "
+ m.name()
+ ": "
+ stream(m.att().unrecognizedKeys())
.map(Key::toString)
.sorted()
.collect(Collectors.toList())
+ stream(m.att().unrecognizedKeys()).map(Key::toString).sorted().toList()
+ "\nHint: User-defined groups can be added with the group(_) attribute."));
}
}
Expand Down Expand Up @@ -84,10 +81,7 @@ private void checkUnrecognizedAtts(Sentence sentence) {
errors.add(
KEMException.compilerError(
"Unrecognized attributes: "
+ stream(sentence.att().unrecognizedKeys())
.map(Key::toString)
.sorted()
.collect(Collectors.toList())
+ stream(sentence.att().unrecognizedKeys()).map(Key::toString).sorted().toList()
+ "\nHint: User-defined groups can be added with the group(_) attribute.",
sentence));
}
Expand All @@ -99,8 +93,7 @@ private void checkRestrictedAtts(Sentence sentence) {
Set<Key> keys = stream(att.att().keySet()).map(k -> k._1()).collect(Collectors.toSet());
keys.removeIf(k -> k.allowedSentences().exists(c -> c.isAssignableFrom(cls)));
if (!keys.isEmpty()) {
List<String> sortedKeys =
keys.stream().map(k -> k.toString()).sorted().collect(Collectors.toList());
List<String> sortedKeys = keys.stream().map(k -> k.toString()).sorted().toList();
errors.add(
KEMException.compilerError(
cls.getSimpleName()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
package org.kframework.compile.checks;

import java.util.List;
import java.util.stream.Collectors;
import org.kframework.attributes.Att;
import org.kframework.kil.Module;
import org.kframework.kil.ModuleItem;
Expand All @@ -25,14 +24,12 @@ private static void check(ModuleItem i) {
for (Production p : b.getProductions()) {
if (p.containsAttribute(Att.BRACKET())) {
List<ProductionItem> nts =
p.getItems().stream()
.filter(x -> x instanceof NonTerminal)
.collect(Collectors.toList());
p.getItems().stream().filter(x -> x instanceof NonTerminal).toList();
if (nts.size() != 1
|| !((NonTerminal) nts.get(0)).getSort().equals(s.getDeclaredSort().getSort()))
throw KEMException.outerParserError(
"bracket productions should have exactly one non-terminal of the same sort as the"
+ " production.",
"bracket productions should have exactly one non-terminal "
+ "of the same sort as the production.",
p.getSource(),
p.getLocation());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,14 @@ public void check(Module mainMod) {
|| s.isEmpty()
|| (prod.att().contains(Att.CELL())
&& stream(prod.nonterminals())
.filter(
.anyMatch(
nt ->
klabels
.get(symbol)
.sortAttributesFor()
.get(nt.sort().head())
.getOrElse(() -> Att.empty())
.contains(Att.CELL_COLLECTION()))
.findAny()
.isPresent())) {
.contains(Att.CELL_COLLECTION())))) {
continue;
}
if (canonicalPath == null || !s.get().source().contains(canonicalPath)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
public class CheckListDecl {

public static void check(Module m) {
m.getItems().stream().forEach(CheckListDecl::check); // i -> check(i)
m.getItems().forEach(CheckListDecl::check); // i -> check(i)
}

private static final Set<Sort> BASE_SORTS =
Expand Down
1 change: 0 additions & 1 deletion kernel/src/main/java/org/kframework/kompile/Kompile.java
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,6 @@ private void checkIsSortPredicates(scala.collection.Set<Module> modules) {
m.productionsForSort()
.getOrElse(Sorts.Bool().head(), Set$.MODULE$::<Production>empty)))
.collect(Collectors.toSet())
.stream()
.forEach(
prod -> {
Seq<ProductionItem> items = prod.items();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -523,9 +523,7 @@ public CompletableFuture<List<? extends Location>> references(ReferenceParams pa

if (isPositionOverLocation(pos, nameLoc)) {
List<DefinitionItem> allDi =
files.values().stream()
.flatMap(doc -> doc.dis.stream())
.collect(Collectors.toList());
files.values().stream().flatMap(doc -> doc.dis.stream()).toList();
allDi.stream()
.filter(ddi -> ddi instanceof Module)
.map(ddi -> ((Module) ddi))
Expand Down Expand Up @@ -787,9 +785,7 @@ public CompletableFuture<List<SelectionRange>> selectionRange(SelectionRangePara
+ " #poss: "
+ poss
+ " rezDepth: "
+ lloc.stream()
.map(TextDocumentSyncHandler::getSelectionRangeDepth)
.collect(Collectors.toList()));
+ lloc.stream().map(TextDocumentSyncHandler::getSelectionRangeDepth).toList());

return lloc;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public Set<Module> loadModules(
groupedModules.entrySet().stream()
.filter(e -> e.getValue().size() > 1)
.map(Map.Entry::getKey)
.collect(Collectors.toList());
.toList();

int errors = 0;
for (String moduleName : duplicateModules) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ public static Tuple3<Module, Module, Module> getCombinedGrammarImpl(
List<Sort> allSorts =
stream(mod.allSorts())
.filter(s -> (!isParserSort(s) || s.equals(Sorts.KItem()) || s.equals(Sorts.K())))
.collect(Collectors.toList());
.toList();
for (SortHead sh : mutable(mod.definedInstantiations()).keySet()) {
for (Sort s : mutable(mod.definedInstantiations().apply(sh))) {
// syntax MInt{K} ::= MInt{6}
Expand Down Expand Up @@ -583,7 +583,7 @@ public static Tuple3<Module, Module, Module> getCombinedGrammarImpl(
.collect(Collectors.toSet());
}

disambProds = parseProds.stream().collect(Collectors.toSet());
disambProds = new HashSet<>(parseProds);
if (mod.importedModuleNames().contains(PROGRAM_LISTS)) {
Set<Sentence> prods3 = new HashSet<>();
// if no start symbol has been defined in the configuration, then use K
Expand All @@ -594,7 +594,7 @@ public static Tuple3<Module, Module, Module> getCombinedGrammarImpl(
}
}
// for each triple, generate a new pattern which works better for parsing lists in programs.
prods3.addAll(parseProds.stream().collect(Collectors.toSet()));
prods3.addAll(new HashSet<>(parseProds));
Set<Sentence> res = new HashSet<>();
for (UserList ul : UserList.getLists(prods3)) {
Production prod1, prod2, prod3 = null, prod4 = null, prod5 = null;
Expand Down Expand Up @@ -714,7 +714,7 @@ public static Tuple3<Module, Module, Module> getCombinedGrammarImpl(
stream(mod.importedModules())
.filter(m -> m.att().contains(Att.NOT_LR1()))
.map(Module::name)
.collect(Collectors.toList());
.toList();
if (!notLrModules.isEmpty()) {
att = att.add(Att.NOT_LR1_MODULES(), notLrModules.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public Tuple2<Either<Set<KEMException>, Term>, Set<KEMException>> apply(TermCons
orig);
java.util.Set<KEMException> warnings = new HashSet<>();

List<Term> reversed = tc.items().stream().collect(Collectors.toList());
List<Term> reversed = new ArrayList<>(tc.items());
Collections.reverse(
reversed); // TermCons with PStack requires the elements to be in the reverse order
Iterator<Term> items = reversed.iterator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ private void makeSubsorts(Module mod, String name, POSet<Sort> relations) {
for (Tuple2<Sort, Set<Sort>> relation :
stream(relations.relations())
.sorted(Comparator.comparing(t -> -ordinals.getOrDefault(t._1().head(), 0)))
.collect(Collectors.toList())) {
.toList()) {
if (!isRealSort(relation._1().head())) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public void appendScanner(
List<TerminalLike> ordered =
tokens.keySet().stream()
.sorted((t1, t2) -> tokens.get(t2)._2() - tokens.get(t1)._2())
.collect(Collectors.toList());
.toList();
for (TerminalLike key : ordered) {
if (key instanceof Terminal t) {
flex.append(StringUtil.enquoteCString(t.value()));
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/main/java/org/kframework/unparser/KPrint.java
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public Optional<K> filterEquality(K term, Multiset<KVariable> vars, Module mod)
return Optional.of(term);
}
Set<KVariable> leftVars = vars(kapp.items().get(0));
if (leftVars.stream().filter(v -> !v.att().contains(Att.ANONYMOUS())).findAny().isPresent()) {
if (leftVars.stream().anyMatch(v -> !v.att().contains(Att.ANONYMOUS()))) {
return Optional.of(term);
}
for (KVariable var : leftVars) {
Expand Down

0 comments on commit 9f99530

Please sign in to comment.