Skip to content

Commit

Permalink
feat(std): updateFirst for Array
Browse files Browse the repository at this point in the history
  • Loading branch information
danielo515 committed May 30, 2024
1 parent 8e221e5 commit e6f28e1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
19 changes: 19 additions & 0 deletions src/std/Array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export * from "fp-ts/Array";

/**
* Update the first element in an array that satisfies a predicate
* @param predicate - The predicate to satisfy
* @param update - The update function
*/
export const updateFirst =
<A>(predicate: (a: A) => boolean, update: (a: A) => A) =>
(as: A[]): A[] => {
return as.reduce((acc, a) => {
if (predicate(a)) {
acc.push(update(a));
return acc;
}
acc.push(a);
return acc;
}, [] as A[]);
};
23 changes: 1 addition & 22 deletions src/std/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
import {
compact,
filter,
filterMap,
findFirst,
findFirstMap,
flatten,
map as mapArr,
partition,
partitionMap,
} from "fp-ts/Array";
import {
Either,
ap,
Expand Down Expand Up @@ -40,17 +29,7 @@ export type { NonEmptyArray } from "fp-ts/NonEmptyArray";
export const flow = f;
export const pipe = p;
export const absurd = _absurd;
export const A = {
partitionMap,
partition,
compact,
findFirst,
findFirstMap,
map: mapArr,
filter,
filterMap,
flatten,
};
export * as A from "./Array";
/**
* Non empty array
*/
Expand Down

0 comments on commit e6f28e1

Please sign in to comment.