1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
//! A non-empty growable vector.
//!
//! This vector is guaranteed to have at least one element.
//! The first element is known as the head, and the remaining elements are known as the tail:
//! ```
//! # #[allow(dead_code)]
//! pub struct NEVec<T> {
//!     pub head: T,
//!     pub tail: Vec<T>,
//! }
//! ```
//!
//! The tail may be empty.
//! The length of the NEVec is always at least one.
//!
//! # Examples
//!
//! You can create a `NEVec` with [`NEVec::new`]:
//!
//! ```
//! use rust2fun::prelude::*;
//!
//! let nevec = NEVec::new(1);
//! ```
//!
//! ...or with the [`ne_vec!`] macro by providing at least one element:
//!
//! ```
//! use rust2fun::prelude::*;
//!
//! let nevec = ne_vec![1, 2, 3];
//! let nevec = ne_vec![1; 3]; // equivalent to ne_vec![1, 1, 1]
//! ```
//!
//! You can push and pop elements onto the tail:
//!
//! ```
//! use rust2fun::prelude::*;
//!
//! let mut nevec = ne_vec![1];
//! nevec.tail.push(2);
//! nevec.tail.push(3);
//! assert_eq!(nevec, [1, 2, 3]);
//!
//! assert_eq!(nevec.tail.pop(), Some(3));
//! assert_eq!(nevec, [1, 2]);
//! ```
//!
//! Non-empty vectors implement many of the same methods as [`Vec`] like ['NEVec::len`],
//! [`NEVec::first`], [`NEVec::last`], [`NEVec::get`], [`NEVec::get_mut`], [`NEVec::insert`],
//! [`NEVec::remove`], [`NEVec::swap_remove`], etc.
//!
//! # Iteration
//!
//! You can iterate over the elements of a `NEVec` with [`NEVec::iter`]:
//!
//! ```
//! use rust2fun::prelude::*;
//!
//! let a = ne_vec!["1", "22", "333"];
//! let b = a.iter().map(|x| x.len()).collect::<NEVec<_>>();
//! assert_eq!(b, ne_vec![1, 2, 3]);
//!
//! let a = ne_vec![1, 2, 3];
//! let b = a.iter().map(|x| x.to_string()).collect::<NEVec<_>>();
//! assert_eq!(b, ne_vec!["1", "2", "3"]);
//! ```
//!
//! Note, `collect` panics if the iterator is empty.
//!
//! ```should_panic
//! use rust2fun::prelude::*;
//!
//! let _panics = ne_vec![1, 2, 3].into_iter().filter(|&x| x == 0).collect::<NEVec<_>>();
//! ```
//!
//! # Indexing
//!
//! Non-empty vectors support indexing (through the [`Index`] and [`IndexMut`] traits).
//! The `head` element is at index `0`, and the `tail` elements are at indices `1` through
//! `len() - 1`.
//!
//! ```
//! use rust2fun::prelude::*;
//!
//! let nevec = ne_vec![1, 2, 3];
//! assert_eq!(nevec[0], 1);
//! assert_eq!(nevec[1], 2);
//! assert_eq!(nevec[2], 3);
//! ```
//!
//! [`ne_vec!`]: crate::ne_vec
use core::num::NonZeroUsize;
use std::ops::{Index, IndexMut};
use std::vec::Vec;
use std::{mem, ptr, vec};

use crate::functor::Functor;
use crate::pure::Pure;
use crate::semigroup::Semigroup;
use crate::{
    and_then_flat_map, apply_iter, flatmap_iter, higher, invariant_functor, semigroup_extend,
    semigroupal_iter,
};

mod from;
mod iter;
mod partial_eq;

/// A non-empty vector. The first element is `head`, and the remaining elements are `tail`.
/// The length of the NEVec is always at least one. The tail may be empty.
///
/// See the [module-level documentation](self) for more details.
#[allow(clippy::len_without_is_empty)]
#[derive(Clone, Debug, Eq, Hash, PartialOrd, Ord)]
pub struct NEVec<T> {
    /// The first element of the NEVec, known as the head.
    /// This is always present.
    pub head: T,
    /// The remaining elements of the NEVec, known as the tail.
    /// This may be empty.
    pub tail: Vec<T>,
}

