Skip to content

Commit f735c75

Browse files
committed
refactor: Remove findAndAdjustA and adjustAtA'
We can define these in terms of their un-primed equivalents, by choosing the correct applicative, so we effectively do so and then inline. Signed-off-by: George Thomas <[email protected]>
1 parent c917038 commit f735c75

File tree

2 files changed

+19
-21
lines changed

2 files changed

+19
-21
lines changed

primer/src/Foreword.hs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ module Foreword (
3232
curry4,
3333
unsafeMaximum,
3434
spanMaybe,
35-
adjustAtA',
36-
findAndAdjustA',
3735
) where
3836

3937
-- In general, we should defer to "Protolude"'s exports and avoid name
@@ -132,11 +130,6 @@ adjustAtA n f xs = case splitAt n xs of
132130
(a, b : bs) -> f b <&> \b' -> Just $ a ++ [b'] ++ bs
133131
_ -> pure Nothing
134132

135-
adjustAtA' :: Applicative f => Int -> (a -> f (a, z)) -> [a] -> f (Maybe ([a], z))
136-
adjustAtA' n f xs = case splitAt n xs of
137-
(a, b : bs) -> f b <&> \(b', z) -> Just (a ++ [b'] ++ bs, z)
138-
_ -> pure Nothing
139-
140133
-- | Adjust the first element of the list which satisfies the
141134
-- predicate. Returns 'Nothing' if there is no such element.
142135
findAndAdjust :: (a -> Bool) -> (a -> a) -> [a] -> Maybe [a]
@@ -150,11 +143,6 @@ findAndAdjustA p f = \case
150143
[] -> pure Nothing
151144
x : xs -> if p x then Just . (: xs) <$> f x else (x :) <<$>> findAndAdjustA p f xs
152145

153-
findAndAdjustA' :: Applicative m => (a -> Bool) -> (a -> m (a, z)) -> [a] -> m (Maybe ([a], z))
154-
findAndAdjustA' p f = \case
155-
[] -> pure Nothing
156-
x : xs -> if p x then (\(x', z) -> Just . (,z) . (: xs) $ x') <$> f x else first (x :) <<$>> findAndAdjustA' p f xs
157-
158146
-- | Change the type of an error.
159147
modifyError :: MonadError e' m => (e -> e') -> ExceptT e m a -> m a
160148
modifyError f = runExceptT >=> either (throwError . f) pure

primer/src/Primer/Action.hs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,16 @@ import Foreword hiding (mod)
2727
import Control.Monad.Fresh (MonadFresh)
2828
import Data.Aeson (Value)
2929
import Data.Bifunctor.Swap qualified as Swap
30+
import Data.Bitraversable (bisequence)
31+
import Data.Functor.Compose (Compose (..))
3032
import Data.Generics.Product (typed)
3133
import Data.List (findIndex)
3234
import Data.List.NonEmpty qualified as NE
3335
import Data.Map (insert)
3436
import Data.Map.Strict qualified as Map
3537
import Data.Set qualified as Set
3638
import Data.Text qualified as T
39+
import Data.Tuple.Extra ((&&&))
3740
import Optics (over, set, (%), (?~), (^.), (^?), _Just)
3841
import Primer.Action.Actions (Action (..), Movement (..), QualifiedText)
3942
import Primer.Action.Available qualified as Available
@@ -260,16 +263,23 @@ applyActionsToField smartHoles imports (mod, mods) (tyName, conName', index, tyD
260263
where
261264
go :: ActionM m => m ([Module], TypeZ)
262265
go = do
266+
(tz, cs) <-
267+
getCompose
268+
. flip (findAndAdjustA ((== conName') . valConName)) (astTypeDefConstructors tyDef)
269+
$ Compose . \(ValCon _ ts) -> do
270+
(tz', cs') <-
271+
getCompose . flip (adjustAtA index) ts $
272+
Compose
273+
. fmap (First . Just &&& target . top)
274+
. flip withWrappedType \tz'' ->
275+
foldlM (\l a -> local addParamsToCxt $ applyActionAndSynth a l) (InType tz'') actions
276+
maybe
277+
(throwError $ InternalFailure "applyActionsToField: con field index out of bounds")
278+
(pure . first (First . Just))
279+
$ bisequence (getFirst tz', ValCon conName' <$> cs')
263280
(valCons, zt) <-
264-
(maybe (throwError $ InternalFailure "applyActionsToField: con name not found") pure =<<) $
265-
flip (findAndAdjustA' ((== conName') . valConName)) (astTypeDefConstructors tyDef) \(ValCon _ ts) -> do
266-
(t, zt) <-
267-
maybe (throwError $ InternalFailure "applyActionsToField: con field index out of bounds") pure
268-
=<< flip (adjustAtA' index) ts \fieldType -> do
269-
zt <- withWrappedType fieldType \zt ->
270-
foldlM (\l -> local addParamsToCxt . flip applyActionAndSynth l) (InType zt) actions
271-
pure (target (top zt), zt)
272-
pure (ValCon conName' t, zt)
281+
maybe (throwError $ InternalFailure "applyActionsToField: con name not found") pure $
282+
bisequence (cs, getFirst tz)
273283
let mod' = mod{moduleTypes = insert tyName (TypeDefAST tyDef{astTypeDefConstructors = valCons}) $ moduleTypes mod}
274284
(,zt) <$> checkEverything smartHoles (CheckEverything{trusted = imports, toCheck = mod' : mods})
275285
addParamsToCxt :: TC.Cxt -> TC.Cxt

0 commit comments

Comments
 (0)