aboutsummaryrefslogtreecommitdiff
path: root/api/lists.yaml
blob: c746cf57a72d502ef5f458915b90930f0ef12702 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
List.binary_search:
  description: >
    Performs a binary search on a sorted list.
  return:
    type: 'Int'
    description: >
      Assuming the input list is sorted according to the given comparison function,
      return the index where the given item would be inserted to maintain the sorted
      order. That is, if the item is found, return its index, otherwise return the
      place where it would be found if it were inserted and the list were sorted.
  args:
    list:
      type: '[T]'
      description: >
        The sorted list to search.
    by:
      type: 'func(x,y:&T->Int32)'
      default: 'T.compare'
      description: >
        The comparison function used to determine order. If not specified, the
        default comparison function for the item type will be used.
  example: |
    >> [1, 3, 5, 7, 9].binary_search(5)
    = 3
    
    >> [1, 3, 5, 7, 9].binary_search(-999)
    = 1
    
    >> [1, 3, 5, 7, 9].binary_search(999)
    = 6
    
List.by:
  description: >
    Creates a new list with elements spaced by the specified step value.
  return:
    type: '[T]'
    description: >
      A new list with every `step`-th element from the original list.
  args:
    list:
      type: '[T]'
      description: >
        The original list.
    step:
      type: 'Int'
      description: >
        The step value for selecting elements.
  example: |
    >> [1, 2, 3, 4, 5, 6].by(2)
    = [1, 3, 5]
    
List.clear:
  description: >
    Clears all elements from the list.
  return:
    type: 'Void'
    description: >
      Nothing.
  args:
    list:
      type: '@[T]'
      description: >
        The mutable reference to the list to be cleared.
  example: |
    >> my_list.clear()
    
List.counts:
  description: >
    Counts the occurrences of each element in the list.
  return:
    type: '{T=Int}'
    description: >
      A table mapping each element to its count.
  args:
    list:
      type: '[T]'
      description: >
        The list to count elements in.
  example: |
    >> [10, 20, 30, 30, 30].counts()
    = {10=1, 20=1, 30=3}
    
List.find:
  description: >
    Finds the index of the first occurrence of an element (if any).
  return:
    type: 'Int?'
    description: >
      The index of the first occurrence or `!Int` if not found.
  args:
    list:
      type: '[T]'
      description: >
        The list to search through.
    target:
      type: 'T'
      description: >
        The item to search for.
  example: |
    >> [10, 20, 30, 40, 50].find(20)
    = 2 : Int?
    
    >> [10, 20, 30, 40, 50].find(9999)
    = none : Int?
    
List.first:
  description: >
    Find the index of the first item that matches a predicate function (if any).
  return:
    type: 'Int'
    description: >
      Returns the index of the first item where the predicate is true or `!Int` if no
      item matches.
  args:
    list:
      type: '[T]'
      description: >
        The list to search through.
    predicate:
      type: 'func(item:&T -> Bool)'
      description: >
        A function that returns `yes` if the item should be returned or
        `no` if it should not.
  example: |
    >> [4, 5, 6].find(func(i:&Int): i.is_prime())
    = 5 : Int?
    >> [4, 6, 8].find(func(i:&Int): i.is_prime())
    = none : Int?
    
List.from:
  description: >
    Returns a slice of the list starting from a specified index.
  return:
    type: '[T]'
    description: >
      A new list starting from the specified index.
  args:
    list:
      type: '[T]'
      description: >
        The original list.
    first:
      type: 'Int'
      description: >
        The index to start from.
  example: |
    >> [10, 20, 30, 40, 50].from(3)
    = [30, 40, 50]
    
List.has:
  description: >
    Checks if the list has an element.
  return:
    type: 'Bool'
    description: >
      `yes` if the list has the element, `no` otherwise.
  args:
    list:
      type: '[T]'
      description: >
        The list to check.
    target:
      type: 'T'
      description: >
        The element to check for.
  example: |
    >> [10, 20, 30].has(20)
    = yes
    
