Skip to content

Commit 0b26e1c

Browse files
committed
mapM{,_} -> traverse{._}
Let GHC figure-out where the specialization to Monad is effective and where use Applicative. Upon investigation I did not found the differences to use either, except of obvious ones.
1 parent 9d04d05 commit 0b26e1c

File tree

8 files changed

+20
-18
lines changed

8 files changed

+20
-18
lines changed

main/Main.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import Control.Monad
1616
import Control.Monad.Catch
1717
import Control.Monad.Free
1818
import Control.Monad.IO.Class
19+
import Data.Foldable ( traverse_ )
1920
import qualified Data.HashMap.Lazy as M
2021
import qualified Data.Map as Map
2122
import Data.List ( sortOn )
@@ -58,10 +59,10 @@ main = do
5859
handleResult opts mempty
5960
. parseNixTextLoc
6061
=<< liftIO Text.getContents
61-
paths -> mapM_ (processFile opts) paths
62-
Just "-" -> mapM_ (processFile opts) . lines =<< liftIO getContents
62+
paths -> traverse_ (processFile opts) paths
63+
Just "-" -> traverse_ (processFile opts) . lines =<< liftIO getContents
6364
Just path ->
64-
mapM_ (processFile opts) . lines =<< liftIO (readFile path)
65+
traverse_ (processFile opts) . lines =<< liftIO (readFile path)
6566
Just s -> handleResult opts mempty (parseNixTextLoc s)
6667
Just path -> do
6768
let file = addExtension (dropExtension path) "nixc"

src/Nix/Builtins.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ any_
535535
=> NValue t f m
536536
-> NValue t f m
537537
-> m (NValue t f m)
538-
any_ f = toValue <=< anyM fromValue <=< mapM (callFunc f) <=< fromValue
538+
any_ f = toValue <=< anyM fromValue <=< traverse (callFunc f) <=< fromValue
539539

540540
allM :: Monad m => (a -> m Bool) -> [a] -> m Bool
541541
allM _ [] = pure True
@@ -550,7 +550,7 @@ all_
550550
=> NValue t f m
551551
-> NValue t f m
552552
-> m (NValue t f m)
553-
all_ f = toValue <=< allM fromValue <=< mapM (callFunc f) <=< fromValue
553+
all_ f = toValue <=< allM fromValue <=< traverse (callFunc f) <=< fromValue
554554

555555
foldl'_
556556
:: forall e t f m
@@ -1179,7 +1179,7 @@ removeAttrs set v =
11791179
do
11801180
(m, p) <- fromValue @(AttrSet (NValue t f m), AttrSet SourcePos) set
11811181
(nsToRemove :: [NixString]) <- fromValue $ Deeper v
1182-
toRemove <- mapM fromStringNoContext nsToRemove
1182+
toRemove <- traverse fromStringNoContext nsToRemove
11831183
toValue (go m toRemove, go p toRemove)
11841184
where
11851185
go = foldl' (flip M.delete)
@@ -1406,7 +1406,7 @@ concatLists
14061406
:: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
14071407
concatLists =
14081408
toValue . concat <=<
1409-
mapM
1409+
traverse
14101410
(pure <=<
14111411
(fromValue @[NValue t f m]) <=< demand
14121412
)

src/Nix/Effects/Derivation.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ parsePath p = case Store.parsePath "/nix/store" (Text.encodeUtf8 p) of
9696
writeDerivation :: (Framed e m, MonadStore m) => Derivation -> m Store.StorePath
9797
writeDerivation drv@Derivation{inputs, name} = do
9898
let (inputSrcs, inputDrvs) = inputs
99-
references <- fmap Set.fromList $ mapM parsePath $ Set.toList $ Set.union inputSrcs $ Set.fromList $ Map.keys inputDrvs
99+
references <- fmap Set.fromList $ traverse parsePath $ Set.toList $ Set.union inputSrcs $ Set.fromList $ Map.keys inputDrvs
100100
path <- addTextToStore (Text.append name ".drv") (unparseDrv drv) (S.fromList $ Set.toList references) False
101101
parsePath $ Text.pack $ unStorePath path
102102

@@ -300,12 +300,12 @@ buildDerivationWithContext drvAttrs = do
300300
useJson <- getAttrOr "__structuredAttrs" False $ pure
301301
ignoreNulls <- getAttrOr "__ignoreNulls" False $ pure
302302

