From 67d6125f0328fb9509cec59306be8706f48e1f4b Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Wed, 29 Jan 2025 01:45:42 -0600 Subject: [PATCH] finish the iterate function --- src/Instructions/GenericInstructions.hs | 11 +++++++++++ src/Instructions/VectorIntInstructions.hs | 3 +++ test/Main.hs | 1 + 3 files changed, 15 insertions(+) diff --git a/src/Instructions/GenericInstructions.hs b/src/Instructions/GenericInstructions.hs index cafeb6c..7f0e850 100644 --- a/src/Instructions/GenericInstructions.hs +++ b/src/Instructions/GenericInstructions.hs @@ -326,3 +326,14 @@ instructionVectorRemove state primAccessor vectorAccessor = (Just (v1, vs), Just (p1, ps)) -> state & vectorAccessor .~ (replace v1 [p1] [] Nothing : vs) & primAccessor .~ ps _ -> state +instructionVectorIterate :: State -> Lens' State [a] -> Lens' State [[a]] -> ([a] -> Gene) -> (State -> State) -> State +instructionVectorIterate state@(State {_exec = e1 : es}) primAccessor vectorAccessor vectorType typeIterateFunction = + case uncons (view vectorAccessor state) of + Just ([], vs) -> state{_exec = es} & vectorAccessor .~ vs + Just ([x], vs) -> state & primAccessor .~ (x : view primAccessor state) & vectorAccessor .~ vs + Just (v1, vs) -> + (case uncons v1 of + Just (nv1, nvs) -> state{_exec = e1 : vectorType nvs : StateFunc typeIterateFunction : e1 : es} & primAccessor .~ (nv1 : view primAccessor state) & vectorAccessor .~ vs + _ -> state) -- This should never happen + _ -> state +instructionVectorIterate state _ _ _ _ = state diff --git a/src/Instructions/VectorIntInstructions.hs b/src/Instructions/VectorIntInstructions.hs index 5cf508c..38c63a9 100644 --- a/src/Instructions/VectorIntInstructions.hs +++ b/src/Instructions/VectorIntInstructions.hs @@ -62,3 +62,6 @@ instructionVectorIntReplaceFirst state = instructionVectorReplaceFirst state int instructionVectorIntRemove :: State -> State instructionVectorIntRemove state = instructionVectorRemove state int vectorInt + +instructionVectorIntIterate :: State -> State +instructionVectorIntIterate state = instructionVectorIterate state int vectorInt GeneVectorInt instructionVectorIntIterate diff --git a/test/Main.hs b/test/Main.hs index cdfcd88..cf578fd 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -288,3 +288,4 @@ main = do vectorIntTestFunc "instructionVectorIntReplaceFirst3" [[0,1,2,99,4,5,3,5,3]] [GeneInt 99, GeneInt 3, GeneVectorInt [0,1,2,3,4,5,3,5,3], StateFunc instructionVectorIntReplaceFirst] emptyState vectorIntTestFunc "instructionVectorIntReplaceFirst-2" [[0,1,2,3,4,5,3,5,3]] [GeneInt 99, GeneInt (-2), GeneVectorInt [0,1,2,3,4,5,3,5,3], StateFunc instructionVectorIntReplaceFirst] emptyState vectorIntTestFunc "instructionVectorIntRemove" [[0,1,2,4,5,5]] [GeneInt 3, GeneVectorInt [0,1,2,3,4,5,3,5,3], StateFunc instructionVectorIntRemove] emptyState + intTestFunc "instructionVectorIntIterate" [66] [GeneInt 40, GeneVectorInt [0,1,2,3,4,5,3,5,3], StateFunc instructionVectorIntIterate, StateFunc instructionIntAdd] emptyState