@@ -236,11 +236,15 @@ berkeleyCite = try $ do
236236 prefix <- berkeleyCiteCommonPrefix <$> bcl
237237 suffix <- berkeleyCiteCommonSuffix <$> bcl
238238 citationList <- berkeleyCiteCitations <$> bcl
239− if parens
240− then return . toCite . addToFirstAndLast prefix suffix $ citationList
241− else return $ maybe mempty (<> " ") prefix
242− <> (toListOfCites $ map toInTextMode citationList)
243− <> maybe mempty (", " <>) suffix
239+ return $
240+ if parens
241+ then toCite
242+ . maybe id (\p -> alterFirst (prependPrefix p)) prefix
243+ . maybe id (\s -> alterLast (appendSuffix s)) suffix
244+ $ citationList
245+ else maybe mempty (<> " ") prefix
246+ <> (toListOfCites $ map toInTextMode citationList)
247+ <> maybe mempty (", " <>) suffix
244248 where
245249 toCite :: [Citation] -> Inlines
246250 toCite cs = B.cite cs mempty
@@ -251,18 +255,14 @@ berkeleyCite = try $ do
251255 toInTextMode :: Citation -> Citation
252256 toInTextMode c = c { citationMode = AuthorInText }
253257
254− addToFirstAndLast :: Maybe Inlines -> Maybe Inlines -> [Citation] -> [Citation]
255− addToFirstAndLast pre suf (c:cs) =
256− let firstCite = maybe c
257− (\p -> c { citationPrefix = B.toList p <> citationPrefix c })
258− pre
259− cites = firstCite:cs
260− lc = last cites
261− lastCite = maybe lc
262− (\s -> lc { citationSuffix = B.toList s <> citationSuffix lc })
263− suf
264− in init cites ++ [lastCite]
265− addToFirstAndLast _ _ _ = []
258+ alterFirst, alterLast :: (a -> a) -> [a] -> [a]
259+ alterFirst _ [] = []
260+ alterFirst f (c:cs) = (f c):cs
261+ alterLast f = reverse . alterFirst f . reverse
262+
263+ prependPrefix, appendSuffix :: Inlines -> Citation -> Citation
264+ prependPrefix pre c = c { citationPrefix = B.toList pre <> citationPrefix c }
265+ appendSuffix suf c = c { citationSuffix = citationSuffix c <> B.toList suf }
266266
267267data BerkeleyCitationList = BerkeleyCitationList
268268 { berkeleyCiteParens :: Bool