@@ -390,6 +390,20 @@ module Array {
390
390
`array .slice (number)`
391
391
}
392
392
393
+ /*
394
+ Drop n number of items from the right.
395
+
396
+ Array.drop(2, [1,2,3,4]) == [1,2]
397
+ */
398
+ fun dropRight (number : Number , array : Array (a )) : Array (a ) {
399
+ `
400
+ (() => {
401
+ if (number < 0 ) { return array }
402
+ return array .slice (0 , - number)
403
+ })()
404
+ `
405
+ }
406
+
393
407
/*
394
408
Group an array into sub groups of specified length (all items are included so
395
409
the last group maybe shorter if after grouping there is a remainder)
@@ -412,4 +426,204 @@ module Array {
412
426
})()
413
427
`
414
428
}
429
+
430
+ /*
431
+ Pushes a new item at the head of the array.
432
+
433
+ Array.unshift(2, [3,4]) == [2,3,4]
434
+ */
435
+ fun unshift (item : a , array : Array (a )) : Array (a ) {
436
+ `
437
+ (() => {
438
+ const result = Array .from (array)
439
+ result .unshift (item)
440
+ return result
441
+ })()
442
+ `
443
+ }
444
+
445
+ /*
446
+ Flattens an `Array(Maybe(a))` into an `Array(a)`, by unwrapping the items
447
+ and skipping nothings.
448
+
449
+ Array.compact([Maybe.just("A"), Maybe.nothing()]) == ["A"]
450
+ */
451
+ fun compact (array : Array (Maybe (a ))) : Array (a ) {
452
+ `
453
+ (() => {
454
+ const result = []
455
+
456
+ for (let item of array) {
457
+ if (item instanceof Just) {
458
+ result .push (item .value )
459
+ }
460
+ }
461
+
462
+ return result
463
+ })()
464
+ `
465
+ }
466
+
467
+ /*
468
+ Moves an item at the given index (`from`) to a new index (`to`).
469
+
470
+ The array is returned as is if:
471
+ * `from` and `to` are the same.
472
+ * a negative number is supplied to `from`
473
+ * a number is supplied to `from` which is grater the the length of the array
474
+
475
+ Array.move(-1, 1, ["A", "B", "C"]) == ["A", "B", "C"]
476
+ Array.move(10, 1, ["A", "B", "C"]) == ["A", "B", "C"]
477
+ Array.move(0, 0, ["A", "B", "C"]) == ["A", "B", "C"]
478
+
479
+ If a negative number is supplied to `to` then, the item is moved to the
480
+ first position.
481
+
482
+ Array.move(2, -1, ["A", "B", "C"]) == ["C", "A", "B"]
483
+
484
+ If a number is supplied to `to` which is grater the the length of the array,
485
+ then the item is moved to the last position.
486
+
487
+ Array.move(0, 10, ["A", "B", "C"]) == ["B", "C", "A"]
488
+ */
489
+ fun move (from : Number , to : Number , array : Array (a )) : Array (a ) {
490
+ `
491
+ (() => {
492
+ const result = Array .from (array)
493
+
494
+ if (from == to || from < 0 || from >= result .length ) {
495
+ return result
496
+ } else if (to < 0 ) {
497
+ /* If the desired position is lower then zero put at the front. */
498
+ result .unshift (result .splice (from, 1 )[0 ])
499
+ } else if (to >= result .length ) {
500
+ /* If the desired position is bigger then length put at the back. */
501
+ result .push (result .splice (from, 1 )[0 ])
502
+ } else {
503
+ /* Else we just move. */
504
+ result .splice (to, 0 , result .splice (from, 1 )[0 ])
505
+ }
506
+
507
+ return result
508
+ })()
509
+ `
510
+ }
511
+
512
+ /*
513
+ Inserts the given item into the given position of the given array.
514
+
515
+ Array.insertAt("a", 0, ["b","c"]) == ["a","b","c"]
516
+ */
517
+ fun insertAt (item : a , position : Number , array : Array (a )) : Array (a ) {
518
+ `
519
+ (() => {
520
+ const result = Array .from (array)
521
+
522
+ if (position <= 0 ) {
523
+ result .unshift (item)
524
+ } else {
525
+ result .splice (position, 0 , item)
526
+ }
527
+
528
+ return result
529
+ })()
530
+ `
531
+ }
532
+
533
+ /*
534
+ Spaws the items at the given indexes of the given array. It returns the array
535
+ unchanged if there is no item at any of the given indexs.
536
+
537
+ Array.swap(0, 1, ["a","b"]) == ["b", "a"]
538
+ */
539
+ fun swap (index1 : Number , index2 : Number , array : Array (a )) : Array (a ) {
540
+ `
541
+ (() => {
542
+ if (index1 < 0 ||
543
+ index2 < 0 ||
544
+ index1 >= array .length ||
545
+ index2 >= array .length ) {
546
+ return array
547
+ }
548
+
549
+ const result = Array .from (array)
550
+ const saved = result[index1]
551
+ result[index1] = result[index2]
552
+ result[index2] = saved;
553
+ return result
554
+ })()
555
+ `
556
+ }
557
+
558
+ /* Deletes the item of an array with the given index. */
559
+ fun deleteAt (index : Number , array : Array (a )) : Array (a ) {
560
+ `
561
+ (() => {
562
+ if (index < 0 || index >= array .length ) { return array }
563
+ const result = Array .from (array)
564
+ result .splice (index, 1 )
565
+ return result
566
+ })()
567
+ `
568
+ }
569
+
570
+ /*
571
+ Updates the item at the given index of the given array using the given
572
+ function.
573
+
574
+ Array.updateAt(
575
+ 2, (number : Number) : Number => {
576
+ number + 2
577
+ }, [0,1,2]) == [0,1,4]
578
+ */
579
+ fun updateAt (
580
+ index : Number ,
581
+ method : Function (a , a ),
582
+ array : Array (a )
583
+ ) : Array (a ) {
584
+ `
585
+ (() => {
586
+ if (array[index]) {
587
+ return #{setAt (index, method (` array[index]` ), array)}
588
+ } else {
589
+ return array
590
+ }
591
+ })()
592
+ `
593
+ }
594
+
595
+ /*
596
+ Sets the item at the given index to the given item of the given array.
597
+
598
+ Array.setAt(2, 5, [1,2,3]) == [1,2,5]
599
+ */
600
+ fun setAt (index : Number , item : a , array : Array (a )) : Array (a ) {
601
+ `
602
+ (() => {
603
+ if (index < 0 || index >= array .length ) { return array }
604
+ const result = Array .from (array)
605
+ result[index] = item
606
+ return result
607
+ })()
608
+ `
609
+ }
610
+
611
+ /*
612
+ Returns the index of the given item in the given array.
613
+
614
+ Arrray.indexOf("a", ["a","b","c"]) == 1
615
+ */
616
+ fun indexOf (item : a , array : Array (a )) : Number {
617
+ `
618
+ (() => {
619
+ for (let index = 0 ; index < array .length ; index++ ) {
620
+ if (_compare (item, array[index])) {
621
+ return index
622
+ }
623
+ }
624
+
625
+ return - 1
626
+ })()
627
+ `
628
+ }
415
629
}
0 commit comments