1
0
mirror of https://github.com/IBM/fp-go.git synced 2025-11-25 22:21:49 +02:00

initial checkin

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
This commit is contained in:
Dr. Carsten Leue
2023-07-07 22:31:06 +02:00
parent 71c47ca560
commit c07df5c771
128 changed files with 5827 additions and 2 deletions

8
string/monoid.go Normal file
View File

@@ -0,0 +1,8 @@
package string
import (
M "github.com/ibm/fp-go/monoid"
)
// Monoid is the monoid implementing string concatenation
var Monoid = M.MakeMonoid(concat, "")

11
string/monoid_test.go Normal file
View File

@@ -0,0 +1,11 @@
package string
import (
"testing"
M "github.com/ibm/fp-go/monoid/testing"
)
func TestMonoid(t *testing.T) {
M.AssertLaws(t, Monoid)([]string{"", "a", "some value"})
}

15
string/semigroup.go Normal file
View File

@@ -0,0 +1,15 @@
package string
import (
"fmt"
S "github.com/ibm/fp-go/semigroup"
)
func concat(left string, right string) string {
return fmt.Sprintf("%s%s", left, right)
}
func Semigroup() S.Semigroup[string] {
return S.MakeSemigroup(concat)
}

49
string/string.go Normal file
View File

@@ -0,0 +1,49 @@
package string
import (
"strings"
F "github.com/ibm/fp-go/function"
O "github.com/ibm/fp-go/ord"
)
var (
// ToUpperCase converts the string to uppercase
ToUpperCase = strings.ToUpper
// ToLowerCase converts the string to lowercase
ToLowerCase = strings.ToLower
// Ord implements the default ordering for strings
Ord = O.FromStrictCompare[string]()
)
func Eq(left string, right string) bool {
return left == right
}
func ToBytes(s string) []byte {
return []byte(s)
}
func IsEmpty(s string) bool {
return len(s) == 0
}
func IsNonEmpty(s string) bool {
return len(s) > 0
}
func Size(s string) int {
return len(s)
}
// Includes returns a predicate that tests for the existence of the search string
func Includes(searchString string) func(string) bool {
return F.Bind2nd(strings.Contains, searchString)
}
// Equals returns a predicate that tests if a string is equal
func Equals(other string) func(string) bool {
return F.Bind2nd(Eq, other)
}

12
string/string_test.go Normal file
View File

@@ -0,0 +1,12 @@
package string
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestEmpty(t *testing.T) {
assert.True(t, IsEmpty(""))
assert.False(t, IsEmpty("Carsten"))
}