Skip to content

Commit 79f5c75

Browse files
committed
Add the mapi function
I'm unsure if we can add this to a trait or not
1 parent ecfb567 commit 79f5c75

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

Stdlib/Data/List/Base.juvix

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ listMap {A B} (fun : A -> B) : (list : List A) -> List B
5757
| nil := nil
5858
| (h :: hs) := fun h :: listMap fun hs;
5959

60+
--- 𝒪(𝓃). Maps a function over each element of a ;List; with an index.
61+
{-# specialize: [1] #-}
62+
mapi {A B} : (fun : Nat -> A -> B) -> List A -> List B
63+
| fun xs :=
64+
let
65+
go : Nat -> List A -> List B
66+
| _ nil := nil
67+
| n (x :: xs) := fun n x :: go (suc n) xs;
68+
in go zero xs;
69+
6070
syntax iterator filter {init := 0; range := 1};
6171

6272
--- 𝒪(𝓃). Filters a ;List; according to a given predicate, i.e.,

0 commit comments

Comments
 (0)