Update to goprogram 0.1.0

This commit is contained in:
Tom Wiesing 2022-10-06 13:38:29 +02:00
parent d2d681a4f2
commit 7cda92b342
No known key found for this signature in database
31 changed files with 141 additions and 244 deletions

View file

@ -35,6 +35,16 @@ func (lazy *Lazy[T]) Get(init func() T) T {
return lazy.value
}
// Set atomically sets the value of this lazy, preventing future calls to get from invoking init.
// It may be called concurrently with calls to [Get] and [Reset].
func (lazy *Lazy[T]) Set(value T) {
lazy.m.Lock()
defer lazy.m.Unlock()
lazy.value = value
lazy.once.Do(func() {})
}
// Reset resets this Lazy, deleting any previously associated value.
//
// May be called concurrently with [Get].

View file

@ -1,102 +0,0 @@
package slicesx
import (
"golang.org/x/exp/constraints"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
)
// ForSorted iterates over the map in an ordered fashion
func ForSorted[K constraints.Ordered, V any](mp map[K]V, callback func(k K, v V)) {
keys := maps.Keys(mp)
slices.Sort(keys)
for _, key := range keys {
callback(key, mp[key])
}
}
// First returns the first value of type V
// When no such value exists, returns the zero value
func First[V any](values []V, test func(v V) bool) V {
for _, v := range values {
if test(v) {
return v
}
}
var v V
return v
}
// Any returns true if test returns true for any of values.
func Any[T any](values []T, test func(T) bool) bool {
for _, v := range values {
if test(v) {
return true
}
}
return false
}
// Filter filters values in place
func Filter[T any](values []T, filter func(T) bool) []T {
results := values[:0]
for _, value := range values {
if filter(value) {
results = append(results, value)
}
}
return results
}
// AsAny returns the same slice as values, but as any
func AsAny[T any](values []T) []any {
results := make([]any, len(values))
for i, v := range values {
results[i] = v
}
return results
}
// Partition partitions values in T by the given functions.
func Partition[T any, P comparable](values []T, partition func(value T) P) map[P][]T {
result := make(map[P][]T)
for _, v := range values {
part := partition(v)
result[part] = append(result[part], v)
}
return result
}
// FilterClone is like [Filter], but creates a new slice
func FilterClone[T any](values []T, filter func(T) bool) (results []T) {
for _, value := range values {
if filter(value) {
results = append(results, value)
}
}
return
}
// NonSequential sorts values, and then removes elements for which test() returns true.
// NonSequential does not re-allocate, but uses the existing slice.
func NonSequential[T constraints.Ordered](values []T, test func(prev, current T) bool) []T {
if len(values) < 2 {
return values
}
// sort the values and make a results array
slices.Sort(values)
results := values[:1]
// do the filter loop
prev := results[0]
for _, current := range values[1:] {
if !test(prev, current) {
results = append(results, current)
}
prev = current
}
return results
}

View file

@ -1,76 +0,0 @@
package smartp
import (
"io"
"github.com/FAU-CDI/wisski-distillery/pkg/logging"
"github.com/tkw1536/goprogram/status"
"github.com/tkw1536/goprogram/stream"
)
// Run runs f over all items with the given paralllelism.
// When parallel is 1, runs items sequentially with a full input / output stream.
func Run[T any](ios stream.IOStream, parallel int, f func(value T, stream stream.IOStream) error, items []T, opts ...Option[T]) error {
// create a group
var group status.Group[T, error]
group.HandlerLimit = parallel
// apply all the options
isParallel := parallel != 1
for _, opt := range opts {
group = opt(isParallel, group)
}
// setup the default prefix string
if group.PrefixString == nil {
group.PrefixString = status.DefaultPrefixString[T]
}
// if we are running sequentially
// then just iterate over the items
if !isParallel {
for index, item := range items {
err := logging.LogOperation(func() error {
return f(item, ios)
}, ios, "%v", group.PrefixString(item, index))
if err != nil {
return err
}
}
return nil
}
// if we are running in parallel, setup a handler
group.Handler = func(item T, index int, writer io.Writer) error {
ios := stream.NewIOStream(writer, writer, nil, 0)
return f(item, ios)
}
// create a new status display
st := status.NewWithCompat(ios.Stdout, 0)
st.Start()
defer st.Stop()
// and use it!
return status.UseErrorGroup(st, group, items)
}
// Option represents an option of a StatusGroup
type Option[T any] func(bool, status.Group[T, error]) status.Group[T, error]
// SmartMessage returns an option that sets the display of the provided item to the given handler
func SmartMessage[T any](handler func(value T) string) Option[T] {
return func(p bool, s status.Group[T, error]) status.Group[T, error] {
s.PrefixString = func(item T, index int) string {
message := handler(item)
if p {
return "[" + message + "]: "
}
return message
}
s.PrefixAlign = true
return s
}
}