From a5d361c33fb4650ccfbbb71e6027664370338735 Mon Sep 17 00:00:00 2001 From: Aslan Askarov Date: Fri, 23 Aug 2024 10:33:38 +0200 Subject: [PATCH] cherry pick changes to the lists lib --- lib/lists.trp | 29 +++++++++++++++++++++++++++-- lib/out/lists.exports | 7 ++++++- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/lib/lists.trp b/lib/lists.trp index 482f875..b402da8 100644 --- a/lib/lists.trp +++ b/lib/lists.trp @@ -9,8 +9,6 @@ let fun map f list = | (x::xs) => (f (j,x)) :: (mapj (j+1) xs) in mapj 0 list end - - fun foldl f y [] = y | foldl f y (x::xs) = foldl f (f (x,y)) xs @@ -58,6 +56,28 @@ let fun map f list = in partition_aux ([],[]) ls end + fun filter f l = case l of + [] => [] + | h :: t => if f h then h :: (filter f t) else filter f t + + fun first l = case l of + [] => [] + | h :: _ => h + + fun slice beg s_end l = case (beg, s_end, l) of + (_, _, []) => [] + | (0, 0, _) => [] + | (0, n, h :: t) => h :: (slice beg (n - 1) t) + | (m, n, h :: t) => slice (m - 1) (n - 1) t + | (_, _, _) => [] + + fun insert_at_index l appended_l index = + let val beg_slice = slice 0 index l + val end_slice = slice index (length l) l + in append beg_slice (append appended_l end_slice) + end + + in [ ("map", map) , ("mapi", mapi) @@ -69,5 +89,10 @@ in , ("length", length) , ("append", append) , ("partition", partition) + , ("nth", nth) + , ("filter", filter) + , ("first", first) + , ("slice", slice) + , ("insert_at_index", insert_at_index) ] end diff --git a/lib/out/lists.exports b/lib/out/lists.exports index d0a5830..db2a6ba 100644 --- a/lib/out/lists.exports +++ b/lib/out/lists.exports @@ -7,4 +7,9 @@ lookup elem length append -partition \ No newline at end of file +partition +nth +filter +first +slice +insert_at_index \ No newline at end of file