diff --git a/concepts/ranges/.meta/config.json b/concepts/ranges/.meta/config.json new file mode 100644 index 00000000..f70347ec --- /dev/null +++ b/concepts/ranges/.meta/config.json @@ -0,0 +1,7 @@ +{ + "authors": [ + "colinleach" + ], + "contributors": [], + "blurb": "Ranges represent a sequence of consecutive elements, ascending or descending, and typically integers or characters." +} diff --git a/concepts/ranges/about.md b/concepts/ranges/about.md new file mode 100644 index 00000000..4443fc7e --- /dev/null +++ b/concepts/ranges/about.md @@ -0,0 +1,56 @@ +# About Ranges + +Suppose you want all the non-negative integers up to 1000. +It would be ridiculous if you had to type all these into a list! + +Kotlin, like many languages, has a [`Range`][ref-range] type to simplify this. + +Most simply, a range can be consecutive integers. +To make clear what is included, most of the following examples convert the [`Range`][ref-range-lib] type (a [`lazy`][wiki-lazy] iterator) to a [`List`][concept-list]. + +```kotlin +// closed range +1..5 // => 1..5 +(1..5).toList() // => [1, 2, 3, 4, 5] + +// open range +1..<5 // => 1..4 +(1..<5).toList() // => [1, 2, 3, 4] +``` + +Note that the end limit is included in the range by default (a "closed" range). + +To get an "open" range, omitting the end limit, use `..<`. +This more closely mimics some other languages such as Python. + +A `step` keyword can be included, but the `..` syntax only allows positive steps. +Use `downTo` for descending sequences. + +```kotlin +(1..5 step 2).toList() // => [1, 3, 5] + +(5 downTo 1).toList() // => [5, 4, 3, 2, 1] +(5 downTo 1 step 2).toList() // => [5, 3, 1] +``` + +[Characters][concept-chars] (not just ASCII) can be used instead of numbers: + +```kotlin +('a'..'e').toList() // => [a, b, c, d, e] + +// Devanagari +('ऄ'..'ई').toList() // => [ऄ, अ, आ, इ, ई] +``` + +~~~~exercism/note +The output listed here after a `=>` was generated interactively by [Kotlin Notebook][notebook], which omits quotes from characters and strings (for readability). +Assume that all the characters would be surrounded by `' '` in code. + +[notebook]: https://kotlinlang.org/docs/kotlin-notebook-overview.html +~~~~ + +[ref-range]: https://kotlinlang.org/docs/ranges.html#range +[ref-range-lib]: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.ranges/ +[wiki-lazy]: https://en.wikipedia.org/wiki/Lazy_evaluation +[concept-list]: https://exercism.org/tracks/kotlin/concepts/lists +[concept-chars]: https://exercism.org/tracks/kotlin/concepts/chars diff --git a/concepts/ranges/introduction.md b/concepts/ranges/introduction.md new file mode 100644 index 00000000..b1bfe101 --- /dev/null +++ b/concepts/ranges/introduction.md @@ -0,0 +1,47 @@ +# Introduction + +Suppose you want all the non-negative integers up to 1000. +It would be ridiculous if you had to type all these into a list! + +Kotlin, like many languages, has a `Range` type to simplify this. + +Most simply, a range can be consecutive integers. +To make clear what is included, most of the following examples convert the `Range` to a [`List`][concept-list]. + +```kotlin +// closed range +1..5 // => 1..5 +(1..5).toList() // => [1, 2, 3, 4, 5] + +// open range +1..<5 // => 1..4 +(1..<5).toList() // => [1, 2, 3, 4] +``` + +Note that the end limit is included in the range by default (a "closed" range). + +To get an "open" range, omitting the end limit, use `..<`. +This more closely mimics some other languages such as Python. + +A `step` keyword can be included, but the `..` syntax only allows positive steps. +Use `downTo` for descending sequences. + +```kotlin +(1..5 step 2).toList() // => [1, 3, 5] + +(5 downTo 1).toList() // => [5, 4, 3, 2, 1] +(5 downTo 1 step 2).toList() // => [5, 3, 1] +``` + +[Characters][concept-chars] (not just ASCII) can be used instead of numbers: + +```kotlin +('a'..'e').toList() // => [a, b, c, d, e] + +// Devanagari +('ऄ'..'ई').toList() // => [ऄ, अ, आ, इ, ई] +``` + +[ref-range]: https://kotlinlang.org/docs/ranges.html#range +[concept-list]: https://exercism.org/tracks/kotlin/concepts/lists +[concept-chars]: https://exercism.org/tracks/kotlin/concepts/chars diff --git a/concepts/ranges/links.json b/concepts/ranges/links.json new file mode 100644 index 00000000..ebcef2fb --- /dev/null +++ b/concepts/ranges/links.json @@ -0,0 +1,10 @@ +[ + { + "url": "https://kotlinlang.org/docs/ranges.html#range", + "description": "Kotlin introduction to ranges." + }, + { + "url": "https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.ranges/", + "description": "Library reference for Range type." + } +] diff --git a/config.json b/config.json index ba7a0c1d..4ea3b7b1 100644 --- a/config.json +++ b/config.json @@ -1387,6 +1387,11 @@ "uuid": "168827c0-4867-449a-ad22-611c87314c48", "slug": "conditionals", "name": "Conditionals" + }, + { + "uuid": "f0f07860-e8de-4ae5-b182-abc9c65b18ec", + "slug": "ranges", + "name": "Ranges" } ], "key_features": [