@grain/stdlib 0.5.0 → 0.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/list.gr CHANGED
@@ -548,6 +548,79 @@ export let unique = list => {
548
548
  iter(list, [])
549
549
  }
550
550
 
551
+ /**
552
+ * Produces a new list filled with tuples of elements from both given lists.
553
+ * The first tuple will contain the first item of each list, the second tuple
554
+ * will contain the second item of each list, and so on.
555
+ *
556
+ * Calling this function with lists of different sizes will cause the returned
557
+ * list to have the length of the smaller list.
558
+ *
559
+ * @param list1: The list to provide values for the first tuple element
560
+ * @param list2: The list to provide values for the second tuple element
561
+ * @returns The new list containing indexed pairs of `(a, b)`
562
+ *
563
+ * @example List.zip([1, 2, 3], [4, 5, 6]) // [(1, 4), (2, 5), (3, 6)]
564
+ * @example List.zip([1, 2, 3], [4, 5]) // [(1, 4), (2, 5)]
565
+ *
566
+ * @since v0.5.3
567
+ */
568
+ export let zip = (list1, list2) => {
569
+ let rec zipInner = (list1, list2, acc) => {
570
+ match ((list1, list2)) {
571
+ ([first1, ...rest1], [first2, ...rest2]) =>
572
+ zipInner(rest1, rest2, [(first1, first2), ...acc]),
573
+ _ => acc,
574
+ }
575
+ }
576
+ reverse(zipInner(list1, list2, []))
577
+ }
578
+
579
+ /**
580
+ * Produces a new list filled with elements defined by applying a function on
581
+ * pairs from both given lists. The first element will contain the result of
582
+ * applying the function to the first elements of each list, the second element
583
+ * will contain the result of applying the function to the second elements of
584
+ * each list, and so on.
585
+ *
586
+ * Calling this function with lists of different sizes will cause the returned
587
+ * list to have the length of the smaller list.
588
+ *
589
+ * @param fn: The function to apply to pairs of elements
590
+ * @param list1: The list whose elements will each be passed to the function as the first argument
591
+ * @param list2: The list whose elements will each be passed to the function as the second argument
592
+ * @returns The new list containing elements derived from applying the function to pairs of input list elements
593
+ *
594
+ * @example List.zipWith((a, b) => a + b, [1, 2, 3], [4, 5, 6]) // [5, 7, 9]
595
+ * @example List.zipWith((a, b) => a * b, [1, 2, 3], [4, 5]) // [4, 10]
596
+ *
597
+ * @since v0.5.3
598
+ */
599
+ export let zipWith = (fn, list1, list2) => {
600
+ let rec zipWithInner = (list1, list2, acc) => {
601
+ match ((list1, list2)) {
602
+ ([first1, ...rest1], [first2, ...rest2]) =>
603
+ zipWithInner(rest1, rest2, [fn(first1, first2), ...acc]),
604
+ _ => acc,
605
+ }
606
+ }
607
+ reverse(zipWithInner(list1, list2, []))
608
+ }
609
+
610
+ /**
611
+ * Produces two lists by splitting apart a list of tuples.
612
+ *
613
+ * @param list: The list of tuples to split
614
+ * @returns An list containing all elements from the first tuple element, and a list containing all elements from the second tuple element
615
+ *
616
+ * @since v0.5.3
617
+ */
618
+ export let unzip = list => {
619
+ reduceRight(((first, second), (firstUnzipped, secondUnzipped)) => {
620
+ ([first, ...firstUnzipped], [second, ...secondUnzipped])
621
+ }, ([], []), list)
622
+ }
623
+
551
624
  /**
552
625
  * Produces a new list with the specified number of elements removed from
553
626
  * the beginning of the input list.
package/list.md CHANGED
@@ -819,6 +819,116 @@ Returns:
819
819
  |----|-----------|
820
820
  |`List<a>`|The new list with only unique values|
821
821
 
822
+ ### List.**zip**
823
+
824
+ <details disabled>
825
+ <summary tabindex="-1">Added in <code>0.5.3</code></summary>
826
+ No other changes yet.
827
+ </details>
828
+
829
+ ```grain
830
+ zip : (List<a>, List<b>) -> List<(a, b)>
831
+ ```
832
+
833
+ Produces a new list filled with tuples of elements from both given lists.
834
+ The first tuple will contain the first item of each list, the second tuple
835
+ will contain the second item of each list, and so on.
836
+
837
+ Calling this function with lists of different sizes will cause the returned
838
+ list to have the length of the smaller list.
839
+
840
+ Parameters:
841
+
842
+ |param|type|description|
843
+ |-----|----|-----------|
844
+ |`list1`|`List<a>`|The list to provide values for the first tuple element|
845
+ |`list2`|`List<b>`|The list to provide values for the second tuple element|
846
+
847
+ Returns:
848
+
849
+ |type|description|
850
+ |----|-----------|
851
+ |`List<(a, b)>`|The new list containing indexed pairs of `(a, b)`|
852
+
853
+ Examples:
854
+
855
+ ```grain
856
+ List.zip([1, 2, 3], [4, 5, 6]) // [(1, 4), (2, 5), (3, 6)]
857
+ ```
858
+
859
+ ```grain
860
+ List.zip([1, 2, 3], [4, 5]) // [(1, 4), (2, 5)]
861
+ ```
862
+
863
+ ### List.**zipWith**
864
+
865
+ <details disabled>
866
+ <summary tabindex="-1">Added in <code>0.5.3</code></summary>
867
+ No other changes yet.
868
+ </details>
869
+
870
+ ```grain
871
+ zipWith : (((a, b) -> c), List<a>, List<b>) -> List<c>
872
+ ```
873
+
874
+ Produces a new list filled with elements defined by applying a function on
875
+ pairs from both given lists. The first element will contain the result of
876
+ applying the function to the first elements of each list, the second element
877
+ will contain the result of applying the function to the second elements of
878
+ each list, and so on.
879
+
880
+ Calling this function with lists of different sizes will cause the returned
881
+ list to have the length of the smaller list.
882
+
883
+ Parameters:
884
+
885
+ |param|type|description|
886
+ |-----|----|-----------|
887
+ |`fn`|`(a, b) -> c`|The function to apply to pairs of elements|
888
+ |`list1`|`List<a>`|The list whose elements will each be passed to the function as the first argument|
889
+ |`list2`|`List<b>`|The list whose elements will each be passed to the function as the second argument|
890
+
891
+ Returns:
892
+
893
+ |type|description|
894
+ |----|-----------|
895
+ |`List<c>`|The new list containing elements derived from applying the function to pairs of input list elements|
896
+
897
+ Examples:
898
+
899
+ ```grain
900
+ List.zipWith((a, b) => a + b, [1, 2, 3], [4, 5, 6]) // [5, 7, 9]
901
+ ```
902
+
903
+ ```grain
904
+ List.zipWith((a, b) => a * b, [1, 2, 3], [4, 5]) // [4, 10]
905
+ ```
906
+
907
+ ### List.**unzip**
908
+
909
+ <details disabled>
910
+ <summary tabindex="-1">Added in <code>0.5.3</code></summary>
911
+ No other changes yet.
912
+ </details>
913
+
914
+ ```grain
915
+ unzip : List<(a, b)> -> (List<a>, List<b>)
916
+ ```
917
+
918
+ Produces two lists by splitting apart a list of tuples.
919
+
920
+ Parameters:
921
+
922
+ |param|type|description|
923
+ |-----|----|-----------|
924
+ |`list`|`List<(a, b)>`|The list of tuples to split|
925
+
926
+ Returns:
927
+
928
+ |type|description|
929
+ |----|-----------|
930
+ |`(List<a>, List<b>)`|An list containing all elements from the first tuple element, and a list containing all elements from the second tuple element|
931
+
822
932
  ### List.**drop**
823
933
 
824
934
  <details disabled>
package/map.gr CHANGED
@@ -504,8 +504,7 @@ export let reject = (predicate, map) => {
504
504
  filter((key, value) => !predicate(key, value), map)
505
505
  }
506
506
 
507
- // TODO: Should return a Record type instead of a Tuple
508
- // Waiting on https://github.com/grain-lang/grain/issues/190
507
+ // TODO(#190): Should return a Record type instead of a Tuple
509
508
  /**
510
509
  * Provides data representing the internal state state of the map.
511
510
  *
package/map.md CHANGED
@@ -263,7 +263,7 @@ Parameters:
263
263
  <tr><th>version</th><th>changes</th></tr>
264
264
  </thead>
265
265
  <tbody>
266
- <tr><td><code>next</code></td><td>Ensured the iterator function return type is always `Void`</td></tr>
266
+ <tr><td><code>0.5.0</code></td><td>Ensured the iterator function return type is always `Void`</td></tr>
267
267
  </tbody>
268
268
  </table>
269
269
  </details>