impl<T> NEVec<T> {
    /// Constructs a new `NEVec<T>`.
    /// The first element of the NEVec is `head`.
    ///
    /// # Examples
    ///
    /// ```
    /// use rust2fun::prelude::*;
    ///
    /// let nevec = NEVec::new(1);
    /// assert_eq!(nevec.head, 1);
    /// assert_eq!(nevec.tail, []);
    /// assert_eq!(nevec, [1]);
    /// ```
    #[inline]
    pub const fn new(head: T) -> Self {
        Self {
            head,
            tail: Vec::new(),
        }
    }

    /// Constructs a new `NEVec<T>` with the given `head` and the capacity for `tail`.
    /// The tail will be empty.
    /// The capacity for `tail` is a lower bound; the `NEVec<T>` may hold more, but will not
    /// reallocate until it exceeds this value.
    /// If `tail_capacity` is `0`, the tail will not allocate.
    ///
    /// # Examples
    ///
    /// ```
    /// use rust2fun::prelude::*;
    ///
    /// let nevec = NEVec::with_tail_capacity(1, 2);
    /// assert_eq!(nevec.head, 1);
    /// assert_eq!(nevec.tail, []);
    /// assert_eq!(nevec.tail.capacity(), 2);
    /// assert_eq!(nevec, [1]);
    /// ```
    #[inline]
    pub fn with_tail_capacity(head: T, tail_capacity: usize) -> Self {
        Self {
            head,
            tail: Vec::with_capacity(tail_capacity),
        }
    }

    /// Constructs a new `NEVec<T>` with the given element repeated `n` times.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::num::NonZeroUsize;
    /// use rust2fun::prelude::*;
    ///
    /// let nevec = NEVec::from_elem(1, NonZeroUsize::new(3).unwrap());
    /// assert_eq!(nevec.head, 1);
    /// assert_eq!(nevec.tail, [1, 1]);
    /// assert_eq!(nevec, [1, 1, 1]);
    /// ```
    #[inline]
    pub fn from_elem(elem: T, n: NonZeroUsize) -> Self
    where
        T: Clone,
    {
        Self {
            head: elem.clone(),
            tail: vec![elem; n.get() - 1],
        }
    }

    /// Constructs a new `NEVec<T>` from a given [`Vec<T>`].
    /// Returns `None` if the given `Vec<T>` is empty.
    /// Otherwise, returns `Some(nevec)`, where `nevec` is the `NEVec<T>` constructed from the
    /// given `Vec<T>`.
    ///
    /// [`Vec<T>`]: std::vec::Vec
    ///
    /// # Examples
    ///
    /// ```
    /// use rust2fun::prelude::*;
    ///
    /// assert_eq!(NEVec::from_vec(vec![1, 2, 3]), Some(ne_vec![1, 2, 3]));
    /// assert_eq!(NEVec::<bool>::from_vec(vec![]), None);
    /// ```
    #[inline]
    pub fn from_vec(mut vec: Vec<T>) -> Option<Self> {
        if vec.is_empty() {
            None
        } else {
            Some(Self {
                head: vec.remove(0),
                tail: vec,
            })
        }
    }

    /// Constructs a new `NEVec<T>` from a given slice.
    /// Returns `None` if the given slice is empty.
    /// Otherwise, returns `Some(nevec)`, where `nevec` is the `NEVec<T>` constructed from the
    /// given slice.
    ///
    /// # Examples
    ///
    /// ```
    /// use rust2fun::prelude::*;
    ///
    /// assert_eq!(NEVec::from_slice(&[1, 2, 3]), Some(ne_vec![1, 2, 3]));
    /// assert_eq!(NEVec::<bool>::from_slice(&[]), None);
    /// ```
    #[inline]
    pub fn from_slice(slice: &[T]) -> Option<Self>
    where
        T: Clone,
    {
        slice.split_first().map(|(h, t)| Self {
            head: h.clone(),
            tail: t.to_vec(),
        })
    }

