apple / pkl

A configuration as code language with rich validation and tooling.
https://pkl-lang.org
Apache License 2.0
10.33k stars 278 forks source link

feat: difference between lists #125

Open ZackarySantana opened 9 months ago

ZackarySantana commented 9 months ago

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.

bioball commented 9 months ago

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.