From 7bb825991c63414d0e7b5f8a66e7fac6a965cf24 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Mon, 10 Feb 2025 16:10:53 -0600 Subject: [PATCH] fix docs/more string functions --- src/HushGP/Instructions/CharInstructions.hs | 7 +++++- .../Instructions/GenericInstructions.hs | 14 +++++++++-- src/HushGP/Instructions/StringInstructions.hs | 25 +++++-------------- 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/HushGP/Instructions/CharInstructions.hs b/src/HushGP/Instructions/CharInstructions.hs index 20f81c8..902dc46 100644 --- a/src/HushGP/Instructions/CharInstructions.hs +++ b/src/HushGP/Instructions/CharInstructions.hs @@ -64,7 +64,7 @@ instructionCharFromAsciiFloat state = state -- |Pushes the top string to the char stack split up into individual chars. -- For example: have the string "hello" and the char stack ['a', 'b', 'c'], the char stack --- looks like ['h', 'e', 'l', 'l', 'o', 'a', 'b', 'c']. +-- looks like ['h', 'e', 'l', 'l', 'o', 'a', 'b', 'c'] after this instruction executes. instructionCharsFromString :: State -> State instructionCharsFromString state@(State {_char = cs, _string = s1 : ss}) = state{_char = s1 <> cs, _string = ss} instructionCharsFromString state = state @@ -131,3 +131,8 @@ instructionCharShoveDup = instructionShoveDup char -- |Duplicate the top N items from the char stack based on the top int from the int stack. instructionCharDupItems :: State -> State instructionCharDupItems = instructionDupItems char + +-- |Takes the top string from the string stack and invidually pushes +-- all chars in said string to the char stack. +instructionCharFromAllString :: State -> State +instructionCharFromAllString = instructionPushAll char string diff --git a/src/HushGP/Instructions/GenericInstructions.hs b/src/HushGP/Instructions/GenericInstructions.hs index ad35a7a..e7e25af 100644 --- a/src/HushGP/Instructions/GenericInstructions.hs +++ b/src/HushGP/Instructions/GenericInstructions.hs @@ -297,6 +297,7 @@ instructionVectorFirst primAccessor vectorAccessor state = -- |Based on a vector lens, takes the first item from the top vector on the vector stack -- and creates a vector wrapping that first item, pushing it back onto the stack. +-- Not to be confused with instructionVectorFromFirstPrim. instructionVectorFromFirstPrim :: Lens' State [[a]] -> State -> State instructionVectorFromFirstPrim accessor state = case uncons (view accessor state) of @@ -306,6 +307,16 @@ instructionVectorFromFirstPrim accessor state = _ -> state _ -> state +-- |Based on two lenses, one of a primitive type and the next of a vector type, +-- pushes the top item of the primitive stack wrapped in a list to the top of the +-- vector stack. Not to be confused with instructionVectorFromFirstPrim. +instructionVectorFromPrim :: Lens' State [a] -> Lens' State [[a]] -> State -> State +instructionVectorFromPrim primAccessor vectorAccessor state = + case uncons (view primAccessor state) of + Just (p1, ps) -> state & primAccessor .~ ps & vectorAccessor .~ ([p1] : view vectorAccessor state) + _ -> state + + -- |Based on two lenses, one of a primitive type and the next of a vector type, -- Takes the last item from the top vector and places it onto the passed primitive stack. instructionVectorLast :: Lens' State [a] -> Lens' State [[a]] -> State -> State @@ -587,8 +598,7 @@ instructionVectorRemoveVectorN accessor state@(State {_int = i1 : is}) = instruc instructionVectorRemoveVectorN _ state = state -- |Based on two lenses, one of a primitive type and the next of a vector type, --- removes the first occurrence 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. +-- iterates over the top vector from the vector stack using the top code from the code stack. instructionVectorIterate :: Lens' State [a] -> Lens' State [[a]] -> ([a] -> Gene) -> (State -> State) -> String -> State -> State instructionVectorIterate primAccessor vectorAccessor vectorType typeIterateFunction typeIterateFunctionName state@(State {_exec = e1 : es}) = case uncons (view vectorAccessor state) of diff --git a/src/HushGP/Instructions/StringInstructions.hs b/src/HushGP/Instructions/StringInstructions.hs index 1804b4d..5f22327 100644 --- a/src/HushGP/Instructions/StringInstructions.hs +++ b/src/HushGP/Instructions/StringInstructions.hs @@ -38,55 +38,38 @@ instructionStringSwap = instructionSwap string -- on the string stack based on an int from the int stack. instructionStringInsertString :: State -> State instructionStringInsertString = instructionVectorInsertVector string --- instructionStringInsertString state@(State{_string = s1 : s2 : ss, _int = i1 : is}) = state {_string = combineTupleList s2 (splitAt i1 s1) : ss, _int = is} --- instructionStringInsertString state = state -- |Takes the first string from the string stack and pushes the first character -- back to the string stack as a string. instructionStringFromFirstChar :: State -> State instructionStringFromFirstChar = instructionVectorFromFirstPrim string --- instructionStringFromFirstChar state@(State {_string = (schar : _) : ss}) = state {_string = [schar] : ss} --- instructionStringFromFirstChar state = state -- |Takes the first string from the string stack and pushes the last character -- back to the string stack as a string. instructionStringFromLastChar :: State -> State instructionStringFromLastChar = instructionVectorFromLastPrim string --- instructionStringFromLastChar state@(State {_string = s1 : ss}) = - -- if not $ null s1 - -- then state {_string = [last s1] : ss} - -- else state --- instructionStringFromLastChar state = state -- |Takes the first string from the string stack and pushes the Nth character -- back to the string stack as a string. N in is the top int of the int stack. instructionStringFromNthChar :: State -> State instructionStringFromNthChar = instructionVectorFromNthPrim string --- instructionStringFromNthChar state@(State {_string = s1 : ss, _int = i1 : is}) = state{_string = [s1 !! absNum i1 s1] : ss, _int = is} --- instructionStringFromNthChar state = state -- |Takes the first two strings from the top of the string stack. Looks for and pushed the -- index of the second substring inside of the first substring to the int stack. -- If not found, returns -1. instructionStringIndexOfString :: State -> State instructionStringIndexOfString = instructionVectorIndexOfVector string --- instructionStringIndexOfString state@(State {_string = s1 : s2 : ss, _int = is}) = state {_string = ss, _int = findSubA s1 s2 : is} --- instructionStringIndexOfString state = state -- |Takes the first two strings from the top of the string stack. Pushes True to the -- bool stack if the second string is contained within the first string. Pushes False otherwise. instructionStringContainsString :: State -> State instructionStringContainsString = instructionVectorContainsVector string --- instructionStringContainsString state@(State {_string = s1 : s2 : ss, _bool = bs}) = state {_string = ss, _bool = (findSubA s1 s2 /= -1) : bs} --- instructionStringContainsString state = state -- |Takes the first two strings from the top of the string stack. Splits the first string -- based on the second string and pushes the result to the string stack. -- pysh reverses this. Check this for propeller instructionStringSplitOnString :: State -> State instructionStringSplitOnString = instructionVectorSplitOnVector string --- instructionStringSplitOnString state@(State {_string = s1 : s2 : ss}) = state {_string = reverse $ splitOn s2 s1 <> ss} --- instructionStringSplitOnString state = state -- |Takes the first three strings from the top of the string stack. Replaces the first instance of -- the second string within the first string with the third string. Pushes the result to the string stack. @@ -285,8 +268,7 @@ 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 +instructionStringFromChar = instructionVectorFromPrim char string -- |Removes the top string from the string stack. instructionStringPop :: State -> State @@ -366,3 +348,8 @@ instructionStringParseToChar = instructionVectorParseToPrim string -- string stack. instructionStringSubString :: State -> State instructionStringSubString = instructionSubVector string + +-- |Iterates over the top string on the string stack, applying the top instruction of the +-- exec stack along the way. +instructionStringIterate :: State -> State +instructionStringIterate = instructionVectorIterate char string GeneString instructionStringIterate "instructionStringIterate"