List.heap_pop:
  description: >
    Removes and returns the top element of a heap or `none` if the list is empty.
    By default, this is the *minimum* value in the heap.
  return:
    type: 'T?'
    description: >
      The removed top element of the heap or `none` if the list is empty.
  args:
    list:
      type: '@[T]'
      description: >
        The mutable reference to the heap.
    by:
      type: 'func(x,y:&T->Int32)'
      default: 'T.compare'
      description: >
        The comparison function used to determine order. If not specified, the
        default comparison function for the item type will be used.
  example: |
    >> my_heap := [30, 10, 20]
    >> my_heap.heapify()
    >> my_heap.heap_pop()
    = 10
    
List.heap_push:
  description: >
    Adds an element to the heap and maintains the heap property. By default, this
    is a *minimum* heap.
  return:
    type: 'Void'
    description: >
      Nothing.
  args:
    list:
      type: '@[T]'
      description: >
        The mutable reference to the heap.
    item:
      type: 'T'
      description: >
        The item to be added.
    by:
      default: 'T.compare'
      description: >
        The comparison function used to determine order. If not specified, the
        default comparison function for the item type will be used.
  example: |
    >> my_heap.heap_push(10)
    
List.heapify:
  description: >
    Converts a list into a heap.
  return:
    type: 'Void'
    description: >
      Nothing.
  args:
    list:
      type: '@[T]'
      description: >
        The mutable reference to the list to be heapified.
    by:
      type: 'func(x,y:&T->Int32)'
      default: 'T.compare'
      description: >
        The comparison function used to determine order. If not specified, the
        default comparison function for the item type will be used.
  example: |
    >> my_heap := [30, 10, 20]
    >> my_heap.heapify()
    
List.insert:
  description: >
    Inserts an element at a specified position in the list.
  return:
    type: 'Void'
    description: >
      Nothing.
  args:
    list:
      type: '@[T]'
      description: >
        The mutable reference to the list.
    item:
      type: 'T'
      description: >
        The item to be inserted.
    at:
      type: 'Int'
      default: '0'
      description: >
        The index at which to insert the item.
  note: >
    Since indices are 1-indexed and negative indices mean "starting from the
    back", an index of `0` means "after the last item".
  example: |
    >> list := [10, 20]
    >> list.insert(30)
    >> list
    = [10, 20, 30]
    
    >> list.insert(999, at=2)
    >> list
    = [10, 999, 20, 30]
    
List.insert_all:
  description: >
    Inserts a list of items at a specified position in the list.
  return:
    type: 'Void'
    description: >
      Nothing.
  args:
    list:
      type: '@[T]'
      description: >
        The mutable reference to the list.
    items:
      type: '[T]'
      description: >
        The items to be inserted.
    at:
      type: 'Int'
      default: '0'
      description: >
        The index at which to insert the item.
  note: >
    Since indices are 1-indexed and negative indices mean "starting from the
    back", an index of `0` means "after the last item".
  example: |
    list := [10, 20]
    list.insert_all([30, 40])
    >> list
    = [10, 20, 30, 40]
    
    list.insert_all([99, 100], at=2)
    >> list
    = [10, 99, 100, 20, 30, 40]
    
List.pop:
  description: >
    Removes and returns an item from the list. If the given index is present in
    the list, the item at that index will be removed and the list will become one
    element shorter.
  return:
    type: 'T?'
    description: >
      `none` if the list is empty or the given index does not exist in the list,
      otherwise the item at the given index.
  args:
    list:
      type: '&[T]'
      description: >
        The list to remove an item from.
    index:
      type: 'Int'
      default: '-1'
      description: >
        The index from which to remove the item.
  note: >
    Since negative indices are counted from the back, the default behavior is
    to pop the last value.
  example: |
    >> list := [10, 20, 30, 40]
    
    >> list.pop()
    = 40
    >> list
    = &[10, 20, 30]
    
    >> list.pop(index=2)
    = 20
    >> list
    = &[10, 30]
    
