Commit 28ee20b0f4aa89162ac921d9af79c90adc9bc065
1 parent
3e3f3d2c
maj tools
Showing
3 changed files
with
89 additions
and
2 deletions
Show diff stats
anubis_dev/library/tools/function.anubis
| ... | ... | @@ -298,6 +298,29 @@ public define Maybe(List($B)) |
| 298 | 298 | }. |
| 299 | 299 | |
| 300 | 300 | |
| 301 | + | |
| 302 | +public define Result($E, List($B)) map_escape( | |
| 303 | + $A -> Result($E, $B) f, | |
| 304 | + List($A) l, | |
| 305 | + ) = | |
| 306 | + if l is | |
| 307 | + { | |
| 308 | + [ ] then ok([ ]), | |
| 309 | + [h . t] then | |
| 310 | + if f(h) is | |
| 311 | + { | |
| 312 | + error(e) then error(e), | |
| 313 | + ok(r) then | |
| 314 | + if map_escape(f, t) is | |
| 315 | + { | |
| 316 | + error(e) then error(e) | |
| 317 | + ok(others) then ok([r . others]) | |
| 318 | + } | |
| 319 | + } | |
| 320 | + }. | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 301 | 324 | *** (4.8.9) Map in parallel and wait or don't wait for completion. |
| 302 | 325 | |
| 303 | 326 | Apply in parallel the function f to all elements of a list | ... | ... |
anubis_dev/library/tools/iterators.anubis
| 1 | 1 | Author : Matthieu Herrmann |
| 2 | 2 | Creation : 2014/02/10 |
| 3 | - Last update : 2015/03/31 18:24 by matthieu@Embryo (inserted by vim). | |
| 3 | + Last update : 2015/05/13 12:32 by matthieu@Embryo (inserted by vim). | |
| 4 | 4 | ====================================================================================================================== |
| 5 | 5 | |
| 6 | 6 | ====================================================================================================================== |
| ... | ... | @@ -79,7 +79,7 @@ public define $A force_nth(Int n, Iterator($A) it, $A default). |
| 79 | 79 | Get an iterator composed of the first n item of current iterator it. |
| 80 | 80 | public define Iterator($A) first(Int n, Iterator($A) it). |
| 81 | 81 | |
| 82 | - *** (3) Item filtering/mapping/iterating function | |
| 82 | + *** (3) Item filtering/mapping/iterating/searching function | |
| 83 | 83 | ================================================================================================================== |
| 84 | 84 | |
| 85 | 85 | Lazily filtering/transforming element from the current iterator it. |
| ... | ... | @@ -97,6 +97,11 @@ public define Iterator($B) map( ($A, $P) -> $B fun, $P parameter, Iterator($A) i |
| 97 | 97 | Apply **immediately** 'fun' to each element from the iterator. |
| 98 | 98 | public define One map_forget( $A -> $B fun, Iterator($A) it). |
| 99 | 99 | |
| 100 | + Search the first item | |
| 101 | +public define Maybe($B) find_first($A -> Maybe($B) test, Iterator($A) it). | |
| 102 | + | |
| 103 | +public define Maybe($A) find_first($A -> Bool test, Iterator($A) it). | |
| 104 | + | |
| 100 | 105 | *** (4) Reducing/folding/unfolding functions |
| 101 | 106 | ================================================================================================================== |
| 102 | 107 | |
| ... | ... | @@ -178,6 +183,15 @@ public define Iterator($A) append(Iterator($A) it1, Iterator($A) it2). |
| 178 | 183 | input iterator. |
| 179 | 184 | public define Iterator(($A,$B)) zip(Iterator($A) it1, Iterator($B) it2). |
| 180 | 185 | |
| 186 | + | |
| 187 | + Separate into two classes, converting while testing the first class, converting the second class with convert. | |
| 188 | + Warning: not lazy! The separation is immediately effective. | |
| 189 | +public define (Iterator($R1), Iterator($R2)) separate(Iterator($A) it, $A -> Maybe($R1) filter, $A -> $R2 convert). | |
| 190 | + | |
| 191 | + Same as above, without conversion of the second class. | |
| 192 | + Warning: not lazy! The separation is immediately effective. | |
| 193 | +public define (Iterator($R1), Iterator($A)) separate(Iterator($A) it, $A -> Maybe($R1) filter). | |
| 194 | + | |
| 181 | 195 | *** (6) Iterator printing |
| 182 | 196 | ================================================================================================================== |
| 183 | 197 | |
| ... | ... | @@ -349,6 +363,18 @@ public define One map_forget( $A -> $B fun, Iterator($A) it) = if it is |
| 349 | 363 | head .. tail then forget(fun(head)); map_forget(fun, tail(unique)) |
| 350 | 364 | }. |
| 351 | 365 | |
| 366 | +public define Maybe($B) find_first($A -> Maybe($B) test, Iterator($A) it) = | |
| 367 | + if it is | |
| 368 | + { | |
| 369 | + nil then failure | |
| 370 | + head .. tail then if test(head) is success(r) then success(r) else find_first(test, tail(unique)) | |
| 371 | + }. | |
| 372 | + | |
| 373 | +public define Maybe($A) find_first($A -> Bool test, Iterator($A) it) = | |
| 374 | + find_first( ($A item)|-> if test(item) then success(item) else failure, it). | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 352 | 378 | *** [4] Reducing/folding/unfolding functions |
| 353 | 379 | ================================================================================================================== |
| 354 | 380 | |
| ... | ... | @@ -475,6 +501,30 @@ public define Iterator(($A,$B)) zip(Iterator($A) it1, Iterator($B) it2) = |
| 475 | 501 | } |
| 476 | 502 | }. |
| 477 | 503 | |
| 504 | + | |
| 505 | +define (Iterator($R1), Iterator($R2)) separate(Iterator($A) it, $A -> Maybe($R1) f, $A -> $R2 c, (Iterator($R1), Iterator($R2)) acc) = | |
| 506 | + since acc is (left, right), | |
| 507 | + if it is | |
| 508 | + { | |
| 509 | + nil then acc, | |
| 510 | + h .. t then if f(h) is | |
| 511 | + { | |
| 512 | + failure then separate( t(unique), f, c, (left, c(h) .. (One u)|->right) ) | |
| 513 | + success(s) then separate( t(unique), f, c, (s .. (One u)|->left, right)) | |
| 514 | + } | |
| 515 | + }. | |
| 516 | + | |
| 517 | + Separate into two classes, converting while testing the first class, converting the second class with convert. | |
| 518 | +public define (Iterator($R1), Iterator($R2)) separate(Iterator($A) it, $A -> Maybe($R1) filter, $A -> $R2 convert) = | |
| 519 | + separate(it, filter, convert, (nil, nil)). | |
| 520 | + | |
| 521 | +public define (Iterator($R1), Iterator($A)) separate(Iterator($A) it, $A -> Maybe($R1) filter) = | |
| 522 | + separate(it, filter, ($A a)|->a). | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 478 | 528 | *** [6] Iterator length |
| 479 | 529 | ================================================================================================================== |
| 480 | 530 | ... | ... |
anubis_dev/library/tools/list.anubis
| ... | ... | @@ -731,6 +731,20 @@ public define One foreach(List($T) l, $T -> One fun) = |
| 731 | 731 | [h . t] then fun(h); foreach(t, fun) |
| 732 | 732 | }. |
| 733 | 733 | |
| 734 | +define (List($R1), List($R2)) separate(List($A) l, $A -> Maybe($R1) test, $A -> $R2 conv, List($R1) acc1, List($R2) acc2) = | |
| 735 | + if l is | |
| 736 | + { | |
| 737 | + [ ] then (acc1, acc2), | |
| 738 | + [h . t] then if test(h) is | |
| 739 | + { | |
| 740 | + failure then separate(t, test, conv, acc1, [conv(h) . acc2]) | |
| 741 | + success(s) then separate(t, test, conv, [s . acc1], acc2) | |
| 742 | + } | |
| 743 | + }. | |
| 744 | + | |
| 745 | +public define (List($R1), List($R2)) separate(List($A) l, $A -> Maybe($R1) test, $A -> $R2 conv) = | |
| 746 | + separate(l, test, conv, [], []). | |
| 747 | + | |
| 734 | 748 | |
| 735 | 749 | *** (4.16.3) Folding/Unfolding |
| 736 | 750 | ============================================================================================================== | ... | ... |