    /// Removes the element at the given index and returns it.
    ///
    /// The removed element is replaced by the last element of the NEVec.
    ///
    /// This does not preserve ordering, but is O(1).
    /// If you need to preserve ordering, use [`remove`] instead.
    ///
    /// [`remove`]: NEVec::remove
    ///
    /// # Panics
    ///
    /// Panics if the index is out of bounds.
    ///
    /// ```should_panic
    /// use rust2fun::prelude::*;
    ///
    /// let mut nevec = NEVec::new(0);
    /// nevec.swap_remove(1);
    /// ```
    ///
    /// Panics if the NEVec contains only one element.
    ///
    /// ```should_panic
    /// use rust2fun::prelude::*;
    ///
    /// let mut nevec = NEVec::new(0);
    /// nevec.swap_remove(0);
    /// ```
    ///
    /// # Examples
    ///
    /// ```
    /// use rust2fun::prelude::*;
    ///
    /// let mut nevec = ne_vec![1, 2, 3, 4];
    ///
    /// assert_eq!(nevec.swap_remove(1), 2);
    /// assert_eq!(nevec, ne_vec![1, 4, 3]);
    ///
    /// assert_eq!(nevec.swap_remove(0), 1);
    /// assert_eq!(nevec, ne_vec![3, 4]);
    ///
    /// assert_eq!(nevec.swap_remove(1), 4);
    /// assert_eq!(nevec, ne_vec![3]);
    /// ```
    #[inline]
    pub fn swap_remove(&mut self, index: usize) -> T {
        #[cold]
        #[inline(never)]
        fn assert_failed(index: usize, len: usize) -> ! {
            panic!("swap_remove index (is {index}) should be < len (is {len})");
        }

        let dst = if index == 0 {
            if self.tail.is_empty() {
                non_empty_invariant_failed();
            }

            &mut self.head
        } else {
            if index >= self.len() {
                assert_failed(index, self.len());
            };

            unsafe { self.tail.as_mut_ptr().add(index - 1) }
        };

        unsafe {
            let result = ptr::read(dst);
            let new_tail_len = self.tail.len() - 1;
            ptr::copy(self.tail.as_ptr().add(new_tail_len), dst, 1);
            self.tail.set_len(new_tail_len);
            result
        }
    }

    /// Inserts an element at position `index` within the NEVec, shifting all
    /// elements after it to the right.
    ///
    /// # Panics
    ///
    /// Panics if `index` is greater than the length of the NEVec.
    ///
    /// # Examples
    ///
    /// ```
    /// use rust2fun::prelude::*;
    ///
    /// let mut nevec = ne_vec![1];
    /// nevec.insert(1, 2);
    /// assert_eq!(nevec, [1, 2]);
    ///
    /// let mut nevec = ne_vec![1];
    /// nevec.insert(0, 2);
    /// assert_eq!(nevec, [2, 1]);
    /// nevec.insert(2, 3);
    /// assert_eq!(nevec, [2, 1, 3]);
    /// nevec.insert(0, 4);
    /// assert_eq!(nevec, [4, 2, 1, 3]);
    /// nevec.insert(1, 5);
    /// assert_eq!(nevec, [4, 5, 2, 1, 3]);
    /// ```
    #[inline]
    pub fn insert(&mut self, index: usize, element: T) {
        #[cold]
        #[inline(never)]
        fn assert_failed(index: usize, len: usize) -> ! {
            panic!("insertion index (is {index}) should be <= len (is {len})");
        }

        if index == 0 {
            self.tail.insert(0, mem::replace(&mut self.head, element));
        } else if index <= self.len() {
            self.tail.insert(index - 1, element);
        } else {
            assert_failed(index, self.len());
        }
    }

    /// Removes and returns the element at position `index` within the NEVec,
    /// shifting all elements after it to the left.
    ///
    /// Note that this function is O(n) because it needs to shift all elements
    /// in the NEVec by one to the left.If you don't need to preserve ordering,
    /// use [`swap_remove`] instead.
    ///
    /// [`swap_remove`]: NEVec::swap_remove
    ///
    /// # Panics
    ///
    /// Panics if `index` is out of bounds.
    ///
    /// ```should_panic
    /// use rust2fun::prelude::*;
    ///
    /// let mut nevec = NEVec::new(0);
    /// nevec.remove(1);
    /// ```
    ///
    /// Panics if the NEVec contains only one element.
    ///
    /// ```should_panic
    /// use rust2fun::prelude::*;
    ///
    /// let mut nevec = NEVec::new(0);
    /// nevec.remove(0);
    /// ```
    ///
    /// # Examples
    ///
    /// ```
    /// use rust2fun::prelude::*;
    ///
    /// let mut nevec = ne_vec![1, 2, 3];
    /// assert_eq!(nevec.remove(0), 1);
    /// assert_eq!(nevec, [2, 3]);
    ///
    /// assert_eq!(nevec.remove(1), 3);
    /// assert_eq!(nevec, [2]);
    /// ```
    #[inline]
    pub fn remove(&mut self, index: usize) -> T {
        #[cold]
        #[inline(never)]
        fn assert_failed(index: usize, len: usize) -> ! {
            panic!("removal index (is {index}) should be < len (is {len})");
        }

        if index == 0 {
            if self.tail.is_empty() {
                non_empty_invariant_failed();
            }

            mem::replace(&mut self.head, self.tail.remove(0))
        } else if index < self.len() {
            self.tail.remove(index - 1)
        } else {
            assert_failed(index, self.len());
        }
    }