303-
args <- getAttrOr "args" mempty $ mapM (fromValue' >=> extractNixString)
303+
args <- getAttrOr "args" mempty $ traverse (fromValue' >=> extractNixString)
304304
builder <- getAttr "builder" $ extractNixString
305305
platform <- getAttr "system" $ extractNoCtx >=> assertNonNull
306306
mHash <- getAttrOr "outputHash" mempty $ extractNoCtx >=> (pure . pure)
307307
hashMode <- getAttrOr "outputHashMode" Flat $ extractNoCtx >=> parseHashMode
308-
outputs <- getAttrOr "outputs" ["out"] $ mapM (fromValue' >=> extractNoCtx)
308+
outputs <- getAttrOr "outputs" ["out"] $ traverse (fromValue' >=> extractNoCtx)
309309

310310
mFixedOutput <-
311311
maybe
@@ -333,7 +333,7 @@ buildDerivationWithContext drvAttrs = do
333333
rawString :: Text <- extractNixString jsonString
334334
pure $ Map.singleton "__json" rawString
335335
else
336-
mapM (lift . coerceToString callFunc CopyToStore CoerceAny >=> extractNixString) $
336+
traverse (lift . coerceToString callFunc CopyToStore CoerceAny >=> extractNixString) $
337337
Map.fromList $ M.toList $ deleteKeys [ "args", "__ignoreNulls" ] attrs
338338

339339
pure $ defaultDerivation { platform, builder, args, env, hashMode, useJson

src/Nix/Eval.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ attrSetAlter (k : ks) pos m p val =
228228
) <$> attrSetAlter ks pos st sp val
229229

230230
desugarBinds :: forall r . ([Binding r] -> r) -> [Binding r] -> [Binding r]
231-
desugarBinds embed binds = evalState (mapM (go <=< collect) binds) M.empty
231+
desugarBinds embed binds = evalState (traverse (go <=< collect) binds) M.empty
232232
where
233233
collect
234234
:: Binding r
@@ -268,7 +268,7 @@ evalBinds
268268
-> m (AttrSet v, AttrSet SourcePos)
269269
evalBinds recursive binds = do
270270
scope <- currentScopes :: m (Scopes m v)
271-
buildResult scope . concat =<< mapM (go scope) (moveOverridesLast binds)
271+
buildResult scope . concat =<< traverse (go scope) (moveOverridesLast binds)
272272
where
273273
moveOverridesLast = uncurry (<>) . partition
274274
(\case

src/Nix/Render/Frame.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ renderFrames (x : xs) = do
5252
| verbose opts <= Informational -> do
5353
f <- renderFrame @v @t @f x
5454
pure $ concatMap go (reverse xs) <> f
55-
| otherwise -> concat <$> mapM (renderFrame @v @t @f) (reverse (x : xs))
55+
| otherwise -> concat <$> traverse (renderFrame @v @t @f) (reverse (x : xs))
5656
pure $
5757
list
5858
mempty

src/Nix/Type/Infer.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ fresh = TVar <$> freshTVar
277277

278278
instantiate :: MonadState InferState m => Scheme -> m Type
279279
instantiate (Forall as t) = do
280-
as' <- mapM (const fresh) as
280+
as' <- traverse (const fresh) as
281281
let s = Subst $ Map.fromList $ zip as as'
282282
pure $ apply s t
283283

src/Nix/Utils.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ loeb :: Functor f => f (f a -> a) -> f a
7575
loeb x = go where go = fmap ($ go) x
7676

7777
loebM :: (MonadFix m, Traversable t) => t (t a -> m a) -> m (t a)
78-
loebM f = mfix $ \a -> mapM ($ a) f
78+
loebM f = mfix $ \a -> traverse ($ a) f
7979

8080
para :: Functor f => (f (Fix f, a) -> a) -> Fix f -> a
8181
para f = f . fmap (id &&& para f) . unFix

tests/ParserTests.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import Prettyprinter.Render.Text
2222
import Test.Tasty
2323
import Test.Tasty.HUnit
2424
import Test.Tasty.TH
25+
import Data.Foldable (traverse_)
2526

2627
case_constant_int = assertParseText "234" $ mkInt 234
2728

@@ -229,9 +230,9 @@ case_identifier_keyword_prefix = do
229230

230231
makeTextParseTest str = assertParseText ("\"" <> str <> "\"") $ mkStr str
231232

232-
case_simple_string = mapM_ makeTextParseTest ["abcdef", "a", "A", " a a ", ""]
233+
case_simple_string = traverse_ makeTextParseTest ["abcdef", "a", "A", " a a ", ""]
233234

234-
case_string_dollar = mapM_ makeTextParseTest ["a$b", "a$$b", "$cdef", "gh$i"]
235+
case_string_dollar = traverse_ makeTextParseTest ["a$b", "a$$b", "$cdef", "gh$i"]
235236

236237
case_string_escape = do
237238
assertParseText "\"\\$\\n\\t\\r\\\\\"" $ mkStr "$\n\t\r\\"

0 commit comments

Comments
 (0)