1
1
import gleam/list
2
-
3
- if erlang {
4
- import gleam/option . { None , Option , Some }
5
- import gleam/map . { Map }
6
- }
2
+ import gleam/option . { None , Option , Some }
3
+ import gleam/map . { Map }
7
4
8
5
// Internal private representation of an Iterator
9
6
type Action ( element) {
@@ -119,11 +116,11 @@ pub fn from_list(list: List(element)) -> Iterator(element) {
119
116
// Consuming Iterators
120
117
fn do_fold (
121
118
continuation : fn ( ) -> Action ( e) ,
122
- f : fn ( e , acc ) -> acc,
119
+ f : fn ( acc , e ) -> acc,
123
120
accumulator : acc,
124
121
) -> acc {
125
122
case continuation ( ) {
126
- Continue ( elem , next ) -> do_fold ( next , f , f ( elem , accumulator ) )
123
+ Continue ( elem , next ) -> do_fold ( next , f , f ( accumulator , elem ) )
127
124
Stop -> accumulator
128
125
}
129
126
}
@@ -147,7 +144,7 @@ fn do_fold(
147
144
pub fn fold (
148
145
over iterator : Iterator ( e) ,
149
146
from initial : acc,
150
- with f : fn ( e , acc ) -> acc,
147
+ with f : fn ( acc , e ) -> acc,
151
148
) -> acc {
152
149
iterator . continuation
153
150
|> do_fold ( f , initial )
@@ -174,7 +171,7 @@ pub fn run(iterator: Iterator(e)) -> Nil {
174
171
///
175
172
pub fn to_list ( iterator : Iterator ( element) ) -> List ( element) {
176
173
iterator
177
- |> fold ( [ ] , fn ( e , acc ) { [ e , .. acc ] } )
174
+ |> fold ( [ ] , fn ( acc , e ) { [ e , .. acc ] } )
178
175
|> list . reverse
179
176
}
180
177
@@ -583,14 +580,14 @@ pub fn drop_while(
583
580
584
581
fn do_scan (
585
582
continuation : fn ( ) -> Action ( element) ,
586
- f : fn ( element , acc ) -> acc,
583
+ f : fn ( acc , element ) -> acc,
587
584
accumulator : acc,
588
585
) -> fn ( ) -> Action ( acc) {
589
586
fn ( ) {
590
587
case continuation ( ) {
591
588
Stop -> Stop
592
589
Continue ( el , next ) -> {
593
- let accumulated = f ( el , accumulator )
590
+ let accumulated = f ( accumulator , el )
594
591
Continue ( accumulated , do_scan ( next , f , accumulated ) )
595
592
}
596
593
}
@@ -610,7 +607,7 @@ fn do_scan(
610
607
pub fn scan (
611
608
over iterator : Iterator ( element) ,
612
609
from initial : acc,
613
- with f : fn ( element , acc ) -> acc,
610
+ with f : fn ( acc , element ) -> acc,
614
611
) -> Iterator ( acc) {
615
612
iterator . continuation
616
613
|> do_scan ( f , initial )
@@ -883,45 +880,41 @@ pub fn all(
883
880
|> do_all ( predicate )
884
881
}
885
882
886
- if erlang {
887
- fn update_group_with (
888
- el : element,
889
- ) -> fn ( Option ( List ( element) ) ) -> List ( element) {
890
- fn ( maybe_group ) {
891
- case maybe_group {
892
- Some ( group ) -> [ el , .. group ]
893
- None -> [ el ]
894
- }
883
+ fn update_group_with ( el : element) -> fn ( Option ( List ( element) ) ) -> List ( element) {
884
+ fn ( maybe_group ) {
885
+ case maybe_group {
886
+ Some ( group ) -> [ el , .. group ]
887
+ None -> [ el ]
895
888
}
896
889
}
890
+ }
897
891
898
- fn group_updater (
899
- f : fn ( element) -> key,
900
- ) -> fn ( element, Map ( key, List ( element) ) ) -> Map ( key, List ( element) ) {
901
- fn ( elem , groups ) {
902
- groups
903
- |> map . update ( f ( elem ) , update_group_with ( elem ) )
904
- }
892
+ fn group_updater (
893
+ f : fn ( element) -> key,
894
+ ) -> fn ( Map ( key, List ( element) ) , element) -> Map ( key, List ( element) ) {
895
+ fn ( groups , elem ) {
896
+ groups
897
+ |> map . update ( f ( elem ) , update_group_with ( elem ) )
905
898
}
899
+ }
906
900
907
- /// Returns a `Map(k, List(element))` of elements from the given iterator
908
- /// grouped with the given key function.
909
- ///
910
- /// The order within each group is preserved from the iterator.
911
- ///
912
- /// ## Examples
913
- ///
914
- /// > from_list([1, 2, 3, 4, 5, 6]) |> group(by: fn(n) { n % 3 })
915
- /// map.from_list([#(0, [3, 6]), #(1, [1, 4]), #(2, [2, 5])])
916
- ///
917
- pub fn group (
918
- in iterator : Iterator ( element) ,
919
- by key : fn ( element) -> key,
920
- ) -> Map ( key, List ( element) ) {
921
- iterator
922
- |> fold ( map . new ( ) , group_updater ( key ) )
923
- |> map . map_values ( fn ( _ , group ) { list . reverse ( group ) } )
924
- }
901
+ /// Returns a `Map(k, List(element))` of elements from the given iterator
902
+ /// grouped with the given key function.
903
+ ///
904
+ /// The order within each group is preserved from the iterator.
905
+ ///
906
+ /// ## Examples
907
+ ///
908
+ /// > from_list([1, 2, 3, 4, 5, 6]) |> group(by: fn(n) { n % 3 })
909
+ /// map.from_list([#(0, [3, 6]), #(1, [1, 4]), #(2, [2, 5])])
910
+ ///
911
+ pub fn group (
912
+ in iterator : Iterator ( element) ,
913
+ by key : fn ( element) -> key,
914
+ ) -> Map ( key, List ( element) ) {
915
+ iterator
916
+ |> fold ( map . new ( ) , group_updater ( key ) )
917
+ |> map . map_values ( fn ( _ , group ) { list . reverse ( group ) } )
925
918
}
926
919
927
920
/// This function acts similar to fold, but does not take an initial state.
@@ -967,7 +960,7 @@ pub fn reduce(
967
960
///
968
961
pub fn last ( iterator : Iterator ( element) ) -> Result ( element, Nil ) {
969
962
iterator
970
- |> reduce ( fn ( elem , _ ) { elem } )
963
+ |> reduce ( fn ( _ , elem ) { elem } )
971
964
}
972
965
973
966
/// Creates an iterator that yields no elements.
@@ -1036,13 +1029,13 @@ pub fn interleave(
1036
1029
1037
1030
fn do_fold_until (
1038
1031
continuation : fn ( ) -> Action ( e) ,
1039
- f : fn ( e , acc ) -> list . ContinueOrStop ( acc) ,
1032
+ f : fn ( acc , e ) -> list . ContinueOrStop ( acc) ,
1040
1033
accumulator : acc,
1041
1034
) -> acc {
1042
1035
case continuation ( ) {
1043
1036
Stop -> accumulator
1044
1037
Continue ( elem , next ) ->
1045
- case f ( elem , accumulator ) {
1038
+ case f ( accumulator , elem ) {
1046
1039
list . Continue ( accumulator ) -> do_fold_until ( next , f , accumulator )
1047
1040
list . Stop ( accumulator ) -> accumulator
1048
1041
}
@@ -1073,21 +1066,21 @@ fn do_fold_until(
1073
1066
pub fn fold_until (
1074
1067
over iterator : Iterator ( e) ,
1075
1068
from initial : acc,
1076
- with f : fn ( e , acc ) -> list . ContinueOrStop ( acc) ,
1069
+ with f : fn ( acc , e ) -> list . ContinueOrStop ( acc) ,
1077
1070
) -> acc {
1078
1071
iterator . continuation
1079
1072
|> do_fold_until ( f , initial )
1080
1073
}
1081
1074
1082
1075
fn do_try_fold (
1083
1076
over continuation : fn ( ) -> Action ( a) ,
1084
- with f : fn ( a , acc ) -> Result ( acc, err) ,
1077
+ with f : fn ( acc , a ) -> Result ( acc, err) ,
1085
1078
from accumulator : acc,
1086
1079
) -> Result ( acc, err) {
1087
1080
case continuation ( ) {
1088
1081
Stop -> Ok ( accumulator )
1089
1082
Continue ( elem , next ) -> {
1090
- try accumulator = f ( elem , accumulator )
1083
+ try accumulator = f ( accumulator , elem )
1091
1084
do_try_fold ( next , f , accumulator )
1092
1085
}
1093
1086
}
@@ -1115,7 +1108,7 @@ fn do_try_fold(
1115
1108
pub fn try_fold (
1116
1109
over iterator : Iterator ( e) ,
1117
1110
from initial : acc,
1118
- with f : fn ( e , acc ) -> Result ( acc, err) ,
1111
+ with f : fn ( acc , e ) -> Result ( acc, err) ,
1119
1112
) -> Result ( acc, err) {
1120
1113
iterator . continuation
1121
1114
|> do_try_fold ( f , initial )
0 commit comments