feat: difference between lists #55

Open
opened 2025-12-30 01:20:11 +01:00 by adam · 1 comment
Owner

Originally created by @ZackarySantana on GitHub (Feb 11, 2024).

Currently, I use this utility function to find the difference A - B

// listDifference gets the elements in A that are not in B.
function listDifference(A: List<Any>, B: List<Any>): List<Any> = A.filter((s) -> !B.contains(s))

It would be a nice to have to be able to do A.difference(B) and have the same resulting list.

I read the contributing to implement this myself but I don't think there's enough docs to do so. I noticed that external (built-in) types (like lists) often have an external modifier for example, and in the docs it doesn't say how that links to anything.

Originally created by @ZackarySantana on GitHub (Feb 11, 2024). Currently, I use this utility function to find the difference A - B ``` // listDifference gets the elements in A that are not in B. function listDifference(A: List<Any>, B: List<Any>): List<Any> = A.filter((s) -> !B.contains(s)) ``` It would be a nice to have to be able to do A.difference(B) and have the same resulting list. I read the contributing to implement this myself but I don't think there's enough docs to do so. I noticed that external (built-in) types (like lists) often have an external modifier for example, and in the docs it doesn't say how that links to anything.
Author
Owner

@bioball commented on GitHub (Feb 13, 2024):

Fair enough; seems sensible to have this.

FYI: our Set API does have a difference:

s1: Set<Int> = Set(1, 2, 3)

s2: Set<Int> = Set(2, 3, 4)

s3 = s1.difference(s2)

Feel free to submit a PR! Take a look at ListNodes.java for where our List methods are implemented.

You will also need to add tests here: https://github.com/apple/pkl/blob/main/pkl-core/src/test/files/LanguageSnippetTests/input/api/list.pkl.

@bioball commented on GitHub (Feb 13, 2024): Fair enough; seems sensible to have this. FYI: our `Set` API does have a `difference`: ```groovy s1: Set<Int> = Set(1, 2, 3) s2: Set<Int> = Set(2, 3, 4) s3 = s1.difference(s2) ``` Feel free to submit a PR! Take a look at [ListNodes.java](https://github.com/apple/pkl/blob/main/pkl-core/src/main/java/org/pkl/core/stdlib/base/ListNodes.java) for where our List methods are implemented. You will also need to add tests here: https://github.com/apple/pkl/blob/main/pkl-core/src/test/files/LanguageSnippetTests/input/api/list.pkl.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/pkl#55