strex: github.com/djhworld/strex Index | Examples | Files

package strex

import "github.com/djhworld/strex"

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

Examples

Package Files

strex.go

func All Uses

func All(p func(rune) bool, s string) bool

All applied to a predicate p and a string s, determines if all elements of s satisfy p

Code:

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 Uses

func Distinct(s string) string

Distinct removes duplicate elements from a string. In particular, it keeps only the first occurrence of each element.

Code:

//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 Uses

func Drop(n int, s string) string

Drop returns the suffix of s after the first n runes, or "" if n > len([]rune(s))

Code:

// Haskell type signature (polymorphic): -
//     drop :: Int -> [a] -> [a]

var str string = "golang"
fmt.Println(Drop(2, str))

Output:

lang

func DropWhile Uses

func DropWhile(p func(rune) bool, s string) string

DropWhile returns the suffix remaining after TakeWhile

Code:

// 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 Uses

func Filter(p func(rune) bool, s string) string

Filter, applied to a predicate and a string, returns a string of characters (runes) that satisfy the predicate

Code:

//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 Uses

func Group(s string) []string

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.

Code:

//Haskell type signature (polymorphic): -
//    group :: Eq a => [a] -> [[a]]

var input string = "aaabbbccd"
fmt.Println(Group(input))

Output:

[aaa bbb cc d]

func GroupBy Uses

func GroupBy(p func(rune, rune) bool, s string) []string

GroupBy is the non-overloaded version of Group.

Code:

//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]
func Head(s string) rune

Head returns the first rune of s which must be non-empty

Code:

//Haskell type signature (polymorphic): -
//    head :: [a] -> a

var str string = "golang"
fmt.Println(string(Head(str))) //Head returns a rune

Output:

g

func Init Uses

func Init(s string) string

Init returns all the elements of s except the last one. The string must be non-empty.

Code:

//Haskell type signature (polymorphic): -
//    init :: [a] -> [a]

var str string = "google"
fmt.Println(string(Init(str)))

Output:

googl

func IsEmpty Uses

func IsEmpty(s string) bool

IsEmpty tests whether the string s is empty

Code:

//Haskell type signature (polymorphic): -
//    null :: [a] -> Bool

var input string = ""
fmt.Println(IsEmpty(input))

Output:

true

func Last Uses

func Last(s string) rune

Last returns the last rune in a string s, which must be non-empty.

Code:

//Haskell type signature (polymorphic): -
//    last :: [a] -> a

var str string = "google"
fmt.Println(string(Last(str))) //Last returns a rune

Output:

e

func Reverse Uses

func Reverse(s string) string

Reverse returns the string s in reverse order

Code:

//Haskell type signature (polymorphic): -
//    reverse :: [a] -> [a]

var str string = "golang"
fmt.Println(Reverse(str))

Output:

gnalog

func Span Uses

func Span(p func(rune) bool, s string) (string, string)

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

Code:

//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 Uses

func Tail(s string) string

Tail returns the the remainder of s minus the first rune of s, which must be non-empty

Code:

// Haskell type signature (polymorphic): -
//    tail :: [a] -> [a]

var str string = "golang"
fmt.Println(Tail(str))

Output:

olang

func Take Uses

func Take(n int, s string) string

Take returns the n rune prefix of s or s itself if n > len([]rune(s))

Code:

//Haskell type signature (polymorphic): -
//    take :: Int -> [a] -> [a]

var str string = "golang"
fmt.Println(Take(2, str))

Output:

go

func TakeWhile Uses

func TakeWhile(p func(rune) bool, s string) string

TakeWhile, applied to a predicate p and a string s, returns the longest prefix (possibly empty) of s of elements that satisfy p

Code:

//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

Package strex imports 2 packages (graph). Updated 2016-07-26. Refresh now. Tools for package owners.