11import 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 }
74
85// Internal private representation of an Iterator
96type Action ( element) {
@@ -119,11 +116,11 @@ pub fn from_list(list: List(element)) -> Iterator(element) {
119116// Consuming Iterators
120117fn do_fold (
121118 continuation : fn ( ) -> Action ( e) ,
122- f : fn ( e , acc ) -> acc,
119+ f : fn ( acc , e ) -> acc,
123120 accumulator : acc,
124121) -> acc {
125122 case continuation ( ) {
126- Continue ( elem , next ) -> do_fold ( next , f , f ( elem , accumulator ) )
123+ Continue ( elem , next ) -> do_fold ( next , f , f ( accumulator , elem ) )
127124 Stop -> accumulator
128125 }
129126}
@@ -147,7 +144,7 @@ fn do_fold(
147144pub fn fold (
148145 over iterator : Iterator ( e) ,
149146 from initial : acc,
150- with f : fn ( e , acc ) -> acc,
147+ with f : fn ( acc , e ) -> acc,
151148) -> acc {
152149 iterator . continuation
153150 |> do_fold ( f , initial )
@@ -174,7 +171,7 @@ pub fn run(iterator: Iterator(e)) -> Nil {
174171///
175172pub fn to_list ( iterator : Iterator ( element) ) -> List ( element) {
176173 iterator
177- |> fold ( [ ] , fn ( e , acc ) { [ e , .. acc ] } )
174+ |> fold ( [ ] , fn ( acc , e ) { [ e , .. acc ] } )
178175 |> list . reverse
179176}
180177
@@ -583,14 +580,14 @@ pub fn drop_while(
583580
584581fn do_scan (
585582 continuation : fn ( ) -> Action ( element) ,
586- f : fn ( element , acc ) -> acc,
583+ f : fn ( acc , element ) -> acc,
587584 accumulator : acc,
588585) -> fn ( ) -> Action ( acc) {
589586 fn ( ) {
590587 case continuation ( ) {
591588 Stop -> Stop
592589 Continue ( el , next ) -> {
593- let accumulated = f ( el , accumulator )
590+ let accumulated = f ( accumulator , el )
594591 Continue ( accumulated , do_scan ( next , f , accumulated ) )
595592 }
596593 }
@@ -610,7 +607,7 @@ fn do_scan(
610607pub fn scan (
611608 over iterator : Iterator ( element) ,
612609 from initial : acc,
613- with f : fn ( element , acc ) -> acc,
610+ with f : fn ( acc , element ) -> acc,
614611) -> Iterator ( acc) {
615612 iterator . continuation
616613 |> do_scan ( f , initial )
@@ -883,45 +880,41 @@ pub fn all(
883880 |> do_all ( predicate )
884881}
885882
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 ]
895888 }
896889 }
890+ }
897891
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 ) )
905898 }
899+ }
906900
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 ) } )
925918}
926919
927920/// This function acts similar to fold, but does not take an initial state.
@@ -967,7 +960,7 @@ pub fn reduce(
967960///
968961pub fn last ( iterator : Iterator ( element) ) -> Result ( element, Nil ) {
969962 iterator
970- |> reduce ( fn ( elem , _ ) { elem } )
963+ |> reduce ( fn ( _ , elem ) { elem } )
971964}
972965
973966/// Creates an iterator that yields no elements.
@@ -1036,13 +1029,13 @@ pub fn interleave(
10361029
10371030fn do_fold_until (
10381031 continuation : fn ( ) -> Action ( e) ,
1039- f : fn ( e , acc ) -> list . ContinueOrStop ( acc) ,
1032+ f : fn ( acc , e ) -> list . ContinueOrStop ( acc) ,
10401033 accumulator : acc,
10411034) -> acc {
10421035 case continuation ( ) {
10431036 Stop -> accumulator
10441037 Continue ( elem , next ) ->
1045- case f ( elem , accumulator ) {
1038+ case f ( accumulator , elem ) {
10461039 list . Continue ( accumulator ) -> do_fold_until ( next , f , accumulator )
10471040 list . Stop ( accumulator ) -> accumulator
10481041 }
@@ -1073,21 +1066,21 @@ fn do_fold_until(
10731066pub fn fold_until (
10741067 over iterator : Iterator ( e) ,
10751068 from initial : acc,
1076- with f : fn ( e , acc ) -> list . ContinueOrStop ( acc) ,
1069+ with f : fn ( acc , e ) -> list . ContinueOrStop ( acc) ,
10771070) -> acc {
10781071 iterator . continuation
10791072 |> do_fold_until ( f , initial )
10801073}
10811074
10821075fn do_try_fold (
10831076 over continuation : fn ( ) -> Action ( a) ,
1084- with f : fn ( a , acc ) -> Result ( acc, err) ,
1077+ with f : fn ( acc , a ) -> Result ( acc, err) ,
10851078 from accumulator : acc,
10861079) -> Result ( acc, err) {
10871080 case continuation ( ) {
10881081 Stop -> Ok ( accumulator )
10891082 Continue ( elem , next ) -> {
1090- try accumulator = f ( elem , accumulator )
1083+ try accumulator = f ( accumulator , elem )
10911084 do_try_fold ( next , f , accumulator )
10921085 }
10931086 }
@@ -1115,7 +1108,7 @@ fn do_try_fold(
11151108pub fn try_fold (
11161109 over iterator : Iterator ( e) ,
11171110 from initial : acc,
1118- with f : fn ( e , acc ) -> Result ( acc, err) ,
1111+ with f : fn ( acc , e ) -> Result ( acc, err) ,
11191112) -> Result ( acc, err) {
11201113 iterator . continuation
11211114 |> do_try_fold ( f , initial )
0 commit comments