A couple of library functions

This commit is contained in:
Edwin Brady 2020-03-19 22:14:48 +00:00
parent 727d6e8ec5
commit d666ed50c9
2 changed files with 32 additions and 0 deletions

View File

@ -437,6 +437,26 @@ export
isInfixOf : Eq a => List a -> List a -> Bool isInfixOf : Eq a => List a -> List a -> Bool
isInfixOf n h = any (isPrefixOf n) (tails h) isInfixOf n h = any (isPrefixOf n) (tails h)
||| Transposes rows and columns of a list of lists.
|||
||| ```idris example
||| with List transpose [[1, 2], [3, 4]]
||| ```
|||
||| This also works for non square scenarios, thus
||| involution does not always hold:
|||
||| transpose [[], [1, 2]] = [[1], [2]]
||| transpose (transpose [[], [1, 2]]) = [[1, 2]]
export
transpose : List (List a) -> List (List a)
transpose [] = []
transpose (heads :: tails) = spreadHeads heads (transpose tails) where
spreadHeads : List a -> List (List a) -> List (List a)
spreadHeads [] tails = tails
spreadHeads (head :: heads) [] = [head] :: spreadHeads heads []
spreadHeads (head :: heads) (tail :: tails) = (head :: tail) :: spreadHeads heads tails
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- Properties -- Properties
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------

View File

@ -173,3 +173,15 @@ strIndex = prim__strIndex
export export
strTail : String -> String strTail : String -> String
strTail = prim__strTail strTail = prim__strTail
export
isPrefixOf : String -> String -> Bool
isPrefixOf a b = isPrefixOf (unpack a) (unpack b)
export
isSuffixOf : String -> String -> Bool
isSuffixOf a b = isSuffixOf (unpack a) (unpack b)
export
isInfixOf : String -> String -> Bool
isInfixOf a b = isInfixOf (unpack a) (unpack b)