Proposal #3271: new methods for Data.Sequence

Louis Wasserman wasserman.louis at
Mon Jul 20 18:15:11 EDT 2009

As a final addition to Data.Sequence, I've
following methods.  Possibly debatable design decisions are

   - mapWithIndex :: (Int -> a -> b) -> Seq a -> Seq b.  (This is based on
   mapAccumL, which is slightly suboptimal [as the full mapping must be done
   before the last element can be examined], but acceptable given the tiny code
   - elemIndex, elemIndices, findIndex, findIndices, all as in Data.List.
   - elemLastIndex, findLastIndex, elemIndicesDesc, findIndicesDesc, all
   working from right-to-left but otherwise analogous.
**Note:**findIndicesDesc and elemIndicesDesc return lists, not
Sequences, so as to
   permit maximum laziness.  Moreover, both of those methods fuse well.
   - takeWhileEnd, dropWhileEnd, spanEnd, breakEnd, all of which work from
   right-to-left but otherwise perform as their Data.List analogues.  *
   *Note:** spanEnd p xs, in particular, returns *first* the longest *suffix
   * of xs of elements satisfying p, and then the remaining prefix.

All but mapWithIndex take O(i) time, where i is the appropriate breakpoint
-- that is, they don't necessarily search the entire sequence.  (Each of the
xxxEnd methods take O(n-i).)

None of these methods are particularly code-intensive, save for some
rewrite-rules-based code that lets most of those methods be defined in terms
of findIndex, without any performance loss.  (In the absence of rewrite
rules, the additional overhead is small.)

Louis Wasserman
wasserman.louis at
-------------- next part --------------
An HTML attachment was scrubbed...

More information about the Libraries mailing list