11// http://erlang.org/doc/man/lists.html
2+ import ErlangTypes from 'erlang-types' ;
23
34function reverse ( list ) {
45 return [ ...list ] . reverse ( ) ;
@@ -10,7 +11,151 @@ function foreach(fun, list) {
1011 return Symbol . for ( 'ok' ) ;
1112}
1213
14+ function duplicate ( n , elem ) {
15+ const list = [ ] ;
16+
17+ while ( list . length < n ) {
18+ list . push ( elem ) ;
19+ }
20+
21+ return list ;
22+ }
23+
24+ function flatten ( deepList , tail = [ ] ) {
25+ const val = deepList . reduce ( ( acc , value ) => {
26+ if ( Array . isArray ( value ) ) {
27+ return acc . concat ( flatten ( value ) ) ;
28+ }
29+
30+ return acc . concat ( value ) ;
31+ } , [ ] ) ;
32+
33+ return val . concat ( tail ) ;
34+ }
35+
36+ function foldl ( fun , acc0 , list ) {
37+ return list . reduce ( ( acc , value ) => {
38+ return fun ( value , acc ) ;
39+ } , acc0 ) ;
40+ }
41+
42+ function foldr ( fun , acc0 , list ) {
43+ return foldl ( fun , acc0 , reverse ( list ) ) ;
44+ }
45+
46+ function keyfind ( key , n , tupleList ) {
47+ for ( const tuple of tupleList ) {
48+ if ( tuple . get ( n ) === key ) {
49+ return tuple ;
50+ }
51+ }
52+
53+ return false ;
54+ }
55+
56+ function keymember ( key , n , tupleList ) {
57+ if ( keyfind ( key , n , tupleList ) === false ) {
58+ return false ;
59+ }
60+
61+ return true ;
62+ }
63+
64+ function keyreplace ( key , n , tupleList , newTuple ) {
65+ const newTupleList = [ ...tupleList ] ;
66+
67+ for ( let index = 0 ; index < newTupleList . length ; index ++ ) {
68+ if ( newTupleList [ index ] . get ( n ) === key ) {
69+ newTupleList [ index ] = newTuple ;
70+ return newTupleList ;
71+ }
72+ }
73+
74+ return newTupleList ;
75+ }
76+
77+ function keysort ( n , tupleList ) {
78+ const newTupleList = [ ...tupleList ] ;
79+
80+ return newTupleList . sort ( ( a , b ) => {
81+ if ( a . get ( n ) < b . get ( n ) ) {
82+ return - 1 ;
83+ } else if ( a . get ( n ) > b . get ( n ) ) {
84+ return 1 ;
85+ }
86+
87+ return 0 ;
88+ } ) ;
89+ }
90+
91+ function keystore ( key , n , tupleList , newTuple ) {
92+ const newTupleList = [ ...tupleList ] ;
93+
94+ for ( let index = 0 ; index < newTupleList . length ; index ++ ) {
95+ if ( newTupleList [ index ] . get ( n ) === key ) {
96+ newTupleList [ index ] = newTuple ;
97+ return newTupleList ;
98+ }
99+ }
100+
101+ return newTupleList . concat ( newTuple ) ;
102+ }
103+
104+ function keydelete ( key , n , tupleList ) {
105+ const newTupleList = [ ] ;
106+ let deleted = false ;
107+
108+ for ( let index = 0 ; index < tupleList . length ; index ++ ) {
109+ if ( deleted === false && tupleList [ index ] . get ( n ) === key ) {
110+ deleted = true ;
111+ } else {
112+ newTupleList . push ( tupleList [ index ] ) ;
113+ }
114+ }
115+
116+ return newTupleList ;
117+ }
118+
119+ function keytake ( key , n , tupleList ) {
120+ const result = keyfind ( key , n , tupleList ) ;
121+
122+ if ( result !== false ) {
123+ return new ErlangTypes . Tuple (
124+ result . get ( n ) ,
125+ result ,
126+ keydelete ( key , n , tupleList )
127+ ) ;
128+ }
129+
130+ return false ;
131+ }
132+
133+ function mapfoldl ( fun , acc0 , list1 ) {
134+ const listResult = [ ] ;
135+ let accResult = acc0 ;
136+
137+ for ( const item of list1 ) {
138+ const tuple = fun ( item , accResult ) ;
139+ listResult . push ( tuple . get ( 0 ) ) ;
140+ accResult = tuple . get ( 1 ) ;
141+ }
142+
143+ return new ErlangTypes . Tuple ( listResult , accResult ) ;
144+ }
145+
13146export default {
14147 reverse,
15148 foreach,
149+ duplicate,
150+ flatten,
151+ foldl,
152+ foldr,
153+ keydelete,
154+ keyfind,
155+ keymember,
156+ keyreplace,
157+ keysort,
158+ keystore,
159+ keytake,
160+ mapfoldl,
16161} ;
0 commit comments