some :: forall n a . SubseqExtractor' n a -> SubseqExtractor' n [a]
some xa = SubseqExtractor' $ \note ->
  let as :: [Ranged a]
      as = runSubseq xa note
      -- Given a list of subseqs [Ranged a], find the adjacent groups [Ranged [a]].
      -- `Pure`s arguably can't be adjacent; not sure what to do with them. Currently ignored.
  in  fmap reverse <$> go Set.empty as
 where
  fromDistinct (DistinctRanged a l r) = Ranged a l r
  go :: Set (DistinctRanged [a]) -> [Ranged a] -> [Ranged [a]]
  go seen [] = fmap fromDistinct . toList $ seen
  go seen (rh@(Ranged h start end) : t) =
    let seen' :: Set (DistinctRanged [a])
        seen' =
          Set.fromList . join . fmap (toList . consRange rh) . toList $ seen
    in  go (Set.insert (DistinctRanged [h] start end) seen `Set.union` seen') t
  go seen (Pure _ : t) = go seen t

  consRange :: Ranged a -> DistinctRanged [a] -> Maybe (DistinctRanged [a])
  consRange new group@(DistinctRanged as start' _) = if isAdjacent group new
    then Just (DistinctRanged (get new : as) start' (end new))
    else Nothing

  -- Returns true if inputs are adjacent Ranged regions
  -- Question: Should a Pure be considered adjacent?
  isAdjacent :: forall a b . DistinctRanged a -> Ranged b -> Bool
  isAdjacent (DistinctRanged _ _ endA) (Ranged _ startB _) = endA + 1 == startB
  isAdjacent _                         _                   = False
some :: forall n a . SubseqExtractor' n a -> SubseqExtractor' n [a]
some xa = SubseqExtractor' $ \note ->
  let as :: [Ranged a]
      as = runSubseq xa note
      -- Given a list of subseqs [Ranged a], find the adjacent groups [Ranged [a]].
      -- `Pure`s arguably can't be adjacent; not sure what to do with them. Currently ignored.
  in  fmap reverse <$> go Set.empty as
 where
  fromDistinct (DistinctRanged a l r) = Ranged a l r
  go :: Set (DistinctRanged [a]) -> [Ranged a] -> [Ranged [a]]
  go seen [] = fmap fromDistinct . toList $ seen
  go seen (rh@(Ranged h start end) : t) =
    let seen' :: Set (DistinctRanged [a])
        seen' =
          Set.fromList . join . fmap (toList . consRange rh) . toList $ seen
    in  go (Set.insert (DistinctRanged [h] start end) seen `Set.union` seen') t
  go seen (Pure _ : t) = go seen t

  consRange :: Ranged a -> DistinctRanged [a] -> Maybe (DistinctRanged [a])
  consRange new group@(DistinctRanged as start' _) = if isAdjacent group new
    then Just (DistinctRanged (get new : as) start' (end new))
    else Nothing

  -- Returns true if inputs are adjacent Ranged regions
  -- Question: Should a Pure be considered adjacent?
  isAdjacent :: forall a b . DistinctRanged a -> Ranged b -> Bool
  isAdjacent (DistinctRanged _ _ endA) (Ranged _ startB _) = endA + 1 == startB
  isAdjacent _                         _                   = False
some xa = SubseqExtractor' $ \note ->
  let as :: [Ranged a]
      as = runSubseq xa note
      -- Given a list of subseqs [Ranged a], find the adjacent groups [Ranged [a]].
      -- `Pure`s arguably can't be adjacent; not sure what to do with them. Currently ignored.
  in  fmap reverse <$> go Set.empty as
 where
  fromDistinct (DistinctRanged a l r) = Ranged a l r
  go :: Set (DistinctRanged [a]) -> [Ranged a] -> [Ranged [a]]
  go seen [] = fmap fromDistinct . toList $ seen
  go seen (rh@(Ranged h start end) : t) =
    let seen' :: Set (DistinctRanged [a])
        seen' =
          Set.fromList . join . fmap (toList . consRange rh) . toList $ seen
    in  go (Set.insert (DistinctRanged [h] start end) seen `Set.union` seen') t
  go seen (Pure _ : t) = go seen t

  consRange :: Ranged a -> DistinctRanged [a] -> Maybe (DistinctRanged [a])
  consRange new group@(DistinctRanged as start' _) = if isAdjacent group new
    then Just (DistinctRanged (get new : as) start' (end new))
    else Nothing

  -- Returns true if inputs are adjacent Ranged regions
  -- Question: Should a Pure be considered adjacent?
  isAdjacent :: forall a b . DistinctRanged a -> Ranged b -> Bool
  isAdjacent (DistinctRanged _ _ endA) (Ranged _ startB _) = endA + 1 == startB
  isAdjacent _                         _                   = False