(In my actual use case I have a list of type [SomeType]
, SomeType
having a finite number of constructors, all nullary; in the following I'll use String
instead of [SomeType]
and use only 4 Char
s, to simplify a bit.)
I have a list like this "aaassddddfaaaffddsssadddssdffsdf"
where each element can be one of 'a'
, 's'
, 'd'
, 'f'
, and I want to do some further processing on each contiguous sequence of non-a
s, let's say turning them upper case and reversing the sequence, thus obtaining "aaaFDDDDSSaaaSSSDDFFaFDSFFDSSDDD"
. (I've added the reversing requirement to make it clear that the processing involves all the contiguous non 'a'
-s at the same time.)
To turn each sub-String
upper case, I can use this:
func :: String -> String
func = reverse . map Data.Char.toUpper
But how do I run that func
only on the sub-String
s of non-'a'
s?
My first thought is that Data.List.groupBy
can be useful, and the overall solution could be:
concat $ map (\x -> if head x == 'a' then x else func x)
$ Data.List.groupBy ((==) `on` (== 'a')) "aaassddddfaaaffddsssadddssdffsdf"
This solution, however, does not convince me, as I'm using == 'a'
both when grouping (which to me seems good and unavoidable) and when deciding whether I should turn a group upper case.
I'm looking for advices on how I can accomplish this small task in the best way.