Skip to content

Commit 3d3b509

Browse files
committed
Builtins: add help fun concatWith, upd concat{List,Map}, add docs
1 parent f6f1d71 commit 3d3b509

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

src/Nix/Builtins.hs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,42 +1325,50 @@ lessThanNix ta tb =
13251325
vb <- demand tb
13261326

13271327
let
1328-
badType = throwError $ ErrorCall $ "builtins.lessThan: expected two numbers or two strings, " <> "got " <> show va <> " and " <> show vb
1328+
badType = throwError $ ErrorCall $ "builtins.lessThan: expected two numbers or two strings, got '" <> show va <> "' and '" <> show vb <> "'."
13291329

13301330
mkNVBool <$>
13311331
case (va, vb) of
13321332
(NVConstant ca, NVConstant cb) ->
13331333
case (ca, cb) of
1334-
(NInt a, NInt b ) -> pure $ a < b
1335-
(NFloat a, NInt b ) -> pure $ a < fromInteger b
1334+
(NInt a, NInt b) -> pure $ a < b
13361335
(NInt a, NFloat b) -> pure $ fromInteger a < b
1336+
(NFloat a, NInt b) -> pure $ a < fromInteger b
13371337
(NFloat a, NFloat b) -> pure $ a < b
13381338
_ -> badType
13391339
(NVStr a, NVStr b) -> pure $ stringIgnoreContext a < stringIgnoreContext b
13401340
_ -> badType
13411341

1342-
concatListsNix
1343-
:: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
1344-
concatListsNix =
1342+
-- | Helper function, generalization of @concat@ operations.
1343+
concatWith
1344+
:: forall e t f m
1345+
. MonadNix e t f m
1346+
=> (NValue t f m -> m (NValue t f m))
1347+
-> NValue t f m
1348+
-> m (NValue t f m)
1349+
concatWith f =
13451350
toValue . concat <=<
13461351
traverse
1347-
(fromValue @[NValue t f m] <=< demand)
1352+
(fromValue @[NValue t f m] <=< f)
13481353
<=< fromValue @[NValue t f m]
13491354

1355+
-- | Nix function of Haskell:
1356+
-- > concat :: [[a]] -> [a]
1357+
--
1358+
-- Concatenate a list of lists into a single list.
1359+
concatListsNix
1360+
:: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
1361+
concatListsNix = concatWith demand
1362+
1363+
-- | Nix function of Haskell:
1364+
-- > concatMap :: Foldable t => (a -> [b]) -> t a -> [b]
13501365
concatMapNix
13511366
:: forall e t f m
13521367
. MonadNix e t f m
13531368
=> NValue t f m
13541369
-> NValue t f m
13551370
-> m (NValue t f m)
1356-
concatMapNix f =
1357-
toValue . concat <=<
1358-
traverse
1359-
applyFunc
1360-
<=< fromValue @[NValue t f m]
1361-
where
1362-
applyFunc :: NValue t f m -> m [NValue t f m]
1363-
applyFunc = fromValue <=< callFunc f
1371+
concatMapNix f = concatWith (callFunc f)
13641372

13651373
listToAttrsNix
13661374
:: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)

0 commit comments

Comments
 (0)