Replace some foldr with foldl'

This commit is contained in:
2022-08-23 15:40:15 +01:00
parent 12ce2365c2
commit 9f00e67298
11 changed files with 35 additions and 30 deletions
+6 -5
View File
@@ -3,6 +3,7 @@ module Dodge.Creature.State (
doDamage,
) where
import Data.Foldable
import Dodge.Base
import Dodge.Corpse.Make
import Dodge.Creature.Action
@@ -34,9 +35,9 @@ foldCr ::
World ->
World
--foldCr xs cr w = foldr ($ cr) w xs
foldCr xs cr w = foldr f w xs
foldCr xs cr w = foldl' f w $ reverse xs
where
f g w' = case w' ^? cWorld . creatures . ix (_crID cr) of
f w' g = case w' ^? cWorld . creatures . ix (_crID cr) of
Just cr' -> g cr' w'
Nothing -> w'
@@ -216,13 +217,13 @@ upInv cr = cWorld . creatures . ix (_crID cr) . crInv %~ IM.mapWithKey (itemUpda
-- a loop going over equipped items
equipmentEffects :: Creature -> World -> World
equipmentEffects cr = flip (foldr $ useEquipment cr) (IM.keys $ _crInvEquipped cr)
equipmentEffects cr = flip (foldl' $ flip $ useEquipment cr) (IM.keys $ _crInvEquipped cr)
-- a loop going over all inventory items
invSideEff :: Creature -> World -> World
invSideEff cr w = weaponReloadSounds cr $ IM.foldrWithKey f w (_crInv cr)
invSideEff cr w = weaponReloadSounds cr $ IM.foldlWithKey' f w (_crInv cr)
where
f i it w' =
f w' i it =
itemInvSideEffect cr it $
doItemTargeting i cr w' & maybe id (\g -> doInvEffect g it cr) (it ^? itEffect . ieInv)