Repository: gcanti/fp-ts (ts)

Issue:
snoc / cons for NonEmptyArray2v

Pull request:
add cons/snoc to nonEmptyArray (fixes #800)



Patch:
diff --git a/src/Array.ts b/src/Array.ts
index c96ce3c1..1eb83511 100644
--- a/src/Array.ts
+++ b/src/Array.ts
@@ -21,6 +21,7 @@ import { getArraySetoid, Setoid } from './Setoid'
 import { TraversableWithIndex1 } from './TraversableWithIndex'
 import { Unfoldable1 } from './Unfoldable'
 import { Witherable1 } from './Witherable'
+import { NonEmptyArray } from './NonEmptyArray2v'
 
 declare global {
   interface Array<T> {
@@ -463,7 +464,7 @@ export const index = <A>(i: number, as: Array<A>): Option<A> => {
 }
 
 /**
- * Attaches an element to the front of an array, creating a new array
+ * Attaches an element to the front of an array, creating a new non empty array
  *
  * @example
  * import { cons } from 'fp-ts/lib/Array'
@@ -472,18 +473,18 @@ export const index = <A>(i: number, as: Array<A>): Option<A> => {
  *
  * @since 1.0.0
  */
-export const cons = <A>(a: A, as: Array<A>): Array<A> => {
+export const cons = <A>(a: A, as: Array<A>): NonEmptyArray<A> => {
   const len = as.length
   const r = Array(len + 1)
   for (let i = 0; i < len; i++) {
     r[i + 1] = as[i]
   }
   r[0] = a
-  return r
+  return r as NonEmptyArray<A>
 }
 
 /**
- * Append an element to the end of an array, creating a new array
+ * Append an element to the end of an array, creating a new non empty array
  *
  * @example
  * import { snoc } from 'fp-ts/lib/Array'
@@ -492,14 +493,14 @@ export const cons = <A>(a: A, as: Array<A>): Array<A> => {
  *
  * @since 1.0.0
  */
-export const snoc = <A>(as: Array<A>, a: A): Array<A> => {
+export const snoc = <A>(as: Array<A>, a: A): NonEmptyArray<A> => {
   const len = as.length
   const r = Array(len + 1)
   for (let i = 0; i < len; i++) {
     r[i] = as[i]
   }
   r[len] = a
-  return r
+  return r as NonEmptyArray<A>
 }
 
 /**
diff --git a/src/NonEmptyArray2v.ts b/src/NonEmptyArray2v.ts
index 5f2190a9..6400b32d 100644
--- a/src/NonEmptyArray2v.ts
+++ b/src/NonEmptyArray2v.ts
@@ -281,6 +281,30 @@ const mapWithIndex = <A, B>(fa: NonEmptyArray<A>, f: (i: number, a: A) => B): No
   return fa.map((a, i) => f(i, a))
 }
 
+/**
+ * Append an element to the end of an array, creating a new non empty array
+ *
+ * @example
+ * import { snoc } from 'fp-ts/lib/NonEmptyArray2v'
+ *
+ * assert.deepStrictEqual(snoc([1, 2, 3], 4), [1, 2, 3, 4])
+ *
+ * @since 1.16.0
+ */
+export const snoc = A.snoc
+
+/**
+ * Append an element to the front of an array, creating a new non empty array
+ *
+ * @example
+ * import { cons } from 'fp-ts/lib/NonEmptyArray2v'
+ *
+ * assert.deepStrictEqual(cons(1, [2, 3, 4]), [1, 2, 3, 4])
+ *
+ * @since 1.16.0
+ */
+export const cons = A.cons
+
 /**
  * @since 1.15.0
  */


Test patch:
diff --git a/test/NonEmptyArray2v.ts b/test/NonEmptyArray2v.ts
index dc47c52a..1eb1bbbe 100644
--- a/test/NonEmptyArray2v.ts
+++ b/test/NonEmptyArray2v.ts
@@ -23,7 +23,9 @@ import {
   updateAt,
   filter,
   filterWithIndex,
-  fromNonEmptyArray
+  fromNonEmptyArray,
+  cons,
+  snoc
 } from '../src/NonEmptyArray2v'
 import { none, option, some, isSome } from '../src/Option'
 import { ordNumber } from '../src/Ord'
@@ -379,4 +381,12 @@ describe.only('NonEmptyArray2v', () => {
       nonEmptyArray.traverseWithIndex(I.identity)(ta, (i, a) => new I.Identity(f(i, a))).value
     )
   })
+
+  it('cons', () => {
+    assert.deepStrictEqual(cons(1, [2, 3, 4]), [1, 2, 3, 4])
+  })
+
+  it('snoc', () => {
+    assert.deepStrictEqual(snoc([1, 2, 3], 4), [1, 2, 3, 4])
+  })
 })