List.random:
  description: >
    Selects a random element from the list.
  return:
    type: 'T'
    description: >
      A random element from the list.
  args:
    list:
      type: '[T]'
      description: >
        The list from which to select a random element.
    random:
      type: 'func(min,max:Int64->Int64)?'
      default: 'none'
      description: >
        If provided, this function will be used to get a random index in the list. Returned
        values must be between `min` and `max` (inclusive). (Used for deterministic pseudorandom number
        generation)
  example: |
    >> [10, 20, 30].random()
    = 20
    
List.remove_at:
  description: >
    Removes elements from the list starting at a specified index.
  return:
    type: 'Void'
    description: >
      Nothing.
  args:
    list:
      type: '@[T]'
      description: >
        The mutable reference to the list.
    at:
      type: 'Int'
      default: '-1'
      description: >
        The index at which to start removing elements.
    count:
      type: 'Int'
      default: '1'
      description: >
        The number of elements to remove.
  note: >
    Since negative indices are counted from the back, the default behavior is
    to remove the last item.
  example: |
    list := [10, 20, 30, 40, 50]
    list.remove_at(2)
    >> list
    = [10, 30, 40, 50]
    
    list.remove_at(2, count=2)
    >> list
    = [10, 50]
    
List.remove_item:
  description: >
    Removes all occurrences of a specified item from the list.
  return:
    type: 'Void'
    description: >
      Nothing.
  args:
    list:
      type: '@[T]'
      description: >
        The mutable reference to the list.
    item:
      type: 'T'
      description: >
        The item to be removed.
    max_count:
      type: 'Int'
      default: '-1'
      description: >
        The maximum number of occurrences to remove.
  note: >
    A negative `max_count` means "remove all occurrences".
  example: |
    list := [10, 20, 10, 20, 30]
    list.remove_item(10)
    >> list
    = [20, 20, 30]
    
    list.remove_item(20, max_count=1)
    >> list
    = [20, 30]
    
List.reversed:
  description: >
    Returns a reversed slice of the list.
  return:
    type: '[T]'
    description: >
      A slice of the list with elements in reverse order.
  args:
    list:
      type: '[T]'
      description: >
        The list to be reversed.
  example: |
    >> [10, 20, 30].reversed()
    = [30, 20, 10]
    
List.sample:
  description: >
    Selects a sample of elements from the list, optionally with weighted
    probabilities.
  return:
    type: '[T]'
    description: >
      A list of sampled elements from the list.
  errors: >
    Errors will be raised if any of the following conditions occurs:
    - The given list has no elements and `count >= 1`
    - `count < 0` (negative count)
    - The number of weights provided doesn't match the length of the list. 
    - Any weight in the weights list is negative, infinite, or `NaN`
    - The sum of the given weights is zero (zero probability for every element).
  args:
    list:
      type: '[T]'
      description: >
        The list to sample from.
    count:
      type: 'Int'
      description: >
        The number of elements to sample.
    weights:
      type: '[Num]?'
      default: '![Num]'
      description: >
        The probability weights for each element in the list. These
        values do not need to add up to any particular number, they are relative
        weights. If no weights are given, elements will be sampled with uniform
        probability.
    random:
      type: 'func(->Num)?'
      default: 'none'
      description: >
        If provided, this function will be used to get random values for
        sampling the list. The provided function should return random numbers
        between `0.0` (inclusive) and `1.0` (exclusive). (Used for deterministic
        pseudorandom number generation)
  example: |
    >> [10, 20, 30].sample(2, weights=[90%, 5%, 5%])
    = [10, 10]
    
