
  # -------------------------------------------------------------------------------------------------------------------------  #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  #                                                                                                                            #
  #           File Name              :  seq_find.r                                                                             #
  #           Last Updated Funclist  :  15 Feb 2014,  1:13 AM (Saturday)                                                       #
  #                                                                                                                            #
  #           Author Name            :  Rick Saporta                                                                           #
  #           Author Email           :  RickSaporta@gmail.com                                                                  #
  #           Author URL             :  www.github.com/rsaporta                                                                #
  #                                                                                                                            #
  #           Packages Called        :  NA                                                                                     #
  #           Packages Used via NS   :  NA                                                                                     #
  #                                                                                                                            #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  #                                                                                                                            #
  #   diffAsLogical      ( x )                                                                                                 #
  #   brkIndx            ( x )                                                                                                 #
  #                                                                                                                            #
  #                                                                                                                            #
  #                                                     <END FUNCS>                                                            #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  # -------------------------------------------------------------------------------------------------------------------------  #

#    x <- rep(c(T, F), times=c(5, 3))
#    y <- {set.seed(2); sample(x)}
#    
#    hh <- rep(c(T, F), times=c(1, 7))
#    tt <- rep(c(T, F), times=c(7, 1))
#    
#    
#    if sequence of one type, followed by another, then this should be (1): 
#    
#      sum(as.logical(diff(x)))    # 1
#      sum(as.logical(diff(!x)))   # 1
#      sum(as.logical(diff(y)))    # 3


## count the diffs
  diffAsLogical <- function(x) as.logical(diff(x))

## If it is a sequence of the kind we are looking for, 
##   then diffAsLogical will sum to one
##   and the sole TRUE value will have an index equal to
##   where the change in value occurs in the original x
  brkIndx <- function(x) ifelse( sum(dAL <- diffAsLogical(x))  == 1, which(dAL), NA)

## Index value to the change in the seq
indx <- brkIndx(x)

# this returns the length of each sequence. Note, okay for NA
c("Head"=indx, "Tail"=length(x)-indx)

# this returns the list chopped in two
if (is.na(indx))
  list()
else 
  list("Head"=head(x, indx), "Tail"=tail(x, -indx))

