import { Optional } from './common'; import { curry } from './curry'; import { notEqual } from './equals'; export const head: (array: T[]) => Optional = array => array[0]; export const last: (array: T[]) => Optional = array => array[array.length - 1]; export const tail = (array: T[] = []): T[] => array.slice(1); export const isArray = (array: T | T[]): array is T[] => Array.isArray(array); export const cutHead = ([head, ...tail]: T[]): { head: T; tail: T[] } => ({ head, tail }); export const length = (arr: T[]) => arr.length export const isEmpty = (array: T[]) => length(array) === 0; export const toArray = (value: T | T[]): T[] => (isArray(value) ? value : [value]); export type Remove = { (remove: T, from: T[]): T[]; (remove: T): (from: T[]) => T[]; } export const remove: Remove = curry( (value: T, array: R[]): R[] => filter(notEqual(value), array) ); export type Concat = { (a: A, b: B): A; (a: A): (b: B) => A; } export const concat: Concat = curry((head, tail) => head.concat(tail)); export const prepend = (value: any, array: T[]): T[] => [value].concat(array); export type Find = { (func: (bit: T) => boolean, array: T[]): Optional; (func: (bit: T) => boolean): (array: T[]) => Optional; } export const find: Find = curry((func, array) => array.find(func)); export type Filter = { (func: (a: T, index: number) => boolean, over: T[]): T[]; (func: (a: T, index: number) => boolean): (over: T[]) => T[]; } export const filter: Filter = curry((func, array) => array.filter(func)); export type Map = { (func: (a: T, index: number) => R, over: T[]): R[]; (func: (a: T, index: number) => R): (over: T[]) => R[]; } export const map: Map = curry((func, array) => (array.map(func)));