From 28ee20b0f4aa89162ac921d9af79c90adc9bc065 Mon Sep 17 00:00:00 2001 From: HerrmannM Date: Wed, 27 May 2015 15:21:45 +0200 Subject: [PATCH] maj tools --- anubis_dev/library/tools/function.anubis | 23 +++++++++++++++++++++++ anubis_dev/library/tools/iterators.anubis | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- anubis_dev/library/tools/list.anubis | 14 ++++++++++++++ 3 files changed, 89 insertions(+), 2 deletions(-) diff --git a/anubis_dev/library/tools/function.anubis b/anubis_dev/library/tools/function.anubis index 0f7bea5..a126197 100644 --- a/anubis_dev/library/tools/function.anubis +++ b/anubis_dev/library/tools/function.anubis @@ -298,6 +298,29 @@ public define Maybe(List($B)) }. + +public define Result($E, List($B)) map_escape( + $A -> Result($E, $B) f, + List($A) l, + ) = + if l is + { + [ ] then ok([ ]), + [h . t] then + if f(h) is + { + error(e) then error(e), + ok(r) then + if map_escape(f, t) is + { + error(e) then error(e) + ok(others) then ok([r . others]) + } + } + }. + + + *** (4.8.9) Map in parallel and wait or don't wait for completion. Apply in parallel the function f to all elements of a list diff --git a/anubis_dev/library/tools/iterators.anubis b/anubis_dev/library/tools/iterators.anubis index 07fdbca..f86f6da 100644 --- a/anubis_dev/library/tools/iterators.anubis +++ b/anubis_dev/library/tools/iterators.anubis @@ -1,6 +1,6 @@ Author : Matthieu Herrmann Creation : 2014/02/10 - Last update : 2015/03/31 18:24 by matthieu@Embryo (inserted by vim). + Last update : 2015/05/13 12:32 by matthieu@Embryo (inserted by vim). ====================================================================================================================== ====================================================================================================================== @@ -79,7 +79,7 @@ public define $A force_nth(Int n, Iterator($A) it, $A default). Get an iterator composed of the first n item of current iterator it. public define Iterator($A) first(Int n, Iterator($A) it). - *** (3) Item filtering/mapping/iterating function + *** (3) Item filtering/mapping/iterating/searching function ================================================================================================================== 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 Apply **immediately** 'fun' to each element from the iterator. public define One map_forget( $A -> $B fun, Iterator($A) it). + Search the first item +public define Maybe($B) find_first($A -> Maybe($B) test, Iterator($A) it). + +public define Maybe($A) find_first($A -> Bool test, Iterator($A) it). + *** (4) Reducing/folding/unfolding functions ================================================================================================================== @@ -178,6 +183,15 @@ public define Iterator($A) append(Iterator($A) it1, Iterator($A) it2). input iterator. public define Iterator(($A,$B)) zip(Iterator($A) it1, Iterator($B) it2). + + Separate into two classes, converting while testing the first class, converting the second class with convert. + Warning: not lazy! The separation is immediately effective. +public define (Iterator($R1), Iterator($R2)) separate(Iterator($A) it, $A -> Maybe($R1) filter, $A -> $R2 convert). + + Same as above, without conversion of the second class. + Warning: not lazy! The separation is immediately effective. +public define (Iterator($R1), Iterator($A)) separate(Iterator($A) it, $A -> Maybe($R1) filter). + *** (6) Iterator printing ================================================================================================================== @@ -349,6 +363,18 @@ public define One map_forget( $A -> $B fun, Iterator($A) it) = if it is head .. tail then forget(fun(head)); map_forget(fun, tail(unique)) }. +public define Maybe($B) find_first($A -> Maybe($B) test, Iterator($A) it) = + if it is + { + nil then failure + head .. tail then if test(head) is success(r) then success(r) else find_first(test, tail(unique)) + }. + +public define Maybe($A) find_first($A -> Bool test, Iterator($A) it) = + find_first( ($A item)|-> if test(item) then success(item) else failure, it). + + + *** [4] Reducing/folding/unfolding functions ================================================================================================================== @@ -475,6 +501,30 @@ public define Iterator(($A,$B)) zip(Iterator($A) it1, Iterator($B) it2) = } }. + +define (Iterator($R1), Iterator($R2)) separate(Iterator($A) it, $A -> Maybe($R1) f, $A -> $R2 c, (Iterator($R1), Iterator($R2)) acc) = + since acc is (left, right), + if it is + { + nil then acc, + h .. t then if f(h) is + { + failure then separate( t(unique), f, c, (left, c(h) .. (One u)|->right) ) + success(s) then separate( t(unique), f, c, (s .. (One u)|->left, right)) + } + }. + + Separate into two classes, converting while testing the first class, converting the second class with convert. +public define (Iterator($R1), Iterator($R2)) separate(Iterator($A) it, $A -> Maybe($R1) filter, $A -> $R2 convert) = + separate(it, filter, convert, (nil, nil)). + +public define (Iterator($R1), Iterator($A)) separate(Iterator($A) it, $A -> Maybe($R1) filter) = + separate(it, filter, ($A a)|->a). + + + + + *** [6] Iterator length ================================================================================================================== diff --git a/anubis_dev/library/tools/list.anubis b/anubis_dev/library/tools/list.anubis index 10d4cd7..731dec6 100644 --- a/anubis_dev/library/tools/list.anubis +++ b/anubis_dev/library/tools/list.anubis @@ -731,6 +731,20 @@ public define One foreach(List($T) l, $T -> One fun) = [h . t] then fun(h); foreach(t, fun) }. +define (List($R1), List($R2)) separate(List($A) l, $A -> Maybe($R1) test, $A -> $R2 conv, List($R1) acc1, List($R2) acc2) = + if l is + { + [ ] then (acc1, acc2), + [h . t] then if test(h) is + { + failure then separate(t, test, conv, acc1, [conv(h) . acc2]) + success(s) then separate(t, test, conv, [s . acc1], acc2) + } + }. + +public define (List($R1), List($R2)) separate(List($A) l, $A -> Maybe($R1) test, $A -> $R2 conv) = + separate(l, test, conv, [], []). + *** (4.16.3) Folding/Unfolding ============================================================================================================== -- libgit2 0.21.4