Skip to content

Commit

Permalink
cherry pick changes to the lists lib
Browse files Browse the repository at this point in the history
  • Loading branch information
aslanix committed Aug 23, 2024
1 parent 1fff4a0 commit a5d361c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
29 changes: 27 additions & 2 deletions lib/lists.trp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
7 changes: 6 additions & 1 deletion lib/out/lists.exports
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ lookup
elem
length
append
partition
partition
nth
filter
first
slice
insert_at_index

0 comments on commit a5d361c

Please sign in to comment.