v2 · Utilities
String Type Classes
The string package provides type class instances and utilities for string values, enabling functional operations on text.
Overview
The String package provides type class instances for the string type, enabling:
- Eq: Equality comparison
- Ord: Lexicographic ordering
- Semigroup: Concatenation
- Monoid: Concatenation with empty string identity
These instances allow strings to be used with generic functional operations.
Equality
string_eq.go
import S "github.com/IBM/fp-go/string"
// Compare strings for equality
S.Eq.Equals("hello", "hello") // true
S.Eq.Equals("hello", "world") // false
S.Eq.Equals("", "") // true
// Case-sensitive comparison
S.Eq.Equals("Hello", "hello") // falseOrdering
Lexicographic (dictionary) ordering:
string_ord.go
import S "github.com/IBM/fp-go/string"
// Compare strings lexicographically
S.Ord.Compare("a", "b") // -1 (less than)
S.Ord.Compare("b", "a") // 1 (greater than)
S.Ord.Compare("a", "a") // 0 (equal)
S.Ord.Compare("apple", "banana") // -1
// Derived operations
S.Ord.LessThan("a", "b") // true
S.Ord.GreaterThan("z", "a") // true
S.Ord.LessThanOrEqual("a", "a") // true
// Min and Max
S.Ord.Min("zebra", "apple") // "apple"
S.Ord.Max("zebra", "apple") // "zebra"Semigroup Concatenation
Combine strings with concatenation:
string_semigroup.go
import S "github.com/IBM/fp-go/string"
// Concatenate strings
S.Semigroup.Concat("Hello", " World") // "Hello World"
S.Semigroup.Concat("foo", "bar") // "foobar"
// Associativity holds
s1 := S.Semigroup.Concat(
S.Semigroup.Concat("a", "b"),
"c",
)
s2 := S.Semigroup.Concat(
"a",
S.Semigroup.Concat("b", "c"),
)
// s1 == s2 == "abc"Monoid Operations
Concatenation with empty string identity:
string_monoid.go
import S "github.com/IBM/fp-go/string"
// Concatenate with monoid
S.Monoid.Concat("Hello", " World") // "Hello World"
S.Monoid.Empty() // ""
// Identity laws
text := "test"
S.Monoid.Concat(S.Monoid.Empty(), text) // "test"
S.Monoid.Concat(text, S.Monoid.Empty()) // "test"Sorting Strings
Sort string arrays alphabetically:
string_sort.go
import (
A "github.com/IBM/fp-go/array"
F "github.com/IBM/fp-go/function"
S "github.com/IBM/fp-go/string"
)
words := []string{"zebra", "apple", "mango", "banana"}
// Sort alphabetically (ascending)
sorted := F.Pipe2(
words,
A.Sort(S.Ord),
)
// []string{"apple", "banana", "mango", "zebra"}
// Sort descending
import O "github.com/IBM/fp-go/ord"
sortedDesc := F.Pipe2(
words,
A.Sort(O.Reverse(S.Ord)),
)
// []string{"zebra", "mango", "banana", "apple"}Concatenating Arrays
Join string arrays:
string_concat.go
import (
A "github.com/IBM/fp-go/array"
F "github.com/IBM/fp-go/function"
S "github.com/IBM/fp-go/string"
)
words := []string{"Hello", "functional", "world"}
// Concatenate all strings
combined := F.Pipe2(
words,
A.Fold(S.Monoid),
)
// "Hellofunctionalworld"
// Join with separator
joined := F.Pipe3(
words,
A.Intersperse(" "),
A.Fold(S.Monoid),
)
// "Hello functional world"
// Join with custom separator
withCommas := F.Pipe3(
words,
A.Intersperse(", "),
A.Fold(S.Monoid),
)
// "Hello, functional, world"Building Strings
Use monoid to build strings from parts:
string_build.go
type User struct {
FirstName string
LastName string
Age int
}
user := User{FirstName: "Alice", LastName: "Smith", Age: 30}
// Build formatted string
parts := []string{
"User: ",
user.FirstName,
" ",
user.LastName,
" (age ",
fmt.Sprintf("%d", user.Age),
")",
}
result := F.Pipe2(
parts,
A.Fold(S.Monoid),
)
// "User: Alice Smith (age 30)"
// Build CSV row
csvRow := F.Pipe3(
[]string{user.FirstName, user.LastName, fmt.Sprintf("%d", user.Age)},
A.Intersperse(","),
A.Fold(S.Monoid),
)
// "Alice,Smith,30"Filtering and Mapping
Combine with array operations:
string_filter.go
import (
A "github.com/IBM/fp-go/array"
F "github.com/IBM/fp-go/function"
S "github.com/IBM/fp-go/string"
)
words := []string{"apple", "banana", "apricot", "cherry", "avocado"}
// Filter words starting with 'a'
aWords := F.Pipe2(
words,
A.Filter(func(s string) bool {
return len(s) > 0 && s[0] == 'a'
}),
)
// []string{"apple", "apricot", "avocado"}
// Sort and join
result := F.Pipe3(
aWords,
A.Sort(S.Ord),
A.Intersperse(", "),
A.Fold(S.Monoid),
)
// "apple, apricot, avocado"API Reference
| Instance | Type | Description |
|---|---|---|
Eq | Eq[string] | Equality comparison |
Ord | Ord[string] | Lexicographic ordering |
Semigroup | Semigroup[string] | Concatenation |
Monoid | Monoid[string] | Concatenation with empty identity |
Monoid Identity:
Monoid.Empty()returns""(empty string)
Related Concepts
Common Use Cases:
- Sorting string arrays
- Joining strings with separators
- Building formatted strings
- String comparison and filtering
See Also: