strex

package module
v0.0.0-...-7ee5d2b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 23, 2012 License: MIT Imports: 2 Imported by: 0

README

#strex

This is a small library that implements some of the functions from the Data.List package in Haskell that are only applicable to strings.

##Install

$ go get github.com/djhworld/strings_ext

##Rationale

Because I enjoy programming in Haskell and want some of the standard library features found Data.List to be in Go, plus I found it an enjoyable intellectual exercise too.

But why only for strings? Why not use empty interfaces and make your code "generic" so it works with any datatype?

Personally, I don't see the use of the empty interface to be best practise when you want to guarantee type safety at runtime. I'm a strong advocate for compile time type checking and I feel it would not be appropriate to use interface{} as a half-way house to generics.

As soon as the Go team implement generics/parametric polymorphism into the language I'll almost certainly drop this library and create a more "generic" solution akin to Data.List. For those looking to go down the interface{} route now, please see the Seq library by Bill Burdick

##Why no Map?

See strings.Map for the default implementation, although this version is NOT like Haskell's map in the sense that you can only input and output a string, no other type.

##Documentation http://go.pkgdoc.org/github.com/djhworld/strex

With thanks

To the people on reddit who helped me with this code. Originally, I implemented all the functions in a Haskell like style with plenty of recursion and other inefficiencies that don't work so well in a non tail-call optimised language. With the help from the users such as "rogpeppe" I was able to tighten up the package a lot with much faster code.

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

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

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

func Distinct(s string) string

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

func Drop(n int, s string) string

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

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

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

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

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

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.

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

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

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(s string) rune

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

func Init(s string) string

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

func IsEmpty(s string) bool

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

func Last(s string) rune

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

func Reverse(s string) string

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

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

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

func Tail(s string) string

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

func Take(n int, s string) string

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

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

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL