added static List.fill(size) { ... }
This commit is contained in:
1 parent
f1003f5b95
commit
a7ab0d3905
9 files changed
+64
-9
No files matched your search
@@ -45,6 +45,16 @@ You can concatenate lists or iterable objects:
|
||||
assert( [4,5] + (1..3) == [4, 5, 1, 2, 3])
|
||||
>>> void
|
||||
|
||||
## Constructing lists
|
||||
|
||||
Besides literals, you can build a list by size using `List.fill`:
|
||||
|
||||
val squares = List.fill(5) { i -> i * i }
|
||||
assertEquals([0, 1, 4, 9, 16], squares)
|
||||
>>> void
|
||||
|
||||
`List.fill(size) { ... }` calls the block once for each index from `0` to `size - 1` and returns a new mutable list.
|
||||
|
||||
## Appending
|
||||
|
||||
To append to lists, use `+=` with elements, lists and any [Iterable] instances, but beware it will
|
||||
@@ -164,6 +174,7 @@ List could be sorted in place, just like [Collection] provide sorted copies, in
|
||||
| `[index]` | get or set element at index | Int |
|
||||
| `[Range]` | get slice of the array (copy) | Range |
|
||||
| `+=` | append element(s) (2) | List or Obj |
|
||||
| `List.fill(size, block)` | build a new list from indices `0..<size` | Int, Callable |
|
||||
| `sort()` | in-place sort, natural order | void |
|
||||
| `sortBy(predicate)` | in-place sort bu `predicate` call result (3) | void |
|
||||
| `sortWith(comparator)` | in-place sort using `comarator` function (4) | void |
|
||||
|
||||
@@ -1020,6 +1020,14 @@ For example, we want to create an extension method that would test if a value ca
|
||||
assert( ! "5.2".isInteger() )
|
||||
>>> void
|
||||
|
||||
Extension methods normally act like instance members. If declared as `static`, they are called on the type object itself:
|
||||
|
||||
```lyng
|
||||
static fun List<T>.fill(size: Int, block: (Int)->T): List<T> { ... }
|
||||
|
||||
val tens = List.fill(5) { it * 10 }
|
||||
```
|
||||
|
||||
## Extension properties
|
||||
|
||||
Just like methods, you can extend existing classes with properties. These can be defined using simple initialization (for `val` only) or with custom accessors.
|
||||
|
||||
@@ -116,6 +116,7 @@ Primary sources used: `lynglib/src/commonMain/kotlin/net/sergeych/lyng/{Parser,T
|
||||
- shorthand: `fun f(x) = expr`.
|
||||
- generics: `fun f<T>(x: T): T`.
|
||||
- extension functions: `fun Type.name(...) { ... }`.
|
||||
- static extension functions are callable on the type object: `static fun List<T>.fill(...)` -> `List.fill(...)`.
|
||||
- delegated callable: `fun f(...) by delegate`.
|
||||
- Type aliases:
|
||||
- `type Name = TypeExpr`
|
||||
|
||||
@@ -46,7 +46,8 @@ Sources: `lynglib/src/commonMain/kotlin/net/sergeych/lyng/Script.kt`, `lynglib/s
|
||||
- Iteration/filtering: `forEach`, `filter`, `filterFlow`, `filterNotNull`, `filterFlowNotNull`, `drop`, `dropLast`, `takeLast`.
|
||||
- Search/predicates: `findFirst`, `findFirstOrNull`, `any`, `all`, `count`, `first`, `last`.
|
||||
- Mapping/aggregation: `map`, `flatMap`, `flatten`, `sum`, `sumOf`, `minOf`, `maxOf`.
|
||||
- Ordering: `sorted`, `sortedBy`, `shuffled`, `List.sort`, `List.sortBy`.
|
||||
- Ordering and list building: `sorted`, `sortedBy`, `shuffled`, `List.sort`, `List.sortBy`, `List.fill`.
|
||||
- `List.fill(size) { index -> ... }` constructs a new `List<T>` by evaluating the block once per index from `0` to `size - 1`.
|
||||
- String helper: `joinToString`, `String.re`.
|
||||
|
||||
### 4.3 Delegation helpers
|
||||
|
||||
@@ -811,6 +811,12 @@ Lyng has built-in mutable array class `List` with simple literals:
|
||||
many collection based methods are implemented there.
|
||||
For immutable list values, use `list.toImmutable()` and [ImmutableList].
|
||||
|
||||
To construct a list programmatically, use the static helper `List.fill`:
|
||||
|
||||
val tens = List.fill(5) { index -> index * 10 }
|
||||
assertEquals([0, 10, 20, 30, 40], tens)
|
||||
>>> void
|
||||
|
||||
Lists can contain any type of objects, lists too:
|
||||
|
||||
val list = [1, [2, 3], 4]
|
||||
|
||||
Reference in new issue
Block a user