I'm proud of these generic instructions

This commit is contained in:
Rowan Torbitzky-Lane 2025-01-25 00:14:37 -06:00
parent 2fb84b1da9
commit 35f8a20a91

View File

@ -5,9 +5,47 @@ import State
-- import Debug.Trace -- import Debug.Trace
-- Files in the spaces in [[a]] with [a] deleteAt :: Int -> [a] -> [a]
fillInHoles :: [a] -> [[a]] -> [a] deleteAt idx xs = take idx xs <> drop 1 (drop idx xs)
fillInHoles filler toFill = undefined -- TODO
findSubA :: forall a. Eq a => [a] -> [a] -> Int
findSubA fullA subA
| length fullA < length subA = -1
| length fullA == length subA = if fullA == subA then 0 else -1
| otherwise = findSubA' fullA subA 0
where
findSubA' :: [a] -> [a] -> Int -> Int
findSubA' fA sA subIndex
| null fA = -1
| length sA > length fA = -1
| sA == take (length sA) fA = subIndex
| otherwise = findSubA' (drop 1 fA) sA (subIndex + 1)
-- The int is the amount of olds to replace with new
-- Just chain findSubA calls lol
-- Nothing means replace all
-- May not be the most efficient method with the findSubA calls
replace :: Eq a => [a] -> [a] -> [a] -> Maybe Int -> [a]
replace fullA old new (Just amt) =
if findSubA fullA old /= -1 && amt > 0
then replace (take (findSubA fullA old) fullA <> new <> drop (findSubA fullA old + length old) fullA) old new (Just $ amt - 1)
else fullA
replace fullA old new Nothing =
if findSubA fullA old /= -1
then replace (take (findSubA fullA old) fullA <> new <> drop (findSubA fullA old + length old) fullA) old new Nothing
else fullA
amtOccurences :: forall a. Eq a => [a] -> [a] -> Int
amtOccurences fullA subA = amtOccurences' fullA subA 0
where
amtOccurences' :: [a] -> [a] -> Int -> Int
amtOccurences' fA sA count =
if findSubA fA sA /= -1
then amtOccurences' (replace fA sA mempty (Just 1)) sA (count + 1)
else count
combineTuple :: a -> ([a], [a]) -> [a]
combineTuple val tup = fst tup <> [val] <> snd tup
notEmptyStack :: State -> Lens' State [a] -> Bool notEmptyStack :: State -> Lens' State [a] -> Bool
notEmptyStack state accessor = not . null $ view accessor state notEmptyStack state accessor = not . null $ view accessor state
@ -79,9 +117,6 @@ instructionYankDup state@(State {_int = i : is}) accessor =
else state else state
instructionYankDup state@(State {_int = []}) _ = state instructionYankDup state@(State {_int = []}) _ = state
deleteAt :: Int -> [a] -> [a]
deleteAt idx xs = take idx xs <> drop 1 (drop idx xs)
-- Is this optimal? Running instrucitonYankDup twice????? -- Is this optimal? Running instrucitonYankDup twice?????
-- int non generic too -- int non generic too
instructionYank :: forall a. State -> Lens' State [a] -> State instructionYank :: forall a. State -> Lens' State [a] -> State
@ -97,9 +132,6 @@ instructionYank state@(State {_int = rawIndex : _}) accessor =
if notEmptyStack state accessor then deletedState & accessor .~ item : view accessor deletedState else state if notEmptyStack state accessor then deletedState & accessor .~ item : view accessor deletedState else state
instructionYank state _ = state instructionYank state _ = state
combineTuple :: a -> ([a], [a]) -> [a]
combineTuple val tup = fst tup <> [val] <> snd tup
-- int non generic :( -- int non generic :(
-- Rewrite this eventually? -- Rewrite this eventually?
instructionShoveDup :: State -> Lens' State [a] -> State instructionShoveDup :: State -> Lens' State [a] -> State