From 14ec3b727e505db8d214e236fc9d9b3cc06f44da Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Mon, 10 Feb 2025 14:44:12 -0600 Subject: [PATCH] most string funcs generic/doced --- src/HushGP/Instructions/FloatInstructions.hs | 4 +- .../Instructions/GenericInstructions.hs | 26 ++++++-- src/HushGP/Instructions/StringInstructions.hs | 60 ++++++++++++++++--- .../Instructions/VectorBoolInstructions.hs | 12 ++-- .../Instructions/VectorCharInstructions.hs | 12 ++-- .../Instructions/VectorFloatInstructions.hs | 12 ++-- .../Instructions/VectorIntInstructions.hs | 12 ++-- .../Instructions/VectorStringInstructions.hs | 12 ++-- 8 files changed, 104 insertions(+), 46 deletions(-) diff --git a/src/HushGP/Instructions/FloatInstructions.hs b/src/HushGP/Instructions/FloatInstructions.hs index a0efe95..c7566a4 100644 --- a/src/HushGP/Instructions/FloatInstructions.hs +++ b/src/HushGP/Instructions/FloatInstructions.hs @@ -126,12 +126,12 @@ instructionFloatEq = instructionEq float instructionFloatStackDepth :: State -> State instructionFloatStackDepth = instructionStackDepth float --- |Moves an item from deep within the float stack to the top of the float stack based on +-- |Copies an item from deep within the float stack to the top of the float stack based on -- the top int from the int stack. instructionFloatYankDup :: State -> State instructionFloatYankDup = instructionYankDup float --- |Copies an item from deep within the float stack to the top of the float stack based on +-- |Moves an item from deep within the float stack to the top of the float stack based on -- the top int from the int stack. instructionFloatYank :: State -> State instructionFloatYank = instructionYank float diff --git a/src/HushGP/Instructions/GenericInstructions.hs b/src/HushGP/Instructions/GenericInstructions.hs index a5173da..ad35a7a 100644 --- a/src/HushGP/Instructions/GenericInstructions.hs +++ b/src/HushGP/Instructions/GenericInstructions.hs @@ -230,8 +230,8 @@ instructionShove :: Lens' State [a] -> State -> State instructionShove accessor state = instructionShoveDup accessor state & accessor .~ drop 1 (view accessor (instructionShoveDup accessor state )) -- |Concats two semigroupable items together based on a lens. Not char generic. -instructionConcat :: Semigroup a => Lens' State [a] -> State -> State -instructionConcat accessor state = +instructionVectorConcat :: Semigroup a => Lens' State [a] -> State -> State +instructionVectorConcat accessor state = case uncons (view accessor state) of Just (x1, x2:_) -> droppedState & accessor .~ (x1 <> x2) : view accessor droppedState _ -> state @@ -266,6 +266,8 @@ instructionVectorTakeN accessor state@(State {_int = i1 : is}) = _ -> state instructionVectorTakeN _ state = state +-- |Takes the last N items from the first vector on the top of a vector stack and +-- pushes the result to said vector stack. instructionVectorTakeRN :: Lens' State [[a]] -> State -> State instructionVectorTakeRN accessor state@(State {_int = i1 : is}) = case uncons (view accessor state) of @@ -372,6 +374,9 @@ instructionVectorDrop accessor state@(State {_int = i1 : is}) = _ -> state instructionVectorDrop _ state = state +-- |Based on a vector lens, drops the last N items from the top vector. +-- Pushes the result back to the vector stack. N is pulled from the top +-- of the int stack. instructionVectorDropR :: Lens' State [[a]] -> State -> State instructionVectorDropR accessor state@(State {_int = i1 : is}) = case uncons (view accessor state{_int = is}) of @@ -538,8 +543,9 @@ instructionVectorReplaceVectorN accessor state@(State {_int = i1 : is}) = instru instructionVectorReplaceVectorN _ state = state -- |Based on two lenses, one of a primitive type and the next of a vector type, --- Removes all occurrences inside of the top vector from the vector stack where the top --- item from the primitive stack equals a primitive inside of the vector stack. +-- Removes Maybe Int occurrences inside of the top vector from the vector stack where the top +-- item from the primitive stack equals a primitive inside of the vector stack. If Nothing is passed +-- rather than a Just Int, will remove all occurrences. instructionVectorRemove :: Eq a => Lens' State [a] -> Lens' State [[a]] -> Maybe Int -> State -> State instructionVectorRemove primAccessor vectorAccessor amt state = case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of @@ -549,11 +555,21 @@ instructionVectorRemove primAccessor vectorAccessor amt state = -- |Based on two lenses, one of a primitive type and the next of a vector type, -- Removes N occurrences inside of the top vector from the vector stack where the top -- item from the primitive stack equals a primitive inside of the vector stack. N is pulled --- from the top of the int stack. +-- from the top of the int stack. Not to be confused with instructionVectorRemoveNth. instructionVectorRemoveN :: Eq a => Lens' State [a] -> Lens' State [[a]] -> State -> State instructionVectorRemoveN primAccessor vectorAccessor state@(State {_int = i1 : is}) = instructionVectorRemove primAccessor vectorAccessor (Just i1) state{_int = is} instructionVectorRemoveN _ _ state = state +-- |Based on a vector lens. Removes the Nth index of the top vector of the passed +-- vector stack. N is pulled from the top of the int stack. Not to be confused with +-- instructionVectorRemoveN. +instructionVectorRemoveNth :: Lens' State [[a]] -> State -> State +instructionVectorRemoveNth accessor state@(State {_int = i1 : is}) = + case uncons (view accessor state{_int = is}) of + Just (v1, vs) -> state{_int = is} & accessor .~ (deleteAt (absNum i1 v1) v1 : vs) + _ -> state +instructionVectorRemoveNth _ state = state + -- |Based on a vector lens and the two vectors on top of said stack. -- Inside of the first vector, removes the number of instances specified -- by the Maybe Int parameter of the second vector. Nothing removes all instances. diff --git a/src/HushGP/Instructions/StringInstructions.hs b/src/HushGP/Instructions/StringInstructions.hs index 7abbc13..1804b4d 100644 --- a/src/HushGP/Instructions/StringInstructions.hs +++ b/src/HushGP/Instructions/StringInstructions.hs @@ -28,7 +28,7 @@ rstrip = reverse . lstrip . reverse -- |Concats the top two strings on the string stack and pushes the result. instructionStringConcat :: State -> State -instructionStringConcat = instructionConcat string +instructionStringConcat = instructionVectorConcat string -- |Swaps the top two strings on the string stack. instructionStringSwap :: State -> State @@ -243,84 +243,126 @@ instructionStringMakeEmpty = instructionVectorMakeEmpty string instructionStringIsEmptyString :: State -> State instructionStringIsEmptyString = instructionVectorIsEmpty string --- TODO: Make this generic +-- |Removes the Nth char from the top string of the string stack. N is pulled +-- from the top of the int stack. instructionStringRemoveNth :: State -> State -instructionStringRemoveNth state@(State {_string = s1 : ss, _int = i1 : is}) = state{_string = deleteAt (absNum i1 s1) s1 : ss, _int = is} -instructionStringRemoveNth state = state +instructionStringRemoveNth = instructionVectorRemoveNth string +-- |Sets the Nth char from the top string of the string stack to the top char from +-- the char stack. N is pulled from the top of the int stack. instructionStringSetNth :: State -> State instructionStringSetNth = instructionVectorSetNth char string +-- |Strips the whitespace of the top string on the string stack and pushes the result +-- back to the string stack. instructionStringStripWhitespace :: State -> State instructionStringStripWhitespace state@(State {_string = s1 : ss}) = state{_string = strip s1 : ss} instructionStringStripWhitespace state = state -instructionStringFromLens :: Show a => State -> Lens' State [a] -> State -instructionStringFromLens state@(State {_string = ss}) accessor = +-- |Utility Function: Casts a type based on a lens to a string. Pushes the result +-- to the string stack. +instructionStringFromLens :: Show a => Lens' State [a] -> State -> State +instructionStringFromLens accessor state@(State {_string = ss}) = case uncons (view accessor state) of Nothing -> state Just (x1,_) -> state{_string = show x1 : ss} +-- |Converts the top bool from the bool stack to a string. Pushes the result to +-- the string stack. instructionStringFromBool :: State -> State -instructionStringFromBool state = instructionStringFromLens state bool +instructionStringFromBool = instructionStringFromLens bool +-- |Converts the top int from the int stack to a string. Pushes the result to +-- the string stack. instructionStringFromInt :: State -> State -instructionStringFromInt state = instructionStringFromLens state int +instructionStringFromInt = instructionStringFromLens int +-- |Converts the top float from the float stack to a string. Pushes the result to +-- the string stack. instructionStringFromFloat :: State -> State -instructionStringFromFloat state = instructionStringFromLens state float +instructionStringFromFloat = instructionStringFromLens float +-- |Converts the top char from the char stack to a string. Pushes the result to +-- the string stack. instructionStringFromChar :: State -> State instructionStringFromChar state@(State {_string = ss, _char = c1 : cs}) = state{_string = [c1] : ss, _char = cs} instructionStringFromChar state = state +-- |Removes the top string from the string stack. instructionStringPop :: State -> State instructionStringPop = instructionPop string +-- |Duplicates the top string on the string stack. instructionStringDup :: State -> State instructionStringDup = instructionDup string +-- |Duplicates the top string on the string stack N times based off the top of the int stack. instructionStringDupN :: State -> State instructionStringDupN = instructionDupN string +-- |Rotates the top three strings on the string stack. instructionStringRot :: State -> State instructionStringRot = instructionRot string +-- |Sets the string stack to [] instructionStringFlush :: State -> State instructionStringFlush = instructionFlush string +-- |Checks to see if the top two strings are equal and pushes the result +-- to the bool stack. instructionStringEq :: State -> State instructionStringEq = instructionEq string +-- |Calculates the size of the string stack and pushes the result +-- to the int stack. instructionStringStackDepth :: State -> State instructionStringStackDepth = instructionStackDepth string +-- |Moves an item from deep within the string stack to the top of the string stack based on +-- the top int from the int stack. instructionStringYank :: State -> State instructionStringYank = instructionYank string +-- |Copies an item from deep within the string stack to the top of the string stack based on +-- the top int from the int stack. instructionStringYankDup :: State -> State instructionStringYankDup = instructionYankDup string +-- |Pushes True to the bool stack if the string stack is empty. Pushes False otherwise. instructionStringIsStackEmpty :: State -> State instructionStringIsStackEmpty = instructionIsStackEmpty string +-- |Moves an item from the top of the string stack to deep within the string stack based on +-- the top int from the int stack. instructionStringShove :: State -> State instructionStringShove = instructionShove string +-- |Copies an item from the top of the string stack to deep within the string stack based on +-- the top int from the int stack. instructionStringShoveDup :: State -> State instructionStringShoveDup = instructionShoveDup string +-- |Sorts the top string on the string stack by their ascii value and pushes the result +-- back to the string stack. instructionStringSort :: State -> State instructionStringSort = instructionVectorSort string +-- |Sorts the top string on the string stack backwards by their ascii value and pushes the result +-- back to the string stack. instructionStringSortReverse :: State -> State instructionStringSortReverse = instructionVectorSortReverse string +-- |Duplicate the top N items from the string stack based on the top int from the int stack. instructionStringDupItems :: State -> State instructionStringDupItems = instructionDupItems string +-- |Takes the top string and splits its up into strings of size 1 and pushes all of those +-- strings back onto the string stack. instructionStringParseToChar :: State -> State instructionStringParseToChar = instructionVectorParseToPrim string +-- |Uses the top two ints from the top of the int stack to pull a sub string +-- from the top string on the string stack. Pushes the result back to the +-- string stack. instructionStringSubString :: State -> State instructionStringSubString = instructionSubVector string diff --git a/src/HushGP/Instructions/VectorBoolInstructions.hs b/src/HushGP/Instructions/VectorBoolInstructions.hs index a69d00d..4294885 100644 --- a/src/HushGP/Instructions/VectorBoolInstructions.hs +++ b/src/HushGP/Instructions/VectorBoolInstructions.hs @@ -4,13 +4,13 @@ import HushGP.State import HushGP.Instructions.GenericInstructions instructionVectorBoolConcat :: State -> State -instructionVectorBoolConcat = instructionConcat vectorBool +instructionVectorBoolConcat = instructionVectorConcat vectorBool instructionVectorBoolConj :: State -> State -instructionVectorBoolConj = instructionConj bool vectorBool +instructionVectorBoolConj = instructionVectorConj bool vectorBool instructionVectorBoolTakeN :: State -> State -instructionVectorBoolTakeN = instructionTakeN vectorBool +instructionVectorBoolTakeN = instructionVectorTakeN vectorBool instructionVectorBoolSubVector :: State -> State instructionVectorBoolSubVector = instructionSubVector vectorBool @@ -25,10 +25,10 @@ instructionVectorBoolNth :: State -> State instructionVectorBoolNth = instructionVectorNth bool vectorBool instructionVectorBoolRest :: State -> State -instructionVectorBoolRest = instructionRest vectorBool +instructionVectorBoolRest = instructionVectorRest vectorBool instructionVectorBoolButLast :: State -> State -instructionVectorBoolButLast = instructionButLast vectorBool +instructionVectorBoolButLast = instructionVectorButLast vectorBool instructionVectorBoolLength :: State -> State instructionVectorBoolLength = instructionLength vectorBool @@ -61,7 +61,7 @@ instructionVectorBoolReplaceFirst :: State -> State instructionVectorBoolReplaceFirst = instructionVectorReplace bool vectorBool (Just 1) instructionVectorBoolRemove :: State -> State -instructionVectorBoolRemove = instructionVectorRemove bool vectorBool +instructionVectorBoolRemove = instructionVectorRemove bool vectorBool Nothing instructionVectorBoolIterate :: State -> State instructionVectorBoolIterate = instructionVectorIterate bool vectorBool GeneVectorBool instructionVectorBoolIterate "instructionVectorBoolIterate" diff --git a/src/HushGP/Instructions/VectorCharInstructions.hs b/src/HushGP/Instructions/VectorCharInstructions.hs index c7a4fe7..d5e0d29 100644 --- a/src/HushGP/Instructions/VectorCharInstructions.hs +++ b/src/HushGP/Instructions/VectorCharInstructions.hs @@ -4,13 +4,13 @@ import HushGP.State import HushGP.Instructions.GenericInstructions instructionVectorCharConcat :: State -> State -instructionVectorCharConcat = instructionConcat vectorChar +instructionVectorCharConcat = instructionVectorConcat vectorChar instructionVectorCharConj :: State -> State -instructionVectorCharConj = instructionConj char vectorChar +instructionVectorCharConj = instructionVectorConj char vectorChar instructionVectorCharTakeN :: State -> State -instructionVectorCharTakeN = instructionTakeN vectorChar +instructionVectorCharTakeN = instructionVectorTakeN vectorChar instructionVectorCharSubVector :: State -> State instructionVectorCharSubVector = instructionSubVector vectorChar @@ -25,10 +25,10 @@ instructionVectorCharNth :: State -> State instructionVectorCharNth = instructionVectorNth char vectorChar instructionVectorCharRest :: State -> State -instructionVectorCharRest = instructionRest vectorChar +instructionVectorCharRest = instructionVectorRest vectorChar instructionVectorCharButLast :: State -> State -instructionVectorCharButLast = instructionButLast vectorChar +instructionVectorCharButLast = instructionVectorButLast vectorChar instructionVectorCharLength :: State -> State instructionVectorCharLength = instructionLength vectorChar @@ -61,7 +61,7 @@ instructionVectorCharReplaceFirst :: State -> State instructionVectorCharReplaceFirst = instructionVectorReplace char vectorChar (Just 1) instructionVectorCharRemove :: State -> State -instructionVectorCharRemove = instructionVectorRemove char vectorChar +instructionVectorCharRemove = instructionVectorRemove char vectorChar Nothing instructionVectorCharIterate :: State -> State instructionVectorCharIterate = instructionVectorIterate char vectorChar GeneVectorChar instructionVectorCharIterate "instructionVectorCharIterate" diff --git a/src/HushGP/Instructions/VectorFloatInstructions.hs b/src/HushGP/Instructions/VectorFloatInstructions.hs index b972685..05a7848 100644 --- a/src/HushGP/Instructions/VectorFloatInstructions.hs +++ b/src/HushGP/Instructions/VectorFloatInstructions.hs @@ -4,13 +4,13 @@ import HushGP.State import HushGP.Instructions.GenericInstructions instructionVectorFloatConcat :: State -> State -instructionVectorFloatConcat = instructionConcat vectorFloat +instructionVectorFloatConcat = instructionVectorConcat vectorFloat instructionVectorFloatConj :: State -> State -instructionVectorFloatConj = instructionConj float vectorFloat +instructionVectorFloatConj = instructionVectorConj float vectorFloat instructionVectorFloatTakeN :: State -> State -instructionVectorFloatTakeN = instructionTakeN vectorFloat +instructionVectorFloatTakeN = instructionVectorTakeN vectorFloat instructionVectorFloatSubVector :: State -> State instructionVectorFloatSubVector = instructionSubVector vectorFloat @@ -25,10 +25,10 @@ instructionVectorFloatNth :: State -> State instructionVectorFloatNth = instructionVectorNth float vectorFloat instructionVectorFloatRest :: State -> State -instructionVectorFloatRest = instructionRest vectorFloat +instructionVectorFloatRest = instructionVectorRest vectorFloat instructionVectorFloatButLast :: State -> State -instructionVectorFloatButLast = instructionButLast vectorFloat +instructionVectorFloatButLast = instructionVectorButLast vectorFloat instructionVectorFloatLength :: State -> State instructionVectorFloatLength = instructionLength vectorFloat @@ -61,7 +61,7 @@ instructionVectorFloatReplaceFirst :: State -> State instructionVectorFloatReplaceFirst = instructionVectorReplace float vectorFloat (Just 1) instructionVectorFloatRemove :: State -> State -instructionVectorFloatRemove = instructionVectorRemove float vectorFloat +instructionVectorFloatRemove = instructionVectorRemove float vectorFloat Nothing instructionVectorFloatIterate :: State -> State instructionVectorFloatIterate = instructionVectorIterate float vectorFloat GeneVectorFloat instructionVectorFloatIterate "instructionVectorFloatIterate" diff --git a/src/HushGP/Instructions/VectorIntInstructions.hs b/src/HushGP/Instructions/VectorIntInstructions.hs index 1eca9ac..04c7b5e 100644 --- a/src/HushGP/Instructions/VectorIntInstructions.hs +++ b/src/HushGP/Instructions/VectorIntInstructions.hs @@ -4,13 +4,13 @@ import HushGP.Instructions.GenericInstructions import HushGP.State instructionVectorIntConcat :: State -> State -instructionVectorIntConcat = instructionConcat vectorInt +instructionVectorIntConcat = instructionVectorConcat vectorInt instructionVectorIntConj :: State -> State -instructionVectorIntConj = instructionConj int vectorInt +instructionVectorIntConj = instructionVectorConj int vectorInt instructionVectorIntTakeN :: State -> State -instructionVectorIntTakeN = instructionTakeN vectorInt +instructionVectorIntTakeN = instructionVectorTakeN vectorInt instructionVectorIntSubVector :: State -> State instructionVectorIntSubVector = instructionSubVector vectorInt @@ -25,10 +25,10 @@ instructionVectorIntNth :: State -> State instructionVectorIntNth = instructionVectorNth int vectorInt instructionVectorIntRest :: State -> State -instructionVectorIntRest = instructionRest vectorInt +instructionVectorIntRest = instructionVectorRest vectorInt instructionVectorIntButLast :: State -> State -instructionVectorIntButLast = instructionButLast vectorInt +instructionVectorIntButLast = instructionVectorButLast vectorInt instructionVectorIntLength :: State -> State instructionVectorIntLength = instructionLength vectorInt @@ -61,7 +61,7 @@ instructionVectorIntReplaceFirst :: State -> State instructionVectorIntReplaceFirst = instructionVectorReplace int vectorInt (Just 1) instructionVectorIntRemove :: State -> State -instructionVectorIntRemove = instructionVectorRemove int vectorInt +instructionVectorIntRemove = instructionVectorRemove int vectorInt Nothing instructionVectorIntIterate :: State -> State instructionVectorIntIterate = instructionVectorIterate int vectorInt GeneVectorInt instructionVectorIntIterate "instructionVectorIntIterate" diff --git a/src/HushGP/Instructions/VectorStringInstructions.hs b/src/HushGP/Instructions/VectorStringInstructions.hs index 9e0348c..64966f9 100644 --- a/src/HushGP/Instructions/VectorStringInstructions.hs +++ b/src/HushGP/Instructions/VectorStringInstructions.hs @@ -4,13 +4,13 @@ import HushGP.State import HushGP.Instructions.GenericInstructions instructionVectorStringConcat :: State -> State -instructionVectorStringConcat = instructionConcat vectorString +instructionVectorStringConcat = instructionVectorConcat vectorString instructionVectorStringConj :: State -> State -instructionVectorStringConj = instructionConj string vectorString +instructionVectorStringConj = instructionVectorConj string vectorString instructionVectorStringTakeN :: State -> State -instructionVectorStringTakeN = instructionTakeN vectorString +instructionVectorStringTakeN = instructionVectorTakeN vectorString instructionVectorStringSubVector :: State -> State instructionVectorStringSubVector = instructionSubVector vectorString @@ -25,10 +25,10 @@ instructionVectorStringNth :: State -> State instructionVectorStringNth = instructionVectorNth string vectorString instructionVectorStringRest :: State -> State -instructionVectorStringRest = instructionRest vectorString +instructionVectorStringRest = instructionVectorRest vectorString instructionVectorStringButLast :: State -> State -instructionVectorStringButLast = instructionButLast vectorString +instructionVectorStringButLast = instructionVectorButLast vectorString instructionVectorStringLength :: State -> State instructionVectorStringLength = instructionLength vectorString @@ -61,7 +61,7 @@ instructionVectorStringReplaceFirst :: State -> State instructionVectorStringReplaceFirst = instructionVectorReplace string vectorString (Just 1) instructionVectorStringRemove :: State -> State -instructionVectorStringRemove = instructionVectorRemove string vectorString +instructionVectorStringRemove = instructionVectorRemove string vectorString Nothing instructionVectorStringIterate :: State -> State instructionVectorStringIterate = instructionVectorIterate string vectorString GeneVectorString instructionVectorStringIterate "instructionVectorStringIterate"