nri-prelude-0.1.0.4: A Prelude inspired by the Elm programming language

Safe HaskellNone
LanguageHaskell2010

Text

Contents

Description

A built-in representation for efficient string manipulation. Text values are not lists of characters.

Synopsis

Text

type Text = Text Source #

A Text is a chunk of text:

"Hello!"
"How are you?"
"🙈🙉🙊"

-- strings with escape characters
"this\n\t\"that\""
"\x1F648\x1F649\x1F64A" -- "🙈🙉🙊"

A Text can represent any sequence of unicode characters. You can use the unicode escapes from x0000 to x10FFFF to represent characters by their code point. You can also include the unicode characters directly. Using the escapes can be better if you need one of the many whitespace characters with different widths.

isEmpty :: Text -> Bool Source #

Determine if a string is empty.

isEmpty "" == True
isEmpty "the world" == False

length :: Text -> Int Source #

Get the length of a string.

length "innumerable" == 11
length "" == 0

reverse :: Text -> Text Source #

Reverse a string.

reverse "stressed" == "desserts"

repeat :: Int -> Text -> Text Source #

Repeat a string n times.

repeat 3 "ha" == "hahaha"

replace :: Text -> Text -> Text -> Text Source #

Replace all occurrences of some substring.

replace "." "-" "Json.Decode.succeed" == "Json-Decode-succeed"
replace "," "/" "a,b,c,d,e"           == "a/b/c/d/e"

Building and Splitting

append :: Text -> Text -> Text Source #

Append two strings. You can also use the (++) operator to do this.

append "butter" "fly" == "butterfly"

concat :: List Text -> Text Source #

Concatenate many strings into one.

concat ["never","the","less"] == "nevertheless"

split :: Text -> Text -> List Text Source #

Split a string using a given separator.

split "," "cat,dog,cow"        == ["cat","dog","cow"]
split "/" "home/evan/Desktop/" == ["home","evan","Desktop", ""]

join :: Text -> List Text -> Text Source #

Put many strings together with a given separator.

join "a" ["H","w","ii","n"]        == "Hawaiian"
join " " ["cat","dog","cow"]       == "cat dog cow"
join "/" ["home","evan","Desktop"] == "home/evan/Desktop"

words :: Text -> List Text Source #

Break a string into words, splitting on chunks of whitespace.

words "How are \t you? \n Good?" == ["How","are","you?","Good?"]

lines :: Text -> List Text Source #

Break a string into lines, splitting on newlines.

lines "How are you?\nGood?" == ["How are you?", "Good?"]

Get Substrings

slice :: Int -> Int -> Text -> Text Source #

Take a substring given a start and end index. Negative indexes are taken starting from the end of the list.

slice  7  9 "snakes on a plane!" == "on"
slice  0  6 "snakes on a plane!" == "snakes"
slice  0 -7 "snakes on a plane!" == "snakes on a"
slice -6 -1 "snakes on a plane!" == "plane"

left :: Int -> Text -> Text Source #

Take n characters from the left side of a string.

left 2 "Mulder" == "Mu"

right :: Int -> Text -> Text Source #

Take n characters from the right side of a string.

right 2 "Scully" == "ly"

dropLeft :: Int -> Text -> Text Source #

Drop n characters from the left side of a string.

dropLeft 2 "The Lone Gunmen" == "e Lone Gunmen"

dropRight :: Int -> Text -> Text Source #

Drop n characters from the right side of a string.

dropRight 2 "Cigarette Smoking Man" == "Cigarette Smoking M"

Check for Substrings

contains :: Text -> Text -> Bool Source #

See if the second string contains the first one.

contains "the" "theory" == True
contains "hat" "theory" == False
contains "THE" "theory" == False

startsWith :: Text -> Text -> Bool Source #

See if the second string starts with the first one.

startsWith "the" "theory" == True
startsWith "ory" "theory" == False

endsWith :: Text -> Text -> Bool Source #

See if the second string ends with the first one.

endsWith "the" "theory" == False
endsWith "ory" "theory" == True

indexes :: Text -> Text -> List Int Source #

Get all of the indexes for a substring in another string.

indexes "i" "Mississippi"   == [1,4,7,10]
indexes "ss" "Mississippi"  == [2,5]
indexes "needle" "haystack" == []

indices :: Text -> Text -> List Int Source #

Alias for indexes.

Int Conversions

toInt :: Text -> Maybe Int Source #

Try to convert a string into an int, failing on improperly formatted strings.

Text.toInt "123" == Just 123
Text.toInt "-42" == Just -42
Text.toInt "3.1" == Nothing
Text.toInt "31a" == Nothing

