Skip to content

Commit

Permalink
Add Array & List replaceAt
Browse files Browse the repository at this point in the history
  • Loading branch information
andywhite37 committed Jul 5, 2019
1 parent 6d72646 commit 80b7978
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 3 deletions.
7 changes: 6 additions & 1 deletion __tests__/Relude_Array_test.re
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,11 @@ describe("Array", () => {
|> toEqual([|6, 1, 2, 3, 4, 5|])
);

test("replaceAt", () =>
expect(Array.replaceAt(2, 100, [|1, 2, 3, 4, 5|]))
|> toEqual([|1, 2, 100, 4, 5|])
);

test("map", () =>
expect(Array.map(a => a + 2, [|1, 2, 3|])) |> toEqual([|3, 4, 5|])
);
Expand Down Expand Up @@ -659,4 +664,4 @@ describe("Array", () => {
226,
|])
);
});
});
5 changes: 5 additions & 0 deletions __tests__/Relude_List_test.re
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,11 @@ describe("List", () => {
|> toEqual([2, 4])
);

test("replaceAt", () =>
expect(List.replaceAt(2, 100, [1, 2, 3, 4, 5]))
|> toEqual([1, 2, 100, 4, 5])
);

test("distinctBy", () =>
expect(List.distinctBy(Int.eq, [6, 1, 1, 2, 1, 3, 2, 3, 2, 4, 5, 5]))
|> toEqual([6, 1, 2, 3, 4, 5])
Expand Down
1 change: 1 addition & 0 deletions src/Relude_Array.rei
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ let removeFirst:
let removeEach:
((module BsAbstract.Interface.EQ with type t = 'a), 'a, array('a)) =>
array('a);
let replaceAt: (int, 'a, array('a)) => array('a);
let scanLeft: (('b, 'a) => 'b, 'b, array('a)) => array('b);
let scanRight: (('a, 'b) => 'b, 'b, array('a)) => array('b);
module ArrayEqExtensions = Relude_Extensions_Array.ArrayEqExtensions;
Expand Down
1 change: 1 addition & 0 deletions src/Relude_List.rei
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ let removeFirst:
let removeEach:
((module BsAbstract.Interface.EQ with type t = 'a), 'a, list('a)) =>
list('a);
let replaceAt: (int, 'a, list('a)) => list('a);
let scanLeft: (('b, 'a) => 'b, 'b, list('a)) => list('b);
let scanRight: (('a, 'b) => 'b, 'b, list('a)) => list('b);
module ListEqExtensions = Relude_Extensions_List.ListEqExtensions;
Expand Down
14 changes: 13 additions & 1 deletion src/array/Relude_Array_Base.re
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,18 @@ let removeEach =
removeEachBy(EqA.eq, x, xs);
};

let replaceAt: 'a. (int, 'a, array('a)) => array('a) =
(targetIndex, newX, xs) => {
xs
|> mapWithIndex((x, currentIndex) =>
if (currentIndex == targetIndex) {
newX;
} else {
x;
}
);
};

// TODO: scans should come from a typeclass/extension
let scanLeft: (('b, 'a) => 'b, 'b, array('a)) => array('b) =
(f, init, xs) =>
Expand All @@ -285,4 +297,4 @@ let scanRight: (('a, 'b) => 'b, 'b, array('a)) => array('b) =
(init, [||]),
xs,
),
);
);
14 changes: 13 additions & 1 deletion src/list/Relude_List_Base.re
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,18 @@ let removeEach =
removeEachBy(EqA.eq, x, xs);
};

let replaceAt: 'a. (int, 'a, list('a)) => list('a) =
(targetIndex, newX, xs) => {
xs
|> mapWithIndex((x, currentIndex) =>
if (currentIndex == targetIndex) {
newX;
} else {
x;
}
);
};

// TODO: scans come from TraversableExtensions
let scanLeft: (('b, 'a) => 'b, 'b, list('a)) => list('b) =
(f, init, xs) =>
Expand All @@ -279,4 +291,4 @@ let scanRight: (('a, 'b) => 'b, 'b, list('a)) => list('b) =
(init, []),
xs,
)
|> snd;
|> snd;

0 comments on commit 80b7978

Please sign in to comment.