This commit is contained in:
Rowan Torbitzky-Lane 2025-01-19 19:50:27 -06:00
parent 774a0db148
commit add9503c88
2 changed files with 19 additions and 5 deletions

View File

@ -75,10 +75,24 @@ instructionYankDup state@(State {_int = i : is}) accessor =
else 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?????
-- int non generic too
instructionYank :: State -> Lens' State [a] -> State
instructionYank state accessor = instructionYankDup state accessor & accessor .~ init (view accessor (instructionYankDup state accessor))
instructionYank :: forall a. State -> Lens' State [a] -> State
-- instructionYank state accessor = instructionYankDup state accessor & accessor .~ init (view accessor (instructionYankDup state accessor))
instructionYank state@(State {_int = rawIndex : _}) accessor =
let
myIndex :: Int
myIndex = max 0 (min rawIndex (length (view accessor state) - 1))
item :: a
item = view accessor state !! myIndex
deletedState :: State
deletedState = state & accessor .~ deleteAt myIndex (view accessor state)
in
if notEmptyStack state accessor then deletedState & accessor .~ item : view accessor deletedState else state
instructionYank state _ = state
combineTuple :: a -> ([a], [a]) -> [a]
combineTuple val tup = fst tup <> [val] <> snd tup

View File

@ -81,6 +81,6 @@ instructionIntStackDepth :: State -> State
instructionIntStackDepth state = instructionStackDepth state int
-- int specific
instructionIntYank :: State -> State
instructionIntYank state@(State {_int = index : i2 : is}) = undefined
instructionIntYank state = state
-- instructionIntYank :: State -> State
-- instructionIntYank state@(State {_int = index : i2 : is}) = undefined
-- instructionIntYank state = state