    /// Returns the number of elements in the NEVec, including the head.
    /// This is always at least one.
    ///
    /// # Examples
    ///
    /// ```
    /// use rust2fun::prelude::*;
    ///
    /// let nevec = ne_vec![1, 2, 3];
    /// assert_eq!(nevec.len(), 3);
    /// ```
    #[inline]
    pub fn len(&self) -> usize {
        self.tail.len() + 1
    }

    /// Returns the first element of the NEVec. This is always the head.
    ///
    /// # Examples
    ///
    /// ```
    /// use rust2fun::prelude::*;
    ///
    /// let nevec = ne_vec![1, 2, 3];
    /// assert_eq!(nevec.first(), &1);
    /// ```
    #[inline]
    pub fn first(&self) -> &T {
        &self.head
    }

    /// Returns a mutable reference to the first element of the NEVec. This is always the head.
    ///
    /// # Examples
    ///
    /// ```
    /// use rust2fun::prelude::*;
    ///
    /// let mut nevec = ne_vec![1, 2, 3];
    /// *nevec.first_mut() = 4;
    /// assert_eq!(nevec, [4, 2, 3]);
    /// ```
    #[inline]
    pub fn first_mut(&mut self) -> &mut T {
        &mut self.head
    }

    /// Returns the last element of the NEVec. If the NEVec has length `1`, this is the head.
    /// Otherwise, it is the last element of the tail.
    ///
    /// # Examples
    ///
    /// ```
    /// use rust2fun::prelude::*;
    ///
    /// let nevec = ne_vec![1, 2, 3];
    /// assert_eq!(nevec.last(), &3);
    ///
    /// let nevec = ne_vec![1];
    /// assert_eq!(nevec.last(), &1);
    /// ```
    #[inline]
    pub fn last(&self) -> &T {
        self.tail.last().unwrap_or(&self.head)
    }

    /// Returns a mutable reference to the last element of the NEVec. If the NEVec has length `1`,
    /// this is the head. Otherwise, it is the last element of the tail.
    ///
    /// # Examples
    ///
    /// ```
    /// use rust2fun::prelude::*;
    ///
    /// let mut nevec = ne_vec![1, 2, 3];
    /// *nevec.last_mut() = 4;
    /// assert_eq!(nevec, [1, 2, 4]);
    ///
    /// let mut nevec = ne_vec![1];
    /// *nevec.last_mut() = 2;
    /// assert_eq!(nevec, [2]);
    /// ```
    #[inline]
    pub fn last_mut(&mut self) -> &mut T {
        self.tail.last_mut().unwrap_or(&mut self.head)
    }

    /// Returns a reference to an element, or `None` if out of bounds.
    /// If the index is `0`, this is the head.
    ///
    /// # Examples
    ///
    /// ```
    /// use rust2fun::prelude::*;
    ///
    /// let nevec = ne_vec![1, 2, 3];
    /// assert_eq!(nevec.get(0), Some(&1));
    /// assert_eq!(nevec.get(2), Some(&3));
    /// assert_eq!(nevec.get(3), None);
    /// ```
    #[inline]
    pub fn get(&self, index: usize) -> Option<&T> {
        if index == 0 {
            Some(&self.head)
        } else {
            self.tail.get(index - 1)
        }
    }

    /// Returns a mutable reference to an element, or `None` if out of bounds.
    /// If the index is `0`, this is the head.
    ///
    /// # Examples
    ///
    /// ```
    /// use rust2fun::prelude::*;
    ///
    /// let mut nevec = ne_vec![1, 2, 3];
    /// assert_eq!(nevec.get_mut(0), Some(&mut 1));
    /// assert_eq!(nevec.get_mut(2), Some(&mut 3));
    /// assert_eq!(nevec.get_mut(3), None);
    /// ```
    #[inline]
    pub fn get_mut(&mut self, index: usize) -> Option<&mut T> {
        if index == 0 {
            Some(&mut self.head)
        } else {
            self.tail.get_mut(index - 1)
        }
    }