List.shuffle:
  description: >
    Shuffles the elements of the list in place.
  return:
    type: 'Void'
    description: >
      Nothing.
  args:
    list:
      type: '@[T]'
      description: >
        The mutable reference to the list to be shuffled.
    random:
      type: 'func(min,max:Int64->Int64)?'
      default: 'none'
      description: >
        If provided, this function will be used to get a random index in the list. Returned
        values must be between `min` and `max` (inclusive). (Used for deterministic pseudorandom number
        generation)
  example: |
    >> list.shuffle()
    
List.shuffled:
  description: >
    Creates a new list with elements shuffled.
  return:
    type: '[T]'
    description: >
      A new list with shuffled elements.
  args:
    list:
      type: '[T]'
      description: >
        The list to be shuffled.
    random:
      type: 'func(min,max:Int64->Int64)?'
      default: 'none'
      description: >
        If provided, this function will be used to get a random index in the list. Returned
        values must be between `min` and `max` (inclusive). (Used for deterministic pseudorandom number
        generation)
  example: |
    >> [10, 20, 30, 40].shuffled()
    = [40, 10, 30, 20]
    
List.slice:
  description: >
    Returns a slice of the list spanning the given indices (inclusive).
  return:
    type: '[T]'
    description: >
      A new list spanning the given indices. Note: negative indices are counted from
      the back of the list, so `-1` refers to the last element, `-2` the
      second-to-last, and so on.
  args:
    list:
      type: '[T]'
      description: >
        The original list.
    from:
      type: 'Int'
      description: >
        The first index to include.
    to:
      type: 'Int'
      description: >
        The last index to include.
  example: |
    >> [10, 20, 30, 40, 50].slice(2, 4)
    = [20, 30, 40]
    
    >> [10, 20, 30, 40, 50].slice(-3, -2)
    = [30, 40]
    
List.sort:
  description: >
    Sorts the elements of the list in place in ascending order (small to large).
  return:
    type: 'Void'
    description: >
      Nothing.
  args:
    list:
      type: '@[T]'
      description: >
        The mutable reference to the list to be sorted.
    by:
      default: 'T.compare'
      description: >
        The comparison function used to determine order. If not specified, the
        default comparison function for the item type will be used.
  example: |
    list := [40, 10, -30, 20]
    list.sort()
    >> list
    = [-30, 10, 20, 40]
    
    list.sort(func(a,b:&Int): a.abs() <> b.abs())
    >> list
    = [10, 20, -30, 40]
    
List.sorted:
  description: >
    Creates a new list with elements sorted.
  return:
    type: '[T]'
    description: >
      A new list with sorted elements.
  args:
    list:
      type: '[T]'
      description: >
        The list to be sorted.
    by:
      default: 'T.compare'
      description: >
        The comparison function used to determine order. If not specified, the
        default comparison function for the item type will be used.
  example: |
    >> [40, 10, -30, 20].sorted()
    = [-30, 10, 20, 40]
    
    >> [40, 10, -30, 20].sorted(func(a,b:&Int): a.abs() <> b.abs())
    = [10, 20, -30, 40]
    
List.to:
  description: >
    Returns a slice of the list from the start of the original list up to a specified index (inclusive).
  return:
    type: '[T]'
    description: >
      A new list containing elements from the start up to the specified index.
  args:
    list:
      type: '[T]'
      description: >
        The original list.
    last:
      type: 'Int'
      description: >
        The index up to which elements should be included.
  example: |
    >> [10, 20, 30, 40, 50].to(3)
    = [10, 20, 30]
    
    >> [10, 20, 30, 40, 50].to(-2)
    = [10, 20, 30, 40]
    
List.unique:
  description: >
    Returns a Set that contains the unique elements of the list.
  return:
    type: '|T|'
    description: >
      A set containing only unique elements from the list.
  args:
    list:
      type: '[T]'
      description: >
        The list to process.
  example: |
    >> [10, 20, 10, 10, 30].unique()
    = {10, 20, 30}