├── go.mod ├── LICENSE └── fungi.go /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/phillip-england/fungi 2 | 3 | go 1.23.3 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Phillip England 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /fungi.go: -------------------------------------------------------------------------------- 1 | package fungi 2 | 3 | // Process executes a series of functions and stops at the first error encountered. 4 | // Returns nil if all functions succeed. 5 | func Process(funcs ...func() error) error { 6 | for _, fn := range funcs { 7 | if err := fn(); err != nil { 8 | return err // Return the first error encountered 9 | } 10 | } 11 | return nil // No errors found, return nil 12 | } 13 | 14 | // Iter iterates over a slice, calling a callback function for each element. 15 | // Stops and returns the first error encountered, or nil if all succeed. 16 | func Iter[T any](someSlice []T, someCallback func(i int, item T) error) error { 17 | for i, item := range someSlice { 18 | err := someCallback(i, item) 19 | if err != nil { 20 | return err 21 | } 22 | } 23 | return nil 24 | } 25 | 26 | // Map transforms a slice into a new slice by applying a transformation function 27 | // to each element, passing its index and value. 28 | func Map[T any, U any](someSlice []T, transform func(i int, item T) U) []U { 29 | result := make([]U, len(someSlice)) 30 | for i, item := range someSlice { 31 | result[i] = transform(i, item) 32 | } 33 | return result 34 | } 35 | 36 | // Filter returns a new slice containing only the elements of the original slice 37 | // that satisfy the predicate function, which receives the index and value. 38 | func Filter[T any](someSlice []T, predicate func(i int, item T) bool) []T { 39 | var result []T 40 | for i, item := range someSlice { 41 | if predicate(i, item) { 42 | result = append(result, item) 43 | } 44 | } 45 | return result 46 | } 47 | 48 | // Reduce aggregates a slice into a single value by applying a reducer function 49 | // to each element along with an accumulator, starting with the initial value. 50 | func Reduce[T any, U any](someSlice []T, initial U, reducer func(i int, acc U, item T) U) U { 51 | acc := initial 52 | for i, item := range someSlice { 53 | acc = reducer(i, acc, item) 54 | } 55 | return acc 56 | } 57 | 58 | // Find searches a slice for the first element that satisfies the predicate function, 59 | // returning the element and true if found, or the zero value and false if not. 60 | func Find[T any](someSlice []T, predicate func(i int, item T) bool) (T, bool) { 61 | var zero T 62 | for i, item := range someSlice { 63 | if predicate(i, item) { 64 | return item, true 65 | } 66 | } 67 | return zero, false 68 | } 69 | 70 | // Every checks whether all elements in a slice satisfy the predicate function. 71 | // Returns true if all elements satisfy the predicate, or false otherwise. 72 | func Every[T any](someSlice []T, predicate func(i int, item T) bool) bool { 73 | for i, item := range someSlice { 74 | if !predicate(i, item) { 75 | return false 76 | } 77 | } 78 | return true 79 | } 80 | 81 | // Some checks whether at least one element in a slice satisfies the predicate function. 82 | // Returns true if any element satisfies the predicate, or false otherwise. 83 | func Some[T any](someSlice []T, predicate func(i int, item T) bool) bool { 84 | for i, item := range someSlice { 85 | if predicate(i, item) { 86 | return true 87 | } 88 | } 89 | return false 90 | } 91 | --------------------------------------------------------------------------------