import { curry, Predicate } from '@typed/lambda' import { Maybe, Nothing } from '@typed/maybe' import { NoInfer } from '../NoInfer' /** * Find a value in an array-like starting from the end of the array-like * @param predicate :: (a -> boolean) * @param list :: [a] * @returns :: Maybe a */ export const findLast: { (predicate: Predicate>, list: ArrayLike): Maybe (predicate: Predicate): (list: ArrayLike) => Maybe } = curry(__findLast) as { (predicate: Predicate>, list: ArrayLike): Maybe (predicate: Predicate): (list: ArrayLike) => Maybe } function __findLast(predicate: Predicate, list: ArrayLike): Maybe { for (let i = list.length - 1; i >= 0; --i) { const value = list[i] if (predicate(value)) { return Maybe.of(value) } } return Nothing }