Skip to content

Commit

Permalink
update: update set-operations
Browse files Browse the repository at this point in the history
Added colon before each code block and split first explanation and code samples into two.
  • Loading branch information
AlejandraPedroza committed Oct 30, 2023
1 parent e3d1595 commit bf97afa
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 26 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,5 @@ dist
generated
.next
.teamcity/*.iml
.idea/misc.xml
.idea/vcs.xml
7 changes: 0 additions & 7 deletions .idea/misc.xml

This file was deleted.

12 changes: 0 additions & 12 deletions .idea/vcs.xml

This file was deleted.

23 changes: 16 additions & 7 deletions docs/topics/set-operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ or subtracting collections from each other.

To merge two collections into one, use the [`union()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/union.html)
function. It can be used in the infix form `a union b`.
Note that for ordered collections the order of the operands is important: in the resulting collection, the elements of the
first operand go before the elements of the second.

To find an intersection between two collections (elements present in both of them), use [`intersect()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/intersect.html).
To find collection elements not present in another collection, use [`subtract()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/subtract.html).
Both these functions can be called in the infix form as well, for example, `a intersect b`.
Note that for ordered collections the order of the operands is important. In the resulting collection, the elements of the
first operand go before the elements of the second:

```kotlin
fun main() {
Expand All @@ -19,7 +15,20 @@ fun main() {

println(numbers union setOf("four", "five"))
println(setOf("four", "five") union numbers)
// output according to the order
//sampleEnd
}
```
{kotlin-runnable="true" kotlin-min-compiler-version="1.3"}

To find an intersection between two collections (elements present in both of them), use [`intersect()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/intersect.html).
To find collection elements not present in another collection, use [`subtract()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/subtract.html).
Both these functions can be called in the infix form as well, for example, `a intersect b`:

```kotlin
fun main() {
//sampleStart
val numbers = setOf("one", "two", "three")
println(numbers intersect setOf("two", "one"))
println(numbers subtract setOf("three", "four"))
println(numbers subtract setOf("four", "three"))
Expand Down Expand Up @@ -47,7 +56,7 @@ fun main() {

You can also apply `union()`, `intersect()`, and `subtract()` to `List`.
However, their result is _always_ a `Set`, even on lists. In this result, all the duplicate elements are merged into one
and the index access is not available.
and the index access is not available:

```kotlin
fun main() {
Expand Down

0 comments on commit bf97afa

Please sign in to comment.