Package fj.data

Class NonEmptyList<A>

  • All Implemented Interfaces:
    java.lang.Iterable<A>

    public final class NonEmptyList<A>
    extends java.lang.Object
    implements java.lang.Iterable<A>
    Provides an in-memory, immutable, singly linked list with total head and tail.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private A head  
      private List<A> tail  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private NonEmptyList​(A head, List<A> tail)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      NonEmptyList<A> append​(List<A> as)
      Appends the given list to this list.
      NonEmptyList<A> append​(NonEmptyList<A> as)
      Appends the given list to this list.
      <B> NonEmptyList<B> bind​(F<A,​NonEmptyList<B>> f)
      Binds the given function across each element of this list with a final join.
      NonEmptyList<A> cons​(A a)
      Prepend the given value to this list.
      boolean equals​(java.lang.Object obj)
      Perform an equality test on this list which delegates to the .equals() method of the member instances.
      A foldLeft1​(F<A,​F<A,​A>> f)
      Performs a left-fold reduction across this list.
      A foldLeft1​(F2<A,​A,​A> f)
      Performs a left-fold reduction across this list.
      A foldRight1​(F<A,​F<A,​A>> f)
      Performs a right-fold reduction across this list.
      A foldRight1​(F2<A,​A,​A> f)
      Performs a right-fold reduction across this list.
      static <A> Option<NonEmptyList<A>> fromList​(List<A> as)
      Returns a potential non-empty list from the given list.
      int hashCode()  
      A head()
      The first element of this linked list.
      NonEmptyList<A> intersperse​(A a)
      Intersperses the given argument between each element of this non empty list.
      java.util.Iterator<A> iterator()
      Returns an iterator for this non-empty list.
      static <A> NonEmptyList<A> join​(NonEmptyList<NonEmptyList<A>> o)
      Concatenate (join) a non empty list of non empty lists.
      int length()
      The length of this list.
      <B> NonEmptyList<B> map​(F<A,​B> f)
      Maps the given function across this list.
      <B> NonEmptyList<B> mapTails​(F<NonEmptyList<A>,​B> f)
      Maps the given function across the tails of this list (comonad pattern).
      A maximum​(Ord<A> o)
      Returns the maximum element in this non empty list according to the given ordering.
      A minimum​(Ord<A> o)
      Returns the minimum element in this non empty list according to the given ordering.
      static <A> F<A,​NonEmptyList<A>> nel()
      Returns a function that puts an element into a non-empty list.
      static <A> NonEmptyList<A> nel​(A head, A... tail)
      Constructs a non empty list from the given elements.
      static <A> NonEmptyList<A> nel​(A head, List<A> tail)
      Return a non-empty list with the given head and tail.
      NonEmptyList<A> reverse()
      Reverse this non empty list in constant stack space.
      NonEmptyList<A> snoc​(A a)
      Appends (snoc) the given element to this non empty list to produce a new non empty list.
      NonEmptyList<A> sort​(Ord<A> o)
      Sorts this non empty list using the given order over elements using a merge sort algorithm.
      NonEmptyList<NonEmptyList<A>> sublists()
      Returns a NonEmptyList of the sublists of this list.
      List<A> tail()
      This list without the first element.
      NonEmptyList<NonEmptyList<A>> tails()
      Returns a NonEmptyList of the tails of this list.
      java.util.Collection<A> toCollection()
      Projects an immutable collection of this non-empty list.
      List<A> toList()
      Returns a List projection of this list.
      static <A> F<NonEmptyList<A>,​List<A>> toList_()
      Returns a function that takes a non-empty list to a list.
      java.lang.String toString()  
      static <A,​B>
      P2<NonEmptyList<A>,​NonEmptyList<B>>
      unzip​(NonEmptyList<P2<A,​B>> xs)
      Transforms a non empty list of pairs into a non empty list of first components and a non empty list of second components.
      <B> NonEmptyList<P2<A,​B>> zip​(NonEmptyList<B> bs)
      Zips this non empty list with the given non empty list to produce a list of pairs.
      NonEmptyList<P2<A,​java.lang.Integer>> zipIndex()
      Zips this non empty list with the index of its element as a pair.
      <B,​C>
      NonEmptyList<C>
      zipWith​(List<B> bs, F<A,​F<B,​C>> f)
      Zips this non empty list with the given non empty list using the given function to produce a new list.
      <B,​C>
      NonEmptyList<C>
      zipWith​(List<B> bs, F2<A,​B,​C> f)
      Zips this non empty list with the given non empty list using the given function to produce a new list.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Field Detail

      • head

        private final A head
      • tail

        private final List<A> tail
    • Constructor Detail

      • NonEmptyList

        private NonEmptyList​(A head,
                             List<A> tail)
    • Method Detail

      • iterator

        public java.util.Iterator<A> iterator()
        Returns an iterator for this non-empty list. This method exists to permit the use in a for-each loop.
        Specified by:
        iterator in interface java.lang.Iterable<A>
        Returns:
        A iterator for this non-empty list.
      • head

        public A head()
        The first element of this linked list.
      • tail

        public List<A> tail()
        This list without the first element.
      • cons

        public NonEmptyList<A> cons​(A a)
        Prepend the given value to this list.
        Parameters:
        a - The value to prepend.
        Returns:
        A non-empty list with an extra element.
      • snoc

        public NonEmptyList<A> snoc​(A a)
        Appends (snoc) the given element to this non empty list to produce a new non empty list. O(n).
        Parameters:
        a - The element to append to this non empty list.
        Returns:
        A new non empty list with the given element appended.
      • length

        public int length()
        The length of this list.
        Returns:
        The length of this list.
      • append

        public NonEmptyList<A> append​(List<A> as)
        Appends the given list to this list.
        Parameters:
        as - The list to append.
        Returns:
        A new list with the given list appended.
      • append

        public NonEmptyList<A> append​(NonEmptyList<A> as)
        Appends the given list to this list.
        Parameters:
        as - The list to append.
        Returns:
        A new list with the given list appended.
      • foldRight1

        public final A foldRight1​(F<A,​F<A,​A>> f)
        Performs a right-fold reduction across this list. This function uses O(length) stack space.
      • foldRight1

        public final A foldRight1​(F2<A,​A,​A> f)
        Performs a right-fold reduction across this list. This function uses O(length) stack space.
      • foldLeft1

        public final A foldLeft1​(F<A,​F<A,​A>> f)
        Performs a left-fold reduction across this list. This function runs in constant space.
      • foldLeft1

        public final A foldLeft1​(F2<A,​A,​A> f)
        Performs a left-fold reduction across this list. This function runs in constant space.
      • map

        public <B> NonEmptyList<B> map​(F<A,​B> f)
        Maps the given function across this list.
        Parameters:
        f - The function to map across this list.
        Returns:
        A new list after the given function has been applied to each element.
      • bind

        public <B> NonEmptyList<B> bind​(F<A,​NonEmptyList<B>> f)
        Binds the given function across each element of this list with a final join.
        Parameters:
        f - The function to apply to each element of this list.
        Returns:
        A new list after performing the map, then final join.
      • sublists

        public NonEmptyList<NonEmptyList<A>> sublists()
        Returns a NonEmptyList of the sublists of this list.
        Returns:
        a NonEmptyList of the sublists of this list.
      • tails

        public NonEmptyList<NonEmptyList<A>> tails()
        Returns a NonEmptyList of the tails of this list. A list is considered a tail of itself for the purpose of this function (Comonad pattern).
        Returns:
        A NonEmptyList of the tails of this list.
      • mapTails

        public <B> NonEmptyList<B> mapTails​(F<NonEmptyList<A>,​B> f)
        Maps the given function across the tails of this list (comonad pattern).
        Parameters:
        f - The function to map across the tails of this list.
        Returns:
        The results of applying the given function to the tails of this list, as a NonEmptyList.
      • intersperse

        public NonEmptyList<A> intersperse​(A a)
        Intersperses the given argument between each element of this non empty list.
        Parameters:
        a - The separator to intersperse in this non empty list.
        Returns:
        A non empty list with the given separator interspersed.
      • reverse

        public NonEmptyList<A> reverse()
        Reverse this non empty list in constant stack space.
        Returns:
        A new non empty list with the elements in reverse order.
      • sort

        public NonEmptyList<A> sort​(Ord<A> o)
        Sorts this non empty list using the given order over elements using a merge sort algorithm.
        Parameters:
        o - The order over the elements of this non empty list.
        Returns:
        A sorted non empty list according to the given order.
      • minimum

        public final A minimum​(Ord<A> o)
        Returns the minimum element in this non empty list according to the given ordering.
        Parameters:
        o - An ordering for the elements of this non empty list.
        Returns:
        The minimum element in this list according to the given ordering.
      • maximum

        public final A maximum​(Ord<A> o)
        Returns the maximum element in this non empty list according to the given ordering.
        Parameters:
        o - An ordering for the elements of this non empty list.
        Returns:
        The maximum element in this list according to the given ordering.
      • zip

        public <B> NonEmptyList<P2<A,​B>> zip​(NonEmptyList<B> bs)
        Zips this non empty list with the given non empty list to produce a list of pairs. If this list and the given list have different lengths, then the longer list is normalised so this function never fails.
        Parameters:
        bs - The non empty list to zip this non empty list with.
        Returns:
        A new non empty list with a length the same as the shortest of this list and the given list.
      • zipIndex

        public NonEmptyList<P2<A,​java.lang.Integer>> zipIndex()
        Zips this non empty list with the index of its element as a pair.
        Returns:
        A new non empty list with the same length as this list.
      • zipWith

        public <B,​C> NonEmptyList<C> zipWith​(List<B> bs,
                                                   F<A,​F<B,​C>> f)
        Zips this non empty list with the given non empty list using the given function to produce a new list. If this list and the given list have different lengths, then the longer list is normalised so this function never fails.
        Parameters:
        bs - The non empty list to zip this non empty list with.
        f - The function to zip this non empty list and the given non empty list with.
        Returns:
        A new non empty list with a length the same as the shortest of this list and the given list.
      • zipWith

        public <B,​C> NonEmptyList<C> zipWith​(List<B> bs,
                                                   F2<A,​B,​C> f)
        Zips this non empty list with the given non empty list using the given function to produce a new list. If this list and the given list have different lengths, then the longer list is normalised so this function never fails.
        Parameters:
        bs - The non empty list to zip this non empty list with.
        f - The function to zip this non empty list and the given non empty list with.
        Returns:
        A new non empty list with a length the same as the shortest of this list and the given list.
      • unzip

        public static <A,​B> P2<NonEmptyList<A>,​NonEmptyList<B>> unzip​(NonEmptyList<P2<A,​B>> xs)
        Transforms a non empty list of pairs into a non empty list of first components and a non empty list of second components.
        Parameters:
        xs - The non empty list of pairs to transform.
        Returns:
        A non empty list of first components and a non empty list of second components.
      • toList

        public List<A> toList()
        Returns a List projection of this list.
        Returns:
        A List projection of this list.
      • toCollection

        public java.util.Collection<A> toCollection()
        Projects an immutable collection of this non-empty list.
        Returns:
        An immutable collection of this non-empty list.
      • toList_

        public static <A> F<NonEmptyList<A>,​List<A>> toList_()
        Returns a function that takes a non-empty list to a list.
        Returns:
        A function that takes a non-empty list to a list.
      • nel

        public static <A> NonEmptyList<A> nel​(A head,
                                              List<A> tail)
        Return a non-empty list with the given head and tail.
        Parameters:
        head - The first element of the new list.
        tail - The remaining elements of the new list.
        Returns:
        A non-empty list with the given head and tail.
      • nel

        @SafeVarargs
        public static <A> NonEmptyList<A> nel​(A head,
                                              A... tail)
        Constructs a non empty list from the given elements.
        Parameters:
        head - The first in the non-empty list.
        tail - The elements to construct a list's tail with.
        Returns:
        A non-empty list with the given elements.
      • nel

        public static <A> F<A,​NonEmptyList<A>> nel()
        Returns a function that puts an element into a non-empty list.
        Returns:
        A function that puts an element into a non-empty list.
      • fromList

        public static <A> Option<NonEmptyList<A>> fromList​(List<A> as)
        Returns a potential non-empty list from the given list. A non-value is returned if the given list is empty.
        Parameters:
        as - The list to construct a potential non-empty list with.
        Returns:
        A potential non-empty list from the given list.
      • join

        public static <A> NonEmptyList<A> join​(NonEmptyList<NonEmptyList<A>> o)
        Concatenate (join) a non empty list of non empty lists.
        Parameters:
        o - The non empty list of non empty lists to join.
        Returns:
        A new non empty list that is the concatenation of the given lists.
      • equals

        public boolean equals​(java.lang.Object obj)
        Perform an equality test on this list which delegates to the .equals() method of the member instances. This is implemented with Equal.nonEmptyListEqual using the anyEqual rule.
        Overrides:
        equals in class java.lang.Object
        Parameters:
        obj - the other object to check for equality against.
        Returns:
        true if this list is equal to the provided argument
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object