  # -------------------------------------------------------------------------------------------------------------------------  #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  #                                                                                                                            #
  #           File Name              :  allEqual.r                                                                             #
  #           Last Updated Funclist  :  17 Feb 2015,  8:48 PM (Tuesday)                                                        #
  #                                                                                                                            #
  #           Author Name            :  Rick Saporta                                                                           #
  #           Author Email           :  RickSaporta@gmail.com                                                                  #
  #           Author URL             :  www.github.com/rsaporta                                                                #
  #                                                                                                                            #
  #           Packages Called        :  NA                                                                                     #
  #           Packages Used via NS   :  NA                                                                                     #
  #                                                                                                                            #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  #                                                                                                                            #
  #   allEqual           ( L )                                                                                                 #
  #   identical_next     ( x, y, ... )                                                                                         #
  #                                                                                                                            #
  #                                                                                                                            #
  #                                                     <END FUNCS>                                                            #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  # -------------------------------------------------------------------------------------------------------------------------  #


## NOTE TO SELF:  This file created 2015-02-17
##                There is an older function areEqual which is longer and probably much slower
##                Deprecate the old one

allEqual <- function(L) {
## L must be a list, but CANNOT be a list of logical values
## If L has length less than 2, return TRUE by definition
  if (!is.list(L))
    stop ("L must be a list")
  if (length(L) < 2)
    return (TRUE)
  if (length(L) == 2)
    return (identical(L[[1]], L[[2]]))

  L1 <- L[[1]]
  if (is.logical(L[[1]]))
    stop ("L cannot be a list of logical values. TODO: This can be implemented, but need to check for the length of each element in L. If all 1, use (all(L) || !all(L)). If lengths are the same and more than 1, ... actually still cannot proceed as normal due to '&&'")

  identical(L1, Reduce(identical_next, tail(L, -1)))
}

identical_next <- function(x, y, ...) {
## If they are identical, returns the value u
## Otherwise, returns FALSE
## The idea is to use this with allEqual
  if (identical(x=x, y=y, ...))
    return (y)
  else 
    return (FALSE)
}
