mirror of
https://github.com/apple/pkl.git
synced 2026-07-08 05:55:13 +02:00
Add mapNonNullIndexed API to List and Set (#1063)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -554,6 +554,22 @@ public final class ListNodes {
|
||||
}
|
||||
}
|
||||
|
||||
public abstract static class mapIndexed extends ExternalMethod1Node {
|
||||
@Child private ApplyVmFunction2Node applyLambdaNode = ApplyVmFunction2NodeGen.create();
|
||||
|
||||
@Specialization
|
||||
protected VmList eval(VmList self, VmFunction function) {
|
||||
var builder = self.builder();
|
||||
long index = 0;
|
||||
|
||||
for (var elem : self) {
|
||||
builder.add(applyLambdaNode.execute(function, index++, elem));
|
||||
}
|
||||
LoopNode.reportLoopCount(this, self.getLength());
|
||||
return builder.build();
|
||||
}
|
||||
}
|
||||
|
||||
public abstract static class mapNonNull extends ExternalMethod1Node {
|
||||
@Child private ApplyVmFunction1Node applyLambdaNode = ApplyVmFunction1Node.create();
|
||||
|
||||
@@ -570,7 +586,7 @@ public final class ListNodes {
|
||||
}
|
||||
}
|
||||
|
||||
public abstract static class mapIndexed extends ExternalMethod1Node {
|
||||
public abstract static class mapNonNullIndexed extends ExternalMethod1Node {
|
||||
@Child private ApplyVmFunction2Node applyLambdaNode = ApplyVmFunction2NodeGen.create();
|
||||
|
||||
@Specialization
|
||||
@@ -579,7 +595,9 @@ public final class ListNodes {
|
||||
long index = 0;
|
||||
|
||||
for (var elem : self) {
|
||||
builder.add(applyLambdaNode.execute(function, index++, elem));
|
||||
var newValue = applyLambdaNode.execute(function, index++, elem);
|
||||
if (newValue instanceof VmNull) continue;
|
||||
builder.add(newValue);
|
||||
}
|
||||
LoopNode.reportLoopCount(this, self.getLength());
|
||||
return builder.build();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -335,6 +335,22 @@ public final class SetNodes {
|
||||
}
|
||||
}
|
||||
|
||||
public abstract static class mapIndexed extends ExternalMethod1Node {
|
||||
@Child private ApplyVmFunction2Node applyLambdaNode = ApplyVmFunction2NodeGen.create();
|
||||
|
||||
@Specialization
|
||||
protected VmSet eval(VmSet self, VmFunction function) {
|
||||
var builder = self.builder();
|
||||
long index = 0;
|
||||
|
||||
for (var elem : self) {
|
||||
builder.add(applyLambdaNode.execute(function, index++, elem));
|
||||
}
|
||||
LoopNode.reportLoopCount(this, self.getLength());
|
||||
return builder.build();
|
||||
}
|
||||
}
|
||||
|
||||
public abstract static class mapNonNull extends ExternalMethod1Node {
|
||||
@Child private ApplyVmFunction1Node applyLambdaNode = ApplyVmFunction1Node.create();
|
||||
|
||||
@@ -351,7 +367,7 @@ public final class SetNodes {
|
||||
}
|
||||
}
|
||||
|
||||
public abstract static class mapIndexed extends ExternalMethod1Node {
|
||||
public abstract static class mapNonNullIndexed extends ExternalMethod1Node {
|
||||
@Child private ApplyVmFunction2Node applyLambdaNode = ApplyVmFunction2NodeGen.create();
|
||||
|
||||
@Specialization
|
||||
@@ -360,7 +376,9 @@ public final class SetNodes {
|
||||
long index = 0;
|
||||
|
||||
for (var elem : self) {
|
||||
builder.add(applyLambdaNode.execute(function, index++, elem));
|
||||
var newValue = applyLambdaNode.execute(function, index++, elem);
|
||||
if (newValue instanceof VmNull) continue;
|
||||
builder.add(newValue);
|
||||
}
|
||||
LoopNode.reportLoopCount(this, self.getLength());
|
||||
return builder.build();
|
||||
|
||||
Reference in New Issue
Block a user