Documentation ¶
Overview ¶
Package that adds a few extra features to the strings package.
These functions are very high level and take inspiration from the Data.List package found in the Haskell programming language
With thanks to reddit users `rogpeppe`, `DavidScone`, `DisposaBoy` who helped me in making this library better and quicker
Daniel Harper (djhworld) 2012
Index ¶
- func All(p func(rune) bool, s string) bool
- func Distinct(s string) string
- func Drop(n int, s string) string
- func DropWhile(p func(rune) bool, s string) string
- func Filter(p func(rune) bool, s string) string
- func Group(s string) []string
- func GroupBy(p func(rune, rune) bool, s string) []string
- func Head(s string) rune
- func Init(s string) string
- func IsEmpty(s string) bool
- func Last(s string) rune
- func Reverse(s string) string
- func Span(p func(rune) bool, s string) (string, string)
- func Tail(s string) string
- func Take(n int, s string) string
- func TakeWhile(p func(rune) bool, s string) string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func All ¶
All applied to a predicate p and a string s, determines if all elements of s satisfy p
Example ¶
var isLowercase func(rune) bool = func(r rune) bool { return r >= 97 && r <= 122 } var input string = "Google" fmt.Println(All(isLowercase, input))
Output: false
func Distinct ¶
Distinct removes duplicate elements from a string. In particular, it keeps only the first occurrence of each element.
Example ¶
//Haskell type signature (polymorphic) - Haskell calls this function 'nub': - // nub :: Eq a => [a] -> [a] var str string = "aaabbbcccdddeeefff" fmt.Println(Distinct(str))
Output: abcdef
func Drop ¶
Drop returns the suffix of s after the first n runes, or "" if n > len([]rune(s))
Example ¶
// Haskell type signature (polymorphic): - // drop :: Int -> [a] -> [a] var str string = "golang" fmt.Println(Drop(2, str))
Output: lang
func DropWhile ¶
DropWhile returns the suffix remaining after TakeWhile
Example ¶
// Haskell type signature (polymorphic): - // dropWhile :: (a -> Bool) -> [a] -> [a] var input string = " Hello World" fmt.Println(DropWhile(func(a rune) bool { return a == ' ' }, input))
Output: Hello World
func Filter ¶
Filter, applied to a predicate and a string, returns a string of characters (runes) that satisfy the predicate
Example ¶
//Haskell type signature (polymorphic): - // filter :: (a -> Bool) -> [a] -> [a] var isNotPunctuation func(rune) bool = func(a rune) bool { return !strings.ContainsRune("!.,?:;-'\"", a) } fmt.Println(Filter(isNotPunctuation, "he said \"hello there!\"")) //strips all punctuation
Output: he said hello there
func Group ¶
Group takes a string and returns a slice of strings such that the concatenation of the result is equal to the argument. Moreover, each sublist in the result contains only equal elements.
Example ¶
//Haskell type signature (polymorphic): - // group :: Eq a => [a] -> [[a]] var input string = "aaabbbccd" fmt.Println(Group(input))
Output: [aaa bbb cc d]
func GroupBy ¶
GroupBy is the non-overloaded version of Group.
Example ¶
//Haskell type signature (polymorphic): - // groupBy :: (a -> a -> Bool) -> [a] -> [[a]] var isDigit func(rune) bool = func(a rune) bool { return strings.ContainsRune("0123456789", a) } var input string = "02/08/2010" fmt.Println(GroupBy(func(a, b rune) bool { return (isDigit(a)) == (isDigit(b)) }, input)) //Ouput: [02 / 08 / 2010]
Output:
func Head ¶
Head returns the first rune of s which must be non-empty
Example ¶
//Haskell type signature (polymorphic): - // head :: [a] -> a var str string = "golang" fmt.Println(string(Head(str))) //Head returns a rune
Output: g
func Init ¶
Init returns all the elements of s except the last one. The string must be non-empty.
Example ¶
//Haskell type signature (polymorphic): - // init :: [a] -> [a] var str string = "google" fmt.Println(string(Init(str)))
Output: googl
func IsEmpty ¶
IsEmpty tests whether the string s is empty
Example ¶
//Haskell type signature (polymorphic): - // null :: [a] -> Bool var input string = "" fmt.Println(IsEmpty(input))
Output: true
func Last ¶
Last returns the last rune in a string s, which must be non-empty.
Example ¶
//Haskell type signature (polymorphic): - // last :: [a] -> a var str string = "google" fmt.Println(string(Last(str))) //Last returns a rune
Output: e
func Reverse ¶
Reverse returns the string s in reverse order
Example ¶
//Haskell type signature (polymorphic): - // reverse :: [a] -> [a] var str string = "golang" fmt.Println(Reverse(str))
Output: gnalog
func Span ¶
Span, applied to a predicate p and a string s, returns two strings where the first string is longest prefix (possibly empty) of s of characters (runes) that satisfy p and the second string is the remainder of the string
Example ¶
//Haskell type signature (polymorphic): - // span :: (a -> Bool) -> [a] -> ([a], [a]) var input string = "hello world" fmt.Println(Span(func(a rune) bool { return a != ' ' }, input))
Output: hello world
func Tail ¶
Tail returns the the remainder of s minus the first rune of s, which must be non-empty
Example ¶
// Haskell type signature (polymorphic): - // tail :: [a] -> [a] var str string = "golang" fmt.Println(Tail(str))
Output: olang
func Take ¶
Take returns the n rune prefix of s or s itself if n > len([]rune(s))
Example ¶
//Haskell type signature (polymorphic): - // take :: Int -> [a] -> [a] var str string = "golang" fmt.Println(Take(2, str))
Output: go
func TakeWhile ¶
TakeWhile, applied to a predicate p and a string s, returns the longest prefix (possibly empty) of s of elements that satisfy p
Example ¶
//Haskell type signature (polymorphic): - // takeWhile :: (a -> Bool) -> [a] -> [a] var input string = "aaaaAbbbbbccccc" fmt.Println(TakeWhile(func(a rune) bool { return a == 'a' }, input))
Output: aaaa
Types ¶
This section is empty.