If you are extracting a number from some raw user input, you will typically want to use @Maybe.withDefault@ to handle bad data:

Maybe.withDefault 0 (Text.toInt "42") == 42
Maybe.withDefault 0 (Text.toInt "ab") == 0

fromInt :: Int -> Text Source #

Convert an Int to a Text.

Text.fromInt 123 == "123"
Text.fromInt -42 == "-42"

Float Conversions

toFloat :: Text -> Maybe Float Source #

Try to convert a string into a float, failing on improperly formatted strings.

Text.toFloat "123" == Just 123.0
Text.toFloat "-42" == Just -42.0
Text.toFloat "3.1" == Just 3.1
Text.toFloat "31a" == Nothing

If you are extracting a number from some raw user input, you will typically want to use @Maybe.withDefault@ to handle bad data:

Maybe.withDefault 0 (Text.toFloat "42.5") == 42.5
Maybe.withDefault 0 (Text.toFloat "cats") == 0

fromFloat :: Float -> Text Source #

Convert a Float to a Text.

Text.fromFloat 123 == "123"
Text.fromFloat -42 == "-42"
Text.fromFloat 3.9 == "3.9"

Char Conversions

fromChar :: Char -> Text Source #

Create a Text from a given character.

fromChar 'a' == "a"

cons :: Char -> Text -> Text Source #

Add a character to the beginning of a Text.

cons 'T' "he truth is out there" == "The truth is out there"

uncons :: Text -> Maybe (Char, Text) Source #

Split a non-empty Text into its head and tail. This lets you pattern match on strings exactly as you would with lists.

uncons "abc" == Just ('a',"bc")
uncons ""    == Nothing

List Conversions

toList :: Text -> List Char Source #

Convert a Text to a list of characters.

toList "abc" == ['a','b','c']
toList "🙈🙉🙊" == ['🙈','🙉','🙊']

fromList :: List Char -> Text Source #

Convert a list of characters into a Text. Can be useful if you want to create a string primarily by consing, perhaps for decoding something.

fromList ['a','b','c'] == "abc"
fromList ['🙈','🙉','🙊'] == "🙈🙉🙊"

Formatting

Cosmetic operations such as padding with extra characters or trimming whitespace.

toUpper :: Text -> Text Source #

Convert a string to all upper case. Useful for case-insensitive comparisons and VIRTUAL YELLING.

toUpper "skinner" == "SKINNER"

toLower :: Text -> Text Source #

Convert a string to all lower case. Useful for case-insensitive comparisons.

toLower "X-FILES" == "x-files"

pad :: Int -> Char -> Text -> Text Source #

Pad a string on both sides until it has a given length.

pad 5 ' ' "1"   == "  1  "
pad 5 ' ' "11"  == "  11 "
pad 5 ' ' "121" == " 121 "

padLeft :: Int -> Char -> Text -> Text Source #

Pad a string on the left until it has a given length.

padLeft 5 '.' "1"   == "....1"
padLeft 5 '.' "11"  == "...11"
padLeft 5 '.' "121" == "..121"

padRight :: Int -> Char -> Text -> Text Source #

Pad a string on the right until it has a given length.

padRight 5 '.' "1"   == "1...."
padRight 5 '.' "11"  == "11..."
padRight 5 '.' "121" == "121.."

trim :: Text -> Text Source #

Get rid of whitespace on both sides of a string.

trim "  hats  \n" == "hats"

trimLeft :: Text -> Text Source #

Get rid of whitespace on the left of a string.

trimLeft "  hats  \n" == "hats  \n"

trimRight :: Text -> Text Source #

Get rid of whitespace on the right of a string.

trimRight "  hats  \n" == "  hats"

Higher-Order Functions

map :: (Char -> Char) -> Text -> Text Source #

Transform every character in a Text

map (\c -> if c == '/' then '.' else c) "a/b/c" == "a.b.c"

filter :: (Char -> Bool) -> Text -> Text Source #

Keep only the characters that pass the test.

filter isDigit "R2-D2" == "22"

foldl :: (Char -> b -> b) -> b -> Text -> b Source #

Reduce a Text from the left.

foldl cons "" "time" == "emit"

foldr :: (Char -> b -> b) -> b -> Text -> b Source #

Reduce a Text from the right.

foldr cons "" "time" == "time"

any :: (Char -> Bool) -> Text -> Bool Source #

Determine whether any characters pass the test.

any isDigit "90210" == True
any isDigit "R2-D2" == True
any isDigit "heart" == False

all :: (Char -> Bool) -> Text -> Bool Source #

Determine whether all characters pass the test.

all isDigit "90210" == True
all isDigit "R2-D2" == False
all isDigit "heart" == False