You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-[Utility functions for wrapped and normal functions](#utility-functions-for-wrapped-and-normal-functions)
@@ -152,6 +153,37 @@ end
152
153
-`lpairs(t:wrapped table)` This functions works just like `ipairs` when called with a list or wrapped string and just like `pairs` when called with anything else.
153
154
-`isList(t:wrapped table or table):boolean` This function returns true if the table is either a list (as a wrapped table) or a normal table that can be turned into a list (i.e. if every key in the table is a number valid for `ipairs`)
154
155
156
+
### Iterables
157
+
Iterables are objects that wrap a function to iterate over: The only parameter they take is a function that takes the current iteration index (or no parameter) and returns a value each time it is called, and nil when there is no new value left to return. Most of the [methods that can be called on wrapped tables](#wrapped-tables-1) can also be called on iterables.
158
+
159
+
```lua
160
+
localfunctionsupply(index)
161
+
ifindex<=10then
162
+
returnindex*index
163
+
end
164
+
end
165
+
166
+
localitr= $(supply) -- Initializing an iterable for an existing function.
167
+
168
+
fori, jinpairs(itr) do-- This calls the function until it returns nil, starting with an iteration index of 1, and provides the iteration index and value.
169
+
print(i, j) -- In this case, the first ten squares of natural numbers are printed.
170
+
end
171
+
172
+
localitr2= $(i->i<=10andi*iornil) -- Iterables can also be initialized using lambda functions.
173
+
174
+
forvalinitr2do-- This iterates through the iterable, only providing the value.
175
+
print(val)
176
+
end
177
+
178
+
localitr3= $( ->switch(math.random(0, 9),
179
+
(val! val>=3->val)
180
+
)) -- Of course, the supplier function does not have to use the index parameter.
181
+
182
+
fori, j<-itr3do-- Selene's for loop syntax works as well.
183
+
print(val)
184
+
end
185
+
```
186
+
155
187
### Lambdas
156
188
Lambdas are wrapped in `()` brackets and always look like `(<var1> [, var2, ...] -> <operation>)`. Alternatively to the `->` you can also use `=>`.
157
189
```lua
@@ -265,7 +297,7 @@ These functions will not work directly called on a string, i.e. `string.drop("He
265
297
-`string.iter(s:string)` This functions returns an iterator over the string `s`, so you can iterate through the characters of the string using `for index, char in string.iter(s) do ... end`.
266
298
267
299
### Wrapped tables
268
-
These are the functions you can call on wrapped tables. `$()` represents a wrapped list or map, `$l()` represents a list.
300
+
These are the functions you can call on wrapped tables. `$()` represents a wrapped list or map, `$l()` represents a list, `$i()` represents an iterable.
269
301
-`$():concat(sep:string, i:number, j:number):string` This works exactly like `table.concat`.
270
302
-`$():foreach(f:function)` This works exactly like `string.foreach`, just that it will iterate through each key/value pair in the table.
271
303
-`$():map(f:function):list or map` This works exactly like `string.map`, just that it will iterate through each key/value pair in the table.
@@ -302,6 +334,7 @@ These are the functions you can call on wrapped tables. `$()` represents a wrapp
302
334
-`$l():reverse():list` This function will invert the list so that the last entry will be the first one etc.
303
335
-`$l():flatten():list` This works exactly like `table.flatten`.
304
336
-`$l():zip(other:list or table or function):list` This will merge the other table (which has to be an ipairs-valid list) or list into itself if both lists have the same length, in the pattern `{{t1[1], t2[1]}, {t1[2], t2[2]}, ...}`. If `other` is a function or wrapped function, it will call it once per iteration and merge the returned value in the described pattern.
337
+
-`$i():collect():list` This function will iterate through the iterable and add every returned element to a list which it will return.
305
338
306
339
### Wrapped strings
307
340
Wrapped strings or stringslists can mostly be seen as lists and have most of the functions wrapped tables have (including `drop`, `dropwhile` and `reverse`).
0 commit comments