diff --git a/__tests__/Relude_Array_test.re b/__tests__/Relude_Array_test.re index 4404c2ea..39020676 100644 --- a/__tests__/Relude_Array_test.re +++ b/__tests__/Relude_Array_test.re @@ -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|]) ); @@ -659,4 +664,4 @@ describe("Array", () => { 226, |]) ); -}); +}); \ No newline at end of file diff --git a/__tests__/Relude_List_test.re b/__tests__/Relude_List_test.re index 56b8d5c5..686394d7 100644 --- a/__tests__/Relude_List_test.re +++ b/__tests__/Relude_List_test.re @@ -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]) diff --git a/src/Relude_Array.rei b/src/Relude_Array.rei index c7f73efd..c3f3e4ee 100644 --- a/src/Relude_Array.rei +++ b/src/Relude_Array.rei @@ -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; diff --git a/src/Relude_List.rei b/src/Relude_List.rei index 99a1026d..bda60341 100644 --- a/src/Relude_List.rei +++ b/src/Relude_List.rei @@ -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; diff --git a/src/array/Relude_Array_Base.re b/src/array/Relude_Array_Base.re index 86560998..d28c86ab 100644 --- a/src/array/Relude_Array_Base.re +++ b/src/array/Relude_Array_Base.re @@ -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) => @@ -285,4 +297,4 @@ let scanRight: (('a, 'b) => 'b, 'b, array('a)) => array('b) = (init, [||]), xs, ), - ); + ); \ No newline at end of file diff --git a/src/list/Relude_List_Base.re b/src/list/Relude_List_Base.re index 701bc6be..11d7054a 100644 --- a/src/list/Relude_List_Base.re +++ b/src/list/Relude_List_Base.re @@ -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) => @@ -279,4 +291,4 @@ let scanRight: (('a, 'b) => 'b, 'b, list('a)) => list('b) = (init, []), xs, ) - |> snd; + |> snd; \ No newline at end of file