generic string instructions and more documentation
This commit is contained in:
parent
91bd09c00f
commit
2d6b888e2e
@ -286,6 +286,17 @@ instructionVectorFirst primAccessor vectorAccessor state =
|
|||||||
_ -> state
|
_ -> state
|
||||||
_ -> state
|
_ -> 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.
|
||||||
|
instructionVectorFromFirstPrim :: Lens' State [[a]] -> State -> State
|
||||||
|
instructionVectorFromFirstPrim accessor state =
|
||||||
|
case uncons (view accessor state) of
|
||||||
|
Just (v1, vs) ->
|
||||||
|
case uncons v1 of
|
||||||
|
Just (vp1, _) -> state & accessor .~ ([vp1] : vs)
|
||||||
|
_ -> state
|
||||||
|
_ -> state
|
||||||
|
|
||||||
-- |Based on two lenses, one of a primitive type and the next of a vector type,
|
-- |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.
|
-- 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
|
instructionVectorLast :: Lens' State [a] -> Lens' State [[a]] -> State -> State
|
||||||
@ -297,6 +308,17 @@ instructionVectorLast primAccessor vectorAccessor state =
|
|||||||
_ -> state
|
_ -> state
|
||||||
_ -> state
|
_ -> state
|
||||||
|
|
||||||
|
-- |Based on a vector lens, takes the last item from the top vector on the vector stack
|
||||||
|
-- and creates a vector wrapping that last item, pushing it back onto the stack.
|
||||||
|
instructionVectorFromLastPrim :: Lens' State [[a]] -> State -> State
|
||||||
|
instructionVectorFromLastPrim accessor state =
|
||||||
|
case uncons (view accessor state) of
|
||||||
|
Just (v1, vs) ->
|
||||||
|
case uncons (drop (length v1 - 1) v1) of
|
||||||
|
Just (vp1, _) -> state & accessor .~ ([vp1] : vs)
|
||||||
|
_ -> state
|
||||||
|
_ -> state
|
||||||
|
|
||||||
-- |Based on two lenses, one of a primitive type and the next of a vector type,
|
-- |Based on two lenses, one of a primitive type and the next of a vector type,
|
||||||
-- Takes the Nth item from the top vector and places it onto the passed primitive stack
|
-- Takes the Nth item from the top vector and places it onto the passed primitive stack
|
||||||
-- based on an int from the int stack.
|
-- based on an int from the int stack.
|
||||||
@ -307,6 +329,16 @@ instructionVectorNth primAccessor vectorAccessor state@(State {_int = i1 : is})
|
|||||||
_ -> state
|
_ -> state
|
||||||
instructionVectorNth _ _ state= state
|
instructionVectorNth _ _ state= state
|
||||||
|
|
||||||
|
-- |Based on a vector lens, takes the Nth item from the top vector on the vector stack
|
||||||
|
-- and creates a vector wrapping that Nth item, pushing it back onto the stack. N is
|
||||||
|
-- the top item on the int stack.
|
||||||
|
instructionVectorFromNthPrim :: Lens' State [[a]] -> State -> State
|
||||||
|
instructionVectorFromNthPrim accessor state@(State {_int = i1 : is}) =
|
||||||
|
case uncons (view accessor state) of
|
||||||
|
Just (v1, vs) -> state{_int = is} & accessor .~ ([v1 !! absNum i1 v1] : vs)
|
||||||
|
_ -> state
|
||||||
|
instructionVectorFromNthPrim _ state = state
|
||||||
|
|
||||||
-- |Takes the top vector, removes the first item of said vector, and pushes the result back to top
|
-- |Takes the top vector, removes the first item of said vector, and pushes the result back to top
|
||||||
-- of the stack, based on a lens.
|
-- of the stack, based on a lens.
|
||||||
instructionRest :: Lens' State [[a]] -> State -> State
|
instructionRest :: Lens' State [[a]] -> State -> State
|
||||||
@ -366,6 +398,15 @@ instructionVectorContains primAccessor vectorAccessor state@(State {_bool = bs})
|
|||||||
(Just (v1, vs), Just (p1, ps)) -> state{_bool = (findSubA v1 [p1] /= -1) : bs} & vectorAccessor .~ vs & primAccessor .~ ps
|
(Just (v1, vs), Just (p1, ps)) -> state{_bool = (findSubA v1 [p1] /= -1) : bs} & vectorAccessor .~ vs & primAccessor .~ ps
|
||||||
_ -> state
|
_ -> state
|
||||||
|
|
||||||
|
-- |Based on a vector lens and the two vectors on the top of said stack.
|
||||||
|
-- If the second vector can be found within the first vector, True is pushed to the
|
||||||
|
-- bool stack. If not, False is pushed to the bool stack.
|
||||||
|
instructionVectorContainsVector :: Eq a => Lens' State [[a]] -> State -> State
|
||||||
|
instructionVectorContainsVector accessor state@(State {_bool = bs}) =
|
||||||
|
case uncons (view accessor state) of
|
||||||
|
Just (v1, v2 : vs) -> state & accessor .~ vs & bool .~ ((findSubA v1 v2 /= (-1)) : bs)
|
||||||
|
_ -> state
|
||||||
|
|
||||||
-- |Based on two lenses, one of a primitive type and the next of a vector type,
|
-- |Based on two lenses, one of a primitive type and the next of a vector type,
|
||||||
-- finds the first index of the top item in the primitive stack inside of the
|
-- finds the first index of the top item in the primitive stack inside of the
|
||||||
-- top vector from the vector stack and pushes the result to the int stack.
|
-- top vector from the vector stack and pushes the result to the int stack.
|
||||||
@ -375,6 +416,14 @@ instructionVectorIndexOf primAccessor vectorAccessor state =
|
|||||||
(Just (v1, vs), Just (p1, ps)) -> (state & vectorAccessor .~ vs & primAccessor .~ ps) & int .~ (findSubA v1 [p1] : view int (state & vectorAccessor .~ vs & primAccessor .~ ps))
|
(Just (v1, vs), Just (p1, ps)) -> (state & vectorAccessor .~ vs & primAccessor .~ ps) & int .~ (findSubA v1 [p1] : view int (state & vectorAccessor .~ vs & primAccessor .~ ps))
|
||||||
_ -> state
|
_ -> state
|
||||||
|
|
||||||
|
-- |Based on a vector lens and the two vectors on top of said stack. Searches and pushes the
|
||||||
|
-- index of the second vector inside of the first vector to the int stack. Pushes -1 if not found.
|
||||||
|
instructionVectorIndexOfVector :: Eq a => Lens' State [[a]] -> State -> State
|
||||||
|
instructionVectorIndexOfVector accessor state@(State {_int = is}) =
|
||||||
|
case uncons (view accessor state) of
|
||||||
|
Just (v1, v2 : vs) -> state & accessor .~ vs & int .~ (findSubA v1 v2 : is)
|
||||||
|
_ -> state
|
||||||
|
|
||||||
-- |Based on two lenses, one of a primitive type and the next of a vector type,
|
-- |Based on two lenses, one of a primitive type and the next of a vector type,
|
||||||
-- finds the amount of times the top item in the primitive stack occurs inside of the
|
-- finds the amount of times the top item in the primitive stack occurs inside of the
|
||||||
-- top vector from the vector stack and pushes the result to the int stack.
|
-- top vector from the vector stack and pushes the result to the int stack.
|
||||||
@ -384,6 +433,15 @@ instructionVectorOccurrencesOf primAccessor vectorAccessor state =
|
|||||||
(Just (v1, vs), Just (p1, ps)) -> (state & vectorAccessor .~ vs & primAccessor .~ ps) & int .~ (amtOccurences v1 [p1] : view int (state & vectorAccessor .~ vs & primAccessor .~ ps))
|
(Just (v1, vs), Just (p1, ps)) -> (state & vectorAccessor .~ vs & primAccessor .~ ps) & int .~ (amtOccurences v1 [p1] : view int (state & vectorAccessor .~ vs & primAccessor .~ ps))
|
||||||
_ -> state
|
_ -> state
|
||||||
|
|
||||||
|
-- |Based on a vector lens and the top two vectors in said stack,
|
||||||
|
-- Counts the amount of occurrences of the second vector in the first
|
||||||
|
-- vector. Pushes the result to the string stack.
|
||||||
|
instructionVectorOccurrencesOfVector :: Eq a => Lens' State [[a]] -> State -> State
|
||||||
|
instructionVectorOccurrencesOfVector accessor state@(State {_int = is}) =
|
||||||
|
case uncons (view accessor state) of
|
||||||
|
Just (v1, v2 : vs) -> state & accessor .~ vs & int .~ (amtOccurences v1 v2 : is)
|
||||||
|
_ -> state
|
||||||
|
|
||||||
-- |This function parses the primitives inside a vector type and pushes that vector split into
|
-- |This function parses the primitives inside a vector type and pushes that vector split into
|
||||||
-- lists of size one onto the respective vector stack. Based on a vector lens.
|
-- lists of size one onto the respective vector stack. Based on a vector lens.
|
||||||
instructionVectorParseToPrim :: Lens' State [[a]] -> State -> State
|
instructionVectorParseToPrim :: Lens' State [[a]] -> State -> State
|
||||||
@ -392,7 +450,7 @@ instructionVectorParseToPrim accessor state =
|
|||||||
Just (x1, xs) -> state & accessor .~ (chunksOf 1 x1 <> xs)
|
Just (x1, xs) -> state & accessor .~ (chunksOf 1 x1 <> xs)
|
||||||
_ -> state
|
_ -> state
|
||||||
|
|
||||||
-- |Based on two lenses, one of a primitive type and the next of a vector type,
|
-- |Based on two lenses, one of a primitive type and the next of a vector type.
|
||||||
-- Sets the Nth index inside of the top vector from the vector stack to the top value
|
-- Sets the Nth index inside of the top vector from the vector stack to the top value
|
||||||
-- from the primitive stack. N is based on an int from the top of the int stack.
|
-- from the primitive stack. N is based on an int from the top of the int stack.
|
||||||
instructionVectorSetNth :: Lens' State [a] -> Lens' State [[a]] -> State -> State
|
instructionVectorSetNth :: Lens' State [a] -> Lens' State [[a]] -> State -> State
|
||||||
@ -402,28 +460,53 @@ instructionVectorSetNth primAccessor vectorAccessor state@(State {_int = i1 : is
|
|||||||
_ -> state
|
_ -> state
|
||||||
instructionVectorSetNth _ _ state = state
|
instructionVectorSetNth _ _ state = state
|
||||||
|
|
||||||
|
-- |Based on two lenses, one of a primitive type and the next of a vector type.
|
||||||
|
-- Splits the vector on top of the vector stack with the top primitive and pushes the
|
||||||
|
-- result to the original vector stack.
|
||||||
|
instructionVectorSplitOn :: Eq a => Lens' State [a] -> Lens' State [[a]] -> State -> State
|
||||||
|
instructionVectorSplitOn primAccessor vectorAccessor state =
|
||||||
|
case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of
|
||||||
|
(Just (v1, vs), Just (p1, ps)) -> state & primAccessor .~ ps & vectorAccessor .~ (reverse (splitOn [p1] v1) <> vs)
|
||||||
|
_ -> state
|
||||||
|
|
||||||
|
-- |Based on a vector lens and top two items of said stack, splits the
|
||||||
|
-- first vector based on the second vector and pushes the result to the
|
||||||
|
-- original vector stack.
|
||||||
|
instructionVectorSplitOnVector :: Eq a => Lens' State [[a]] -> State -> State
|
||||||
|
instructionVectorSplitOnVector accessor state =
|
||||||
|
case uncons (view accessor state) of
|
||||||
|
Just (v1, v2 : vs) -> state & accessor .~ (reverse (splitOn v2 v1) <> vs)
|
||||||
|
_ -> state
|
||||||
|
|
||||||
-- |Based on two lenses, one of a primitive type and the next of a vector type,
|
-- |Based on two lenses, one of a primitive type and the next of a vector type,
|
||||||
-- replaces all occurrences inside of the top vector from the vector stack with two values from
|
-- replaces all occurrences inside of the top vector from the vector stack with two values from
|
||||||
-- the primitive stack. The top of the primitive stack is the old value to be replaced. The second item
|
-- the primitive stack. The top of the primitive stack is the old value to be replaced. The second item
|
||||||
-- in the primitive stack is the new value to replace the old one.
|
-- in the primitive stack is the new value to replace the old one.
|
||||||
instructionVectorReplace :: Eq a => Lens' State [a] -> Lens' State [[a]] -> State -> State
|
instructionVectorReplace :: Eq a => Lens' State [a] -> Lens' State [[a]] -> Maybe Int -> State -> State
|
||||||
instructionVectorReplace primAccessor vectorAccessor state =
|
instructionVectorReplace primAccessor vectorAccessor amt state =
|
||||||
case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of
|
case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of
|
||||||
(Just (v1, vs), Just (p1, p2 : ps)) -> state & vectorAccessor .~ (replace v1 [p1] [p2] Nothing : vs) & primAccessor .~ ps
|
(Just (v1, vs), Just (p1, p2 : ps)) -> state & vectorAccessor .~ (replace v1 [p1] [p2] amt: vs) & primAccessor .~ ps
|
||||||
_ -> state
|
_ -> state
|
||||||
|
|
||||||
-- |Based on two lenses, one of a primitive type and the next of a vector type,
|
-- |Based on a vector lens and the top three vectors on said stack.
|
||||||
-- replaces the first occurrence inside of the top vector from the vector stack with two values from
|
-- Inside of the first vector, replaces the number of instances specified
|
||||||
-- the primitive stack. The top of the primitive stack is the old value to be replaced. The second item
|
-- by the Maybe Int parameter of the second vector with the third vector.
|
||||||
-- in the primitive stack is the new value to replace the old one.
|
-- If amt is Nothing, replaces all instances.
|
||||||
instructionVectorReplaceFirst :: Eq a => Lens' State [a] -> Lens' State [[a]] -> State -> State
|
instructionVectorReplaceVector :: Eq a => Lens' State [[a]] -> Maybe Int -> State -> State
|
||||||
instructionVectorReplaceFirst primAccessor vectorAccessor state =
|
instructionVectorReplaceVector accessor amt state =
|
||||||
case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of
|
case uncons (view accessor state) of
|
||||||
(Just (v1, vs), Just (p1, p2 : ps)) -> state & vectorAccessor .~ (replace v1 [p1] [p2] (Just 1) : vs) & primAccessor .~ ps
|
Just (v1, v2 : v3 : vs) -> state & accessor .~ (replace v1 v2 v3 amt : vs)
|
||||||
_ -> state
|
_ -> state
|
||||||
|
|
||||||
|
-- |Based on a vector lens, the top three vectors on said stack, and the top int on the int stack.
|
||||||
|
-- Inside of the first vector, replaces the number of instances specified
|
||||||
|
-- by the top of the int stack of the second vector with the third vector.
|
||||||
|
instructionVectorReplaceVectorN :: Eq a => Lens' State [[a]] -> State -> State
|
||||||
|
instructionVectorReplaceVectorN accessor state@(State {_int = i1 : is}) = instructionVectorReplaceVector accessor (Just i1) state{_int = is}
|
||||||
|
instructionVectorReplaceVectorN _ state = state
|
||||||
|
|
||||||
-- |Based on two lenses, one of a primitive type and the next of a vector type,
|
-- |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
|
-- 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.
|
-- item from the primitive stack equals a primitive inside of the vector stack.
|
||||||
instructionVectorRemove :: Eq a => Lens' State [a] -> Lens' State [[a]] -> State -> State
|
instructionVectorRemove :: Eq a => Lens' State [a] -> Lens' State [[a]] -> State -> State
|
||||||
instructionVectorRemove primAccessor vectorAccessor state =
|
instructionVectorRemove primAccessor vectorAccessor state =
|
||||||
@ -431,6 +514,22 @@ instructionVectorRemove primAccessor vectorAccessor state =
|
|||||||
(Just (v1, vs), Just (p1, ps)) -> state & vectorAccessor .~ (replace v1 [p1] [] Nothing : vs) & primAccessor .~ ps
|
(Just (v1, vs), Just (p1, ps)) -> state & vectorAccessor .~ (replace v1 [p1] [] Nothing : vs) & primAccessor .~ ps
|
||||||
_ -> 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.
|
||||||
|
instructionVectorRemoveVector :: Eq a => Lens' State [[a]] -> Maybe Int -> State -> State
|
||||||
|
instructionVectorRemoveVector accessor amt state =
|
||||||
|
case uncons (view accessor state) of
|
||||||
|
Just (v1, v2 : vs) -> state & accessor .~ (replace v1 v2 [] amt : vs)
|
||||||
|
_ -> state
|
||||||
|
|
||||||
|
-- |Based on a vector lens, the top two vectors on said stack, and the top int on the int stack.
|
||||||
|
-- Inside of the first vector, removes the number of instances specified
|
||||||
|
-- by the top of the int stack of the second vector.
|
||||||
|
instructionVectorRemoveVectorN :: Eq a => Lens' State [[a]] -> State -> State
|
||||||
|
instructionVectorRemoveVectorN accessor state@(State {_int = i1 : is}) = instructionVectorRemoveVector accessor (Just i1) state{_int = is}
|
||||||
|
instructionVectorRemoveVectorN _ state = state
|
||||||
|
|
||||||
-- |Based on two lenses, one of a primitive type and the next of a vector type,
|
-- |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
|
-- 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.
|
-- item from the primitive stack equals a primitive inside of the vector stack.
|
||||||
@ -467,3 +566,24 @@ instructionVectorSortReverse accessor state =
|
|||||||
case uncons (view accessor state) of
|
case uncons (view accessor state) of
|
||||||
Just (x, xs) -> state & accessor .~ (sortBy (comparing Data.Ord.Down) x : xs)
|
Just (x, xs) -> state & accessor .~ (sortBy (comparing Data.Ord.Down) x : xs)
|
||||||
_ -> state
|
_ -> state
|
||||||
|
|
||||||
|
-- |Takes a vector lens, a primitive lens, and the top of the int stack
|
||||||
|
-- Inserts the top of the primitive stack into a index specified by the
|
||||||
|
-- top of the int stack into the top vector from the vector stack.
|
||||||
|
instructionVectorInsert :: Lens' State [a] -> Lens' State [[a]] -> State -> State
|
||||||
|
instructionVectorInsert primAccessor vectorAccessor state@(State {_int = i1 : is}) =
|
||||||
|
case (uncons (view vectorAccessor state{_int = is}), uncons (view primAccessor state{_int = is})) of
|
||||||
|
(Just (v1, vs), Just (p1, ps)) -> state{_int = is} & primAccessor .~ ps & vectorAccessor .~ (combineTuple p1 (splitAt i1 v1) : vs)
|
||||||
|
_ -> state
|
||||||
|
instructionVectorInsert _ _ state = state
|
||||||
|
|
||||||
|
-- |Takes a vector lens and inserts the second vector on the vector stack
|
||||||
|
-- into the first vector on the vector stack based on an int from the
|
||||||
|
-- int stack.
|
||||||
|
instructionVectorInsertVector :: Lens' State [[a]] -> State -> State
|
||||||
|
instructionVectorInsertVector accessor state@(State {_int = i1 : is}) =
|
||||||
|
case uncons (view accessor state) of
|
||||||
|
Just (v1, v2 : vs) ->
|
||||||
|
state{_int = is} & accessor .~ (combineTupleList v2 (splitAt i1 v1) : vs)
|
||||||
|
_ -> state
|
||||||
|
instructionVectorInsertVector _ state = state
|
||||||
|
@ -5,13 +5,16 @@ import HushGP.Instructions.GenericInstructions
|
|||||||
import Data.List.Split
|
import Data.List.Split
|
||||||
import Control.Lens
|
import Control.Lens
|
||||||
|
|
||||||
|
-- |Utility String: Whitespack characters.
|
||||||
-- shamelessly stolen from https://hackage.haskell.org/package/MissingH-1.6.0.1/docs/src/Data.String.Utils.html#strip
|
-- shamelessly stolen from https://hackage.haskell.org/package/MissingH-1.6.0.1/docs/src/Data.String.Utils.html#strip
|
||||||
wschars :: String
|
wschars :: String
|
||||||
wschars = " \t\r\n"
|
wschars = " \t\r\n"
|
||||||
|
|
||||||
|
-- |Utility Function: Strips a string of its whitespace on both sides.
|
||||||
strip :: String -> String
|
strip :: String -> String
|
||||||
strip = lstrip . rstrip
|
strip = lstrip . rstrip
|
||||||
|
|
||||||
|
-- |Utility Function: Strips a string of its whitespace on the left side.
|
||||||
lstrip :: String -> String
|
lstrip :: String -> String
|
||||||
lstrip s = case s of
|
lstrip s = case s of
|
||||||
[] -> []
|
[] -> []
|
||||||
@ -19,99 +22,136 @@ lstrip s = case s of
|
|||||||
then lstrip xs
|
then lstrip xs
|
||||||
else s
|
else s
|
||||||
|
|
||||||
-- this is a tad inefficient init
|
-- |Utility Function: Strips a string of its whitespace on the right side.
|
||||||
|
-- this is a tad inefficient
|
||||||
rstrip :: String -> String
|
rstrip :: String -> String
|
||||||
rstrip = reverse . lstrip . reverse
|
rstrip = reverse . lstrip . reverse
|
||||||
|
|
||||||
|
-- |Concats the top two strings on the string stack and pushes the result.
|
||||||
instructionStringConcat :: State -> State
|
instructionStringConcat :: State -> State
|
||||||
instructionStringConcat = instructionConcat string
|
instructionStringConcat = instructionConcat string
|
||||||
|
|
||||||
|
-- |Swaps the top two strings on the string stack.
|
||||||
instructionStringSwap :: State -> State
|
instructionStringSwap :: State -> State
|
||||||
instructionStringSwap = instructionSwap string
|
instructionStringSwap = instructionSwap string
|
||||||
|
|
||||||
|
-- |Inserts the second string on the string stack into the first string
|
||||||
|
-- on the string stack based on an int from the int stack.
|
||||||
instructionStringInsertString :: State -> State
|
instructionStringInsertString :: State -> State
|
||||||
instructionStringInsertString state@(State{_string = s1 : s2 : ss, _int = i1 : is}) = state {_string = combineTupleList s2 (splitAt i1 s1) : ss, _int = is}
|
instructionStringInsertString = instructionVectorInsertVector string
|
||||||
instructionStringInsertString state = state
|
-- 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 :: State -> State
|
||||||
instructionStringFromFirstChar state@(State {_string = (schar : _) : ss}) = state {_string = [schar] : ss}
|
instructionStringFromFirstChar = instructionVectorFromFirstPrim string
|
||||||
instructionStringFromFirstChar state = state
|
-- 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 :: State -> State
|
||||||
instructionStringFromLastChar state@(State {_string = s1 : ss}) =
|
instructionStringFromLastChar = instructionVectorFromLastPrim string
|
||||||
if not $ null s1
|
-- instructionStringFromLastChar state@(State {_string = s1 : ss}) =
|
||||||
then state {_string = [last s1] : ss}
|
-- if not $ null s1
|
||||||
else state
|
-- then state {_string = [last s1] : ss}
|
||||||
instructionStringFromLastChar state = state
|
-- 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 :: State -> State
|
||||||
instructionStringFromNthChar state@(State {_string = s1 : ss, _int = i1 : is}) = state{_string = [s1 !! absNum i1 s1] : ss, _int = is}
|
instructionStringFromNthChar = instructionVectorFromNthPrim string
|
||||||
instructionStringFromNthChar state = state
|
-- 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 :: State -> State
|
||||||
instructionStringIndexOfString state@(State {_string = s1 : s2 : ss, _int = is}) = state {_string = ss, _int = findSubA s1 s2 : is}
|
instructionStringIndexOfString = instructionVectorIndexOfVector string
|
||||||
instructionStringIndexOfString state = state
|
-- 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 :: State -> State
|
||||||
instructionStringContainsString state@(State {_string = s1 : s2 : ss, _bool = bs}) = state {_string = ss, _bool = (findSubA s1 s2 /= -1) : bs}
|
instructionStringContainsString = instructionVectorContainsVector string
|
||||||
instructionStringContainsString state = state
|
-- 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
|
-- pysh reverses this. Check this for propeller
|
||||||
instructionStringSplitOnString :: State -> State
|
instructionStringSplitOnString :: State -> State
|
||||||
instructionStringSplitOnString state@(State {_string = s1 : s2 : ss}) = state {_string = reverse $ splitOn s2 s1 <> ss}
|
instructionStringSplitOnString = instructionVectorSplitOnVector string
|
||||||
instructionStringSplitOnString state = state
|
-- 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.
|
||||||
instructionStringReplaceFirstString :: State -> State
|
instructionStringReplaceFirstString :: State -> State
|
||||||
instructionStringReplaceFirstString state@(State {_string = s1 : s2 : s3 : ss}) = state {_string = replace s1 s2 s3 (Just 1) : ss}
|
instructionStringReplaceFirstString = instructionVectorReplaceVector string (Just 1)
|
||||||
instructionStringReplaceFirstString state = state
|
|
||||||
|
|
||||||
|
-- |Takes the first three strings from the top of the string stack. Replaces the number of instances based on the of the int stack of
|
||||||
|
-- the second string within the first string with the third string. Pushes the result to the string stack.
|
||||||
instructionStringReplaceNString :: State -> State
|
instructionStringReplaceNString :: State -> State
|
||||||
instructionStringReplaceNString state@(State {_string = s1 : s2 : s3 : ss, _int = i1 : is}) = state{_string = replace s1 s2 s3 (Just i1) : ss, _int = is}
|
instructionStringReplaceNString = instructionVectorReplaceVectorN string
|
||||||
instructionStringReplaceNString state = state
|
|
||||||
|
|
||||||
|
-- |Takes the first three strings from the top of the string stack. Replaces all instances of
|
||||||
|
-- the second string within the first string with the third string. Pushes the result to the string stack.
|
||||||
instructionStringReplaceAllString :: State -> State
|
instructionStringReplaceAllString :: State -> State
|
||||||
instructionStringReplaceAllString state@(State {_string = s1 : s2 : s3 : ss}) = state{_string = replace s1 s2 s3 Nothing : ss}
|
instructionStringReplaceAllString = instructionVectorReplaceVector string Nothing
|
||||||
instructionStringReplaceAllString state = state
|
|
||||||
|
|
||||||
|
-- |Takes the first two strings from the top of the string stack. Removes the first instance of
|
||||||
|
-- the second string. Pushes the result to the string stack.
|
||||||
instructionStringRemoveFirstString :: State -> State
|
instructionStringRemoveFirstString :: State -> State
|
||||||
instructionStringRemoveFirstString state@(State {_string = s1 : s2 : ss}) = state{_string = replace s1 s2 "" (Just 1) : ss}
|
instructionStringRemoveFirstString = instructionVectorRemoveVector string (Just 1)
|
||||||
instructionStringRemoveFirstString state = state
|
|
||||||
|
|
||||||
|
-- |Takes the first two strings from the top of the string stack. Removes N instances
|
||||||
|
-- based on the top int from the int stack of the second string. Pushes the result to the string stack.
|
||||||
instructionStringRemoveNString :: State -> State
|
instructionStringRemoveNString :: State -> State
|
||||||
instructionStringRemoveNString state@(State {_string = s1 : s2 : ss, _int = i1 : is}) = state{_string = replace s1 s2 "" (Just i1) : ss, _int = is}
|
instructionStringRemoveNString = instructionVectorRemoveVectorN string
|
||||||
instructionStringRemoveNString state = state
|
|
||||||
|
|
||||||
|
-- |Takes the first two strings from the top of the string stack. Removes all instances of
|
||||||
|
-- the second string. Pushes the result to the string stack.
|
||||||
instructionStringRemoveAllString :: State -> State
|
instructionStringRemoveAllString :: State -> State
|
||||||
instructionStringRemoveAllString state@(State {_string = s1 : s2 : ss}) = state{_string = replace s1 s2 "" Nothing : ss}
|
instructionStringRemoveAllString = instructionVectorRemoveVector string Nothing
|
||||||
instructionStringRemoveAllString state = state
|
|
||||||
|
|
||||||
|
-- |Counts the amount of occurrences of the second string in the first
|
||||||
|
-- string. Pushes the result to the string stack.
|
||||||
instructionStringOccurrencesOfString :: State -> State
|
instructionStringOccurrencesOfString :: State -> State
|
||||||
instructionStringOccurrencesOfString state@(State {_string = s1 : s2 : ss, _int = is}) = state{_string = ss, _int = amtOccurences s1 s2 : is}
|
instructionStringOccurrencesOfString = instructionVectorOccurrencesOfVector string
|
||||||
instructionStringOccurrencesOfString state = state
|
|
||||||
|
|
||||||
|
-- |Inserts the top char of the char stack into the top string of the string
|
||||||
|
-- stack based on an index from the top int of the int stack.
|
||||||
instructionStringInsertChar :: State -> State
|
instructionStringInsertChar :: State -> State
|
||||||
instructionStringInsertChar state@(State {_string = s1 : ss, _char = c1 : cs, _int = i1 : is}) = state{_string = combineTuple c1 (splitAt i1 s1) : ss, _char = cs, _int = is}
|
instructionStringInsertChar = instructionVectorInsert char string
|
||||||
instructionStringInsertChar state = state
|
|
||||||
|
|
||||||
|
-- |Pushes True to the bool stack if the top char on the char stack is within the
|
||||||
|
-- top string on the string stack. Pushes False otherwise.
|
||||||
instructionStringContainsChar :: State -> State
|
instructionStringContainsChar :: State -> State
|
||||||
instructionStringContainsChar = instructionVectorContains char string
|
instructionStringContainsChar = instructionVectorContains char string
|
||||||
|
|
||||||
|
-- |Pushes the first index found of the top char of the char stack within the
|
||||||
|
-- first string in the string stack to the int stack.
|
||||||
instructionStringIndexOfChar :: State -> State
|
instructionStringIndexOfChar :: State -> State
|
||||||
instructionStringIndexOfChar = instructionVectorIndexOf char string
|
instructionStringIndexOfChar = instructionVectorIndexOf char string
|
||||||
|
|
||||||
|
-- |@TODO
|
||||||
instructionStringSplitOnChar :: State -> State
|
instructionStringSplitOnChar :: State -> State
|
||||||
instructionStringSplitOnChar state@(State {_string = s1 : ss, _char = c1 : cs}) = state {_string = reverse $ splitOn [c1] s1 <> ss, _char = cs}
|
instructionStringSplitOnChar = instructionVectorSplitOn char string
|
||||||
instructionStringSplitOnChar state = state
|
|
||||||
|
|
||||||
instructionStringReplaceFirstChar :: State -> State
|
instructionStringReplaceFirstChar :: State -> State
|
||||||
instructionStringReplaceFirstChar = instructionVectorReplaceFirst char string
|
instructionStringReplaceFirstChar = instructionVectorReplace char string (Just 1)
|
||||||
|
|
||||||
instructionStringReplaceNChar :: State -> State
|
instructionStringReplaceNChar :: State -> State
|
||||||
instructionStringReplaceNChar state@(State {_string = s1 : ss, _char = c1 : c2 : cs, _int = i1 : is}) = state{_string = replace s1 [c1] [c2] (Just i1) : ss, _char = cs, _int = is}
|
instructionStringReplaceNChar state@(State {_string = s1 : ss, _char = c1 : c2 : cs, _int = i1 : is}) = state{_string = replace s1 [c1] [c2] (Just i1) : ss, _char = cs, _int = is}
|
||||||
instructionStringReplaceNChar state = state
|
instructionStringReplaceNChar state = state
|
||||||
|
|
||||||
instructionStringReplaceAllChar :: State -> State
|
instructionStringReplaceAllChar :: State -> State
|
||||||
instructionStringReplaceAllChar = instructionVectorReplace char string
|
instructionStringReplaceAllChar = instructionVectorReplace char string Nothing
|
||||||
|
|
||||||
instructionStringRemoveFirstChar :: State -> State
|
instructionStringRemoveFirstChar :: State -> State
|
||||||
instructionStringRemoveFirstChar state@(State {_string = s1 : ss, _char = c1 : cs}) = state {_string = replace s1 [c1] "" (Just 1) : ss, _char = cs}
|
instructionStringRemoveFirstChar state@(State {_string = s1 : ss, _char = c1 : cs}) = state {_string = replace s1 [c1] "" (Just 1) : ss, _char = cs}
|
||||||
|
@ -55,10 +55,10 @@ instructionVectorBoolSetNth :: State -> State
|
|||||||
instructionVectorBoolSetNth = instructionVectorSetNth bool vectorBool
|
instructionVectorBoolSetNth = instructionVectorSetNth bool vectorBool
|
||||||
|
|
||||||
instructionVectorBoolReplace :: State -> State
|
instructionVectorBoolReplace :: State -> State
|
||||||
instructionVectorBoolReplace = instructionVectorReplace bool vectorBool
|
instructionVectorBoolReplace = instructionVectorReplace bool vectorBool Nothing
|
||||||
|
|
||||||
instructionVectorBoolReplaceFirst :: State -> State
|
instructionVectorBoolReplaceFirst :: State -> State
|
||||||
instructionVectorBoolReplaceFirst = instructionVectorReplaceFirst bool vectorBool
|
instructionVectorBoolReplaceFirst = instructionVectorReplace bool vectorBool (Just 1)
|
||||||
|
|
||||||
instructionVectorBoolRemove :: State -> State
|
instructionVectorBoolRemove :: State -> State
|
||||||
instructionVectorBoolRemove = instructionVectorRemove bool vectorBool
|
instructionVectorBoolRemove = instructionVectorRemove bool vectorBool
|
||||||
|
@ -55,10 +55,10 @@ instructionVectorCharSetNth :: State -> State
|
|||||||
instructionVectorCharSetNth = instructionVectorSetNth char vectorChar
|
instructionVectorCharSetNth = instructionVectorSetNth char vectorChar
|
||||||
|
|
||||||
instructionVectorCharReplace :: State -> State
|
instructionVectorCharReplace :: State -> State
|
||||||
instructionVectorCharReplace = instructionVectorReplace char vectorChar
|
instructionVectorCharReplace = instructionVectorReplace char vectorChar Nothing
|
||||||
|
|
||||||
instructionVectorCharReplaceFirst :: State -> State
|
instructionVectorCharReplaceFirst :: State -> State
|
||||||
instructionVectorCharReplaceFirst = instructionVectorReplaceFirst char vectorChar
|
instructionVectorCharReplaceFirst = instructionVectorReplace char vectorChar (Just 1)
|
||||||
|
|
||||||
instructionVectorCharRemove :: State -> State
|
instructionVectorCharRemove :: State -> State
|
||||||
instructionVectorCharRemove = instructionVectorRemove char vectorChar
|
instructionVectorCharRemove = instructionVectorRemove char vectorChar
|
||||||
|
@ -55,10 +55,10 @@ instructionVectorFloatSetNth :: State -> State
|
|||||||
instructionVectorFloatSetNth = instructionVectorSetNth float vectorFloat
|
instructionVectorFloatSetNth = instructionVectorSetNth float vectorFloat
|
||||||
|
|
||||||
instructionVectorFloatReplace :: State -> State
|
instructionVectorFloatReplace :: State -> State
|
||||||
instructionVectorFloatReplace = instructionVectorReplace float vectorFloat
|
instructionVectorFloatReplace = instructionVectorReplace float vectorFloat Nothing
|
||||||
|
|
||||||
instructionVectorFloatReplaceFirst :: State -> State
|
instructionVectorFloatReplaceFirst :: State -> State
|
||||||
instructionVectorFloatReplaceFirst = instructionVectorReplaceFirst float vectorFloat
|
instructionVectorFloatReplaceFirst = instructionVectorReplace float vectorFloat (Just 1)
|
||||||
|
|
||||||
instructionVectorFloatRemove :: State -> State
|
instructionVectorFloatRemove :: State -> State
|
||||||
instructionVectorFloatRemove = instructionVectorRemove float vectorFloat
|
instructionVectorFloatRemove = instructionVectorRemove float vectorFloat
|
||||||
|
@ -55,10 +55,10 @@ instructionVectorIntSetNth :: State -> State
|
|||||||
instructionVectorIntSetNth = instructionVectorSetNth int vectorInt
|
instructionVectorIntSetNth = instructionVectorSetNth int vectorInt
|
||||||
|
|
||||||
instructionVectorIntReplace :: State -> State
|
instructionVectorIntReplace :: State -> State
|
||||||
instructionVectorIntReplace = instructionVectorReplace int vectorInt
|
instructionVectorIntReplace = instructionVectorReplace int vectorInt Nothing
|
||||||
|
|
||||||
instructionVectorIntReplaceFirst :: State -> State
|
instructionVectorIntReplaceFirst :: State -> State
|
||||||
instructionVectorIntReplaceFirst = instructionVectorReplaceFirst int vectorInt
|
instructionVectorIntReplaceFirst = instructionVectorReplace int vectorInt (Just 1)
|
||||||
|
|
||||||
instructionVectorIntRemove :: State -> State
|
instructionVectorIntRemove :: State -> State
|
||||||
instructionVectorIntRemove = instructionVectorRemove int vectorInt
|
instructionVectorIntRemove = instructionVectorRemove int vectorInt
|
||||||
|
@ -55,10 +55,10 @@ instructionVectorStringSetNth :: State -> State
|
|||||||
instructionVectorStringSetNth = instructionVectorSetNth string vectorString
|
instructionVectorStringSetNth = instructionVectorSetNth string vectorString
|
||||||
|
|
||||||
instructionVectorStringReplace :: State -> State
|
instructionVectorStringReplace :: State -> State
|
||||||
instructionVectorStringReplace = instructionVectorReplace string vectorString
|
instructionVectorStringReplace = instructionVectorReplace string vectorString Nothing
|
||||||
|
|
||||||
instructionVectorStringReplaceFirst :: State -> State
|
instructionVectorStringReplaceFirst :: State -> State
|
||||||
instructionVectorStringReplaceFirst = instructionVectorReplaceFirst string vectorString
|
instructionVectorStringReplaceFirst = instructionVectorReplace string vectorString (Just 1)
|
||||||
|
|
||||||
instructionVectorStringRemove :: State -> State
|
instructionVectorStringRemove :: State -> State
|
||||||
instructionVectorStringRemove = instructionVectorRemove string vectorString
|
instructionVectorStringRemove = instructionVectorRemove string vectorString
|
||||||
|
Loading…
x
Reference in New Issue
Block a user