Skip to content

Commit eb51d8b

Browse files
committed
Nix.Builtins: replaceStrings: m refactor: mv funcs to local scope
1 parent 5a97037 commit eb51d8b

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

src/Nix/Builtins.hs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -938,27 +938,17 @@ replaceStrings
938938
-> NValue t f m
939939
-> NValue t f m
940940
-> m (NValue t f m)
941-
-- `ns*` goes for NixString here, which are with context - remember
942941
replaceStrings tfrom tto ts =
943-
fromValue (Deeper tfrom) >>= \(nsFrom :: [NixString]) ->
944-
fromValue (Deeper tto) >>= \(nsTo :: [NixString]) ->
945-
fromValue ts >>= \(ns :: NixString ) ->
946-
do
942+
do
943+
-- `ns*` goes for NixString here, which are with context - remember
944+
(nsFrom :: [NixString]) <- fromValue (Deeper tfrom)
945+
(nsTo :: [NixString]) <- fromValue (Deeper tto)
946+
(ns :: NixString ) <- fromValue ts
947+
947948
when (length nsFrom /= length nsTo)
948949
$ throwError $ ErrorCall "builtins.replaceStrings: Arguments `from`&`to` are lists `from` what replace `to` what, so the number of their inhabitanting elements must always match."
949950
let
950951

951-
from = fmap stringIgnoreContext nsFrom
952-
953-
lookupPrefix s =
954-
do -- monadic context handles Maybe result here, aka if Nothing returned
955-
(prefix, replacement) <- find ((`Text.isPrefixOf` s) . fst)
956-
$ zip from nsTo
957-
let rest = Text.drop (Text.length prefix) s
958-
pure (prefix, replacement, rest)
959-
960-
finish = makeNixString . LazyText.toStrict . Builder.toLazyText
961-
962952
go source resultAccum ctx =
963953
case lookupPrefix source of
964954
Nothing ->
@@ -976,6 +966,17 @@ replaceStrings tfrom tto ts =
976966
(finish result)
977967
(\(h, t) -> go t (result <> Builder.singleton h))
978968
(Text.uncons text)
969+
where
970+
finish = makeNixString . LazyText.toStrict . Builder.toLazyText
971+
972+
lookupPrefix src =
973+
do -- monadic context handles Maybe result here, aka if Nothing returned
974+
(prefix, replacement) <- find ((`Text.isPrefixOf` src) . fst)
975+
$ zip from nsTo
976+
let rest = Text.drop (Text.length prefix) src
977+
pure (prefix, replacement, rest)
978+
where
979+
from = fmap stringIgnoreContext nsFrom
979980

980981
toValue
981982
$ go (stringIgnoreContext ns) mempty

0 commit comments

Comments
 (0)