    /// Copies `self` into a new [`Vec`].
    ///
    /// [`Vec`]: std::vec::Vec
    ///
    /// # Examples
    ///
    /// ```
    /// use rust2fun::prelude::*;
    ///
    /// let nevec = ne_vec![1, 2, 3];
    /// let vec = nevec.to_vec();
    /// assert_eq!(vec, [1, 2, 3]);
    /// assert_eq!(nevec, [1, 2, 3]);
    /// ```
    #[inline]
    pub fn to_vec(&self) -> Vec<T>
    where
        T: Clone,
    {
        let mut vec = Vec::with_capacity(self.len());
        vec.push(self.head.clone());
        vec.extend_from_slice(&self.tail);
        vec
    }

    /// Converts `self` into a [`Vec`].
    ///
    /// [`Vec`]: std::vec::Vec
    ///
    /// # Examples
    ///
    /// ```
    /// use rust2fun::prelude::*;
    ///
    /// let nevec = ne_vec![1, 2, 3];
    /// let vec = nevec.into_vec();
    /// assert_eq!(vec, [1, 2, 3]);
    /// ```
    #[inline]
    pub fn into_vec(self) -> Vec<T> {
        let mut vec = Vec::with_capacity(self.len());
        vec.push(self.head);
        vec.extend(self.tail);
        vec
    }
}

impl<T: Default> Default for NEVec<T> {
    #[inline]
    fn default() -> Self {
        Self::new(Default::default())
    }
}

impl<T> Extend<T> for NEVec<T> {
    #[inline]
    fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
        self.tail.extend(iter);
    }
}

impl<T> Index<usize> for NEVec<T> {
    type Output = T;

    #[inline]
    fn index(&self, index: usize) -> &Self::Output {
        if index == 0 {
            &self.head
        } else {
            &self.tail[index - 1]
        }
    }
}

impl<T> IndexMut<usize> for NEVec<T> {
    #[inline]
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        if index == 0 {
            &mut self.head
        } else {
            &mut self.tail[index - 1]
        }
    }
}

#[cold]
#[inline(never)]
fn non_empty_invariant_failed() -> ! {
    panic!("NEVec cannot be empty");
}

/// Creates a [`NEVec`] containing the arguments.
///
/// `ne_vec!` allows `NEVec`s to be defined with the same syntax as array expressions.
/// There are two forms of this macro:
///
/// - Create a [`NEVec`] containing a given list of elements:
///
/// ```
/// use rust2fun::prelude::*;
///
/// let nevec = ne_vec![1, 2, 3];
/// assert_eq!(nevec.head, 1);
/// assert_eq!(nevec.tail, [2, 3]);
/// ```
///
/// - Create a [`NEVec`] from a given element and size:
///
/// ```
/// use rust2fun::prelude::*;
///
/// let nevec = ne_vec![1; 3];
/// assert_eq!(nevec, [1, 1, 1]);
/// ```
///
/// Note that unlike array expressions this syntax supports all elements
/// which implement [`Clone`] and the number of elements doesn't have to be
/// a constant, but it has to be nonzero.
///
/// This will use `clone` to duplicate an expression, so one should be careful
/// using this with types having a nonstandard `Clone` implementation. For
/// example, `ne_vec![Rc::new(1); 5]` will create a vector of five references
/// to the same boxed integer value, not five references pointing to independently
/// boxed integers.
///
/// Also, note that `ne_vec![expr; 0]` is not allowed, and will panic, because
/// it violates the invariant that a `NEVec` cannot be empty.
#[macro_export]
macro_rules! ne_vec {
    ($head:expr) => (
        $crate::data::ne_vec::NEVec::new($head)
    );
    ($head:expr, $($tail:expr),* $(,)?) => (
        $crate::data::ne_vec::NEVec {
            head: $head,
            tail: vec![$($tail),*],
        }
    );
    ($elem:expr; $n:expr) => (
        $crate::data::ne_vec::NEVec::from_elem(
            $elem,
            core::num::NonZeroUsize::new($n).expect("NEVec cannot be empty"))
    );
}

higher!(NEVec);
apply_iter!(NEVec);
flatmap_iter!(NEVec);
semigroupal_iter!(NEVec);
semigroup_extend!(NEVec);
invariant_functor!(NEVec<T>);
and_then_flat_map!(NEVec<T>);

impl<A, B> Functor<B> for NEVec<A> {
    #[inline]
    fn map(self, mut f: impl FnMut(A) -> B) -> NEVec<B> {
        NEVec {
            head: f(self.head),
            tail: self.tail.map(f),
        }
    }
}

impl<T> Pure for NEVec<T> {
    #[inline]
    fn pure(x: T) -> Self {
        NEVec::new(x)
    }
}