muScript: allMatch/anyMatch
ci/woodpecker/push/woodpecker Pipeline was successful Details

This commit is contained in:
Johannes Frohnmeyer 2023-03-12 17:50:29 +01:00
parent 5e3dae3ef9
commit ee7c5f8327
Signed by: Johannes
GPG Key ID: E76429612C2929F4
1 changed files with 14 additions and 0 deletions

View File

@ -61,6 +61,8 @@ public class StandardLib {
.set("isEmpty", StandardLib::isEmpty)
.set("concat", StandardLib::concat)
.set("filter", StandardLib::filter)
.set("allMatch", StandardLib::allMatch)
.set("anyMatch", StandardLib::anyMatch)
.set("map", StandardLib::map)
.set("flatMap", StandardLib::flatMap)
.set("fold", StandardLib::fold)
@ -160,6 +162,18 @@ public class StandardLib {
return of(args.get(0).asList().getValue().stream().filter(a -> dc.call(a).asBool().getValue()).toList());
}
public static DBool allMatch(DList args) {
if (args.size() != 2) throw new IllegalArgumentException("Invalid number of arguments for allMatch: expected 2 but got " + args.size());
DCallable dc = args.get(1).asCallable();
return of(args.get(0).asList().getValue().stream().allMatch(a -> dc.call(a).asBool().getValue()));
}
public static DBool anyMatch(DList args) {
if (args.size() != 2) throw new IllegalArgumentException("Invalid number of arguments for anyMatch: expected 2 but got " + args.size());
DCallable dc = args.get(1).asCallable();
return of(args.get(0).asList().getValue().stream().anyMatch(a -> dc.call(a).asBool().getValue()));
}
public static DList map(DList args) {
if (args.size() != 2) throw new IllegalArgumentException("Invalid number of arguments for map: expected 2 but got " + args.size());
return of(args.get(0).asList().getValue().stream().<Dynamic<?>>map(args.get(1).asCallable()::call).toList());