//////////////////////////////////////////////////// // Para acceder al primer elemento del array //////////////////////////////////////////////////// const head = ([x]) => x const array = [1,2,3,4,5] head(array) // 1 //////////////////////////////////////////////////// // Traer todo menos el primer elemento //////////////////////////////////////////////////// const tail = ([, ...xs]) => xs const array = [1,2,3,4,5] tail(array) // [2,3,4,5] //////////////////////////////////////////////////// // Comprobar que el argumento esta definido //////////////////////////////////////////////////// const def = x => typeof x !== 'undefined' //////////////////////////////////////////////////// // Comprobar que el argumento no esta definido //////////////////////////////////////////////////// const undef = x => !def(x) //////////////////////////////////////////////////// // Copiar array //////////////////////////////////////////////////// const copy = array => [...array] //////////////////////////////////////////////////// // Length array //////////////////////////////////////////////////// const length = ([x, ...xs], len = 0) => def(x) ? length(xs, len + 1) : len // alternativa const length = ([x, ...xs]) => def(x) ? 1 + length(xs) : 0 //////////////////////////////////////////////////// // Reverse array //////////////////////////////////////////////////// const reverse = ([x, ...xs]) => def(x) ? [...reverse(xs), x] : [] ////////////////////////////////////////////////////// // First array - devuelve un array con los n primeros ////////////////////////////////////////////////////// const first = ([x, ...xs], n = 1) => def(x) && n ? [x, ...first(xs, n - 1)] : []