diff --git a/src/Instructions/GenericInstructions.hs b/src/Instructions/GenericInstructions.hs index 9131412..3513a95 100644 --- a/src/Instructions/GenericInstructions.hs +++ b/src/Instructions/GenericInstructions.hs @@ -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 diff --git a/src/Instructions/IntInstructions.hs b/src/Instructions/IntInstructions.hs index 2a1c7b6..1227003 100644 --- a/src/Instructions/IntInstructions.hs +++ b/src/Instructions/IntInstructions.hs @@ -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