Skip to content

Commit f5e131a

Browse files
committed
adding documentation for List.ark, dropping dropWhile an takeWhile
1 parent 8c0ceec commit f5e131a

File tree

1 file changed

+39
-67
lines changed

1 file changed

+39
-67
lines changed

List.ark

Lines changed: 39 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,14 @@
6868

6969
###
7070
# @meta List
71-
# @brief
72-
# @details
71+
# @brief Drop the first n elements of a list
72+
# @details The original list is left unmodified.
7373
# ---
74+
# (let cool-stuff [1 2 3 4 5 6 7 8 9])
75+
# (print (list:drop cool-stuff 4)) # [5 6 7 8 9]
7476
# ---
7577
# @param _L the list to work on
78+
# @param _n the number of elements to drop
7679
# @author https://github.com/rstefanic
7780
###
7881
(let list:drop (fun (_L _n) {
@@ -87,35 +90,14 @@
8790

8891
###
8992
# @meta List
90-
# @brief
91-
# @details
92-
# ---
93-
# ---
94-
# @param _L the list to work on
95-
# @author https://github.com/rstefanic
96-
###
97-
(let list:dropWhile (fun (_L _f) {
98-
(mut _index 0)
99-
(mut _output [])
100-
(mut _continue true)
101-
102-
(while (and (< _index (len _L)) _continue) {
103-
(if (_f (@ _L _index)) {
104-
(set _index (+ 1 _index))
105-
(set _output (append _output (@ _L _index)))
106-
}
107-
(set _continue false))
108-
})
109-
_output
110-
}))
111-
112-
###
113-
# @meta List
114-
# @brief
115-
# @details
93+
# @brief Keep elements in a given list if they follow a predicate
94+
# @details The original list is left unmodified.
11695
# ---
96+
# (import "Math.ark")
97+
# (print (list:filter [1 2 3 4 5 6 7 8 9] math:even)) # [2 4 6 8]
11798
# ---
11899
# @param _L the list to work on
100+
# @param _f the predicate
119101
# @author https://github.com/rstefanic
120102
###
121103
(let list:filter (fun (_L _f) {
@@ -131,11 +113,13 @@
131113

132114
###
133115
# @meta List
134-
# @brief
135-
# @details
116+
# @brief Applies a given function to each element of a list
117+
# @details The original list is left unmodified.
136118
# ---
119+
# (print (list:map [1 2 3 4 5 6 7 8 9] (fun (e) (* e e)))) # [1 4 9 25 36 49 64 81]
137120
# ---
138121
# @param _L the list to work on
122+
# @param _f the function to apply to each element
139123
# @author https://github.com/rstefanic
140124
###
141125
(let list:map (fun (_L _f) {
@@ -150,11 +134,14 @@
150134

151135
###
152136
# @meta List
153-
# @brief
154-
# @details
137+
# @brief Apply a function to the elements of a list to reduce it
138+
# @details The original list is left unmodified.
155139
# ---
140+
# (let cool [1 2 3 4 5 6 7 8 9])
141+
# (print (list:reduce cool (fun (a b) (+ a b)))) # 45
156142
# ---
157143
# @param _L the list to work on
144+
# @param _f the function to apply
158145
# @author https://github.com/FrenchMasterSword
159146
###
160147
(let list:reduce (fun (_L _f) {
@@ -167,19 +154,25 @@
167154
_output
168155
}))
169156

157+
(import "Math.ark") # needed for (math:min a b)
158+
170159
###
171160
# @meta List
172-
# @brief
173-
# @details
161+
# @brief Take the first n elements of
162+
# @details The original list is left unmodified.
174163
# ---
164+
# (print (list:take [1 2 3 4 5 6 7 8 9] 4)) # [1 2 3 4]
175165
# ---
176166
# @param _L the list to work on
167+
# @param _n the number of elements to take
177168
# @author https://github.com/rstefanic
178169
###
179170
(let list:take (fun (_L _n) {
180171
(mut _index 0)
181172
(mut _output [])
182-
(while (and (< _index _n) (< _index (len _L))) {
173+
(set _n (math:min _n (len _L)))
174+
175+
(while (< _index _n) {
183176
(set _output (append _output (@ _L _index)))
184177
(set _index (+ 1 _index))
185178
})
@@ -188,34 +181,11 @@
188181

189182
###
190183
# @meta List
191-
# @brief
192-
# @details
193-
# ---
194-
# ---
195-
# @param _L the list to work on
196-
# @author https://github.com/rstefanic
197-
###
198-
(let list:takeWhile (fun (_L _f) {
199-
(mut _index 0)
200-
(mut _output [])
201-
(mut _continue true)
202-
203-
(while (and (< _index (len _L)) _continue) {
204-
(if (_f (@ _L _index)) {
205-
(set _output (append _output (@ _L _index)))
206-
(set _index (+ 1 _index))
207-
}
208-
(set _continue false)
209-
)
210-
})
211-
_output
212-
}))
213-
214-
###
215-
# @meta List
216-
# @brief
217-
# @details
184+
# @brief Unzip a list of [[a b] [c d]...] into [[a c ...] [b d ...]]
185+
# @details The original list is left unmodified.
218186
# ---
187+
# (let zipped [[1 5] [2 6] [3 7] [4 8]])
188+
# (print (list:unzip zipped)) # [[1 2 3 4] [5 6 7 8]]
219189
# ---
220190
# @param _L the list to work on
221191
# @author https://github.com/FrenchMasterSword
@@ -234,15 +204,17 @@
234204
[_list1 _list2]
235205
}))
236206

237-
(import "Math.ark") # needed for (math:min a b)
238-
239207
###
240208
# @meta List
241-
# @brief
242-
# @details
209+
# @brief Zip two lists into one: [1 2 3 4] and [5 6 7 8] will give [[1 5] [2 6] [3 7] [4 8]]
210+
# @details The original lists are left unmodified.
243211
# ---
212+
# (let a [1 2 3 4])
213+
# (let b [5 6 7 8])
214+
# (print (list:zip a b)) # [[1 5] [2 6] [3 7] [4 8]]
244215
# ---
245-
# @param _L the list to work on
216+
# @param _a the first list to work on
217+
# @param _b the second list to work on
246218
# @author https://github.com/FrenchMasterSword
247219
###
248220
(let list:zip (fun (_a _b) {

0 commit comments

Comments
 (0)