Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

basic integration with iterators #31

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 phelmkamp
Copyright (c) 2023 phelmkamp

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
17 changes: 17 additions & 0 deletions constraints/constraints.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Package constraints defines a set of useful constraints to be used with type parameters.
package constraints

// Float is a constraint that permits any floating-point type.
type Float interface {
~float32 | ~float64
}

// Integer is a constraint that permits any integer type.
type Integer interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

// Ordered is a constraint that permits any ordered type: any type that supports the operators < <= >= >.
type Ordered interface {
Integer | Float | ~string
}
2 changes: 1 addition & 1 deletion funcs/funcs.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 phelmkamp. All rights reserved.
// Copyright 2023 phelmkamp. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/phelmkamp/valor

go 1.19
go 1.20
2 changes: 1 addition & 1 deletion optional/doc.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 phelmkamp. All rights reserved.
// Copyright 2023 phelmkamp. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.

Expand Down
141 changes: 141 additions & 0 deletions optional/iter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package optional

import "github.com/phelmkamp/valor/constraints"

// Iter is an iterator over a collection of elements.
type Iter[T any] func(yield func(T) bool)

// First returns a Value of the first element.
// Returns a not-ok Value if there are no elements.
func First[T any](iter Iter[T]) Value[T] {
// for v := range iter {
// return OfOk(v)
// }
// return OfNotOk[T]()

var first T
var ok bool
iter(func(v T) bool {
first = v
ok = true
return false
})
return Of(first, ok)
}

// Last returns a Value of the last element.
// Returns a not-ok Value if there are no elements.
func Last[T any](iter Iter[T]) Value[T] {
var last T
var ok bool
iter(func(v T) bool {
last = v
ok = true
return true
})
return Of(last, ok)
}

// Min returns a Value of the minimum element.
// Returns a not-ok Value if there are no elements.
func Min[T constraints.Ordered](iter Iter[T]) Value[T] {
var min T
var ok bool
iter(func(v T) bool {
if !ok {
min = v
} else if v < min {
min = v
}
ok = true
return true
})
return Of(min, ok)
}

// MinFunc returns a Value of the minimum element.
// Returns a not-ok Value if there are no elements.
func MinFunc[T any](iter Iter[T], lt func(T, T) bool) Value[T] {
var min T
var ok bool
iter(func(v T) bool {
if !ok {
min = v
} else if lt(v, min) {
min = v
}
ok = true
return true
})
return Of(min, ok)
}

// Max returns a Value of the minimum element.
// Returns a not-ok Value if there are no elements.
func Max[T constraints.Ordered](iter Iter[T]) Value[T] {
var max T
var ok bool
iter(func(v T) bool {
if !ok {
max = v
} else if v > max {
max = v
}
ok = true
return true
})
return Of(max, ok)
}

// MaxFunc returns a Value of the maximum element.
// Returns a not-ok Value if there are no elements.
func MaxFunc[T any](iter Iter[T], gt func(T, T) bool) Value[T] {
var max T
var ok bool
iter(func(v T) bool {
if !ok {
max = v
} else if gt(v, max) {
max = v
}
ok = true
return true
})
return Of(max, ok)
}

// Reduce returns a Value of the result of accumulating all elements.
// Returns a not-ok Value if there are no elements.
func Reduce[T any](iter Iter[T], accum func(res T, v T) T) Value[T] {
var res T
var ok bool
iter(func(v T) bool {
if !ok {
res = v
} else {
res = accum(res, v)
}
ok = true
return true
})
return Of(res, ok)
}

// Sum returns a Value of the sum of all elements.
// Returns a not-ok Value if there are no elements.
func Sum[T constraints.Integer | constraints.Float](iter Iter[T]) Value[T] {
return Reduce(iter, func(v, v2 T) T {
return v + v2
})
}

// Avg returns a Value of the average of all elements.
// Returns a not-ok Value if there are no elements.
func Avg[T constraints.Integer | constraints.Float](iter Iter[T]) Value[float64] {
n := float64(1)
sum := Reduce(iter, func(v, v2 T) T {
n++
return v + v2
})
return Map(sum, func(v T) float64 { return float64(v) / n })
}
137 changes: 137 additions & 0 deletions optional/iter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package optional_test

import (
"testing"

"github.com/phelmkamp/valor/optional"
)

func all[T any](s []T) func(yield func(T) bool) {
return func(yield func(T) bool) {
for _, v := range s {
if !yield(v) {
return
}
}
}
}

func TestFirst(t *testing.T) {
if got := optional.First(all([]string{})); got.IsOk() {
t.Errorf("First() of empty slice = %v, want %v", got, optional.OfNotOk[string]())
}
var res string
if got := optional.First(all([]string{"a", "b", "c"})); !got.Ok(&res) || res != "a" {
t.Errorf("First() = %v, want %v", got, optional.OfOk("a"))
}
}

func TestLast(t *testing.T) {
if got := optional.Last(all([]string{})); got.IsOk() {
t.Errorf("Last() of empty slice = %v, want %v", got, optional.OfNotOk[string]())
}
var res string
if got := optional.Last(all([]string{"a", "b", "c"})); !got.Ok(&res) || res != "c" {
t.Errorf("Last() = %v, want %v", got, optional.OfOk("c"))
}
}

func TestMin(t *testing.T) {
if got := optional.Min(all([]int{})); got.IsOk() {
t.Errorf("Min() of empty slice = %v, want %v", got, optional.OfNotOk[int]())
}
var res int
if got := optional.Min(all([]int{2})); !got.Ok(&res) || res != 2 {
t.Errorf("Min() of 1 element = %v, want %v", got, optional.OfOk(2))
}
if got := optional.Min(all([]int{3, 1, 2})); !got.Ok(&res) || res != 1 {
t.Errorf("Min() = %v, want %v", got, optional.OfOk(1))
}
}

func TestMinFunc(t *testing.T) {
shorter := func(s1, s2 string) bool {
return len(s1) < len(s2)
}
if got := optional.MinFunc(all([]string{}), shorter); got.IsOk() {
t.Errorf("MinFunc() of empty slice = %v, want %v", got, optional.OfNotOk[string]())
}
var res string
if got := optional.MinFunc(all([]string{"ab"}), shorter); !got.Ok(&res) || res != "ab" {
t.Errorf("MinFunc() of 1 element = %v, want %v", got, optional.OfOk("ab"))
}
if got := optional.MinFunc(all([]string{"ab", "abc", "a"}), shorter); !got.Ok(&res) || res != "a" {
t.Errorf("MinFunc() = %v, want %v", got, optional.OfOk("a"))
}
}

func TestMax(t *testing.T) {
if got := optional.Max(all([]int{})); got.IsOk() {
t.Errorf("Max() of empty slice = %v, want %v", got, optional.OfNotOk[int]())
}
var res int
if got := optional.Max(all([]int{2})); !got.Ok(&res) || res != 2 {
t.Errorf("Max() of 1 element = %v, want %v", got, optional.OfOk(2))
}
if got := optional.Max(all([]int{1, 3, 2})); !got.Ok(&res) || res != 3 {
t.Errorf("Max() = %v, want %v", got, optional.OfOk(3))
}
}

func TestMaxFunc(t *testing.T) {
longer := func(s1, s2 string) bool {
return len(s1) > len(s2)
}
if got := optional.MaxFunc(all([]string{}), longer); got.IsOk() {
t.Errorf("MaxFunc() of empty slice = %v, want %v", got, optional.OfNotOk[string]())
}
var res string
if got := optional.MaxFunc(all([]string{"ab"}), longer); !got.Ok(&res) || res != "ab" {
t.Errorf("MaxFunc() of 1 element = %v, want %v", got, optional.OfOk("ab"))
}
if got := optional.MaxFunc(all([]string{"ab", "abc", "a"}), longer); !got.Ok(&res) || res != "abc" {
t.Errorf("MaxFunc() = %v, want %v", got, optional.OfOk("abc"))
}
}

func TestReduce(t *testing.T) {
joiner := func(s1, s2 string) string {
return s1 + s2
}
if got := optional.Reduce(all([]string{}), joiner); got.IsOk() {
t.Errorf("Reduce() of empty slice = %v, want %v", got, optional.OfNotOk[string]())
}
var res string
if got := optional.Reduce(all([]string{"a"}), joiner); !got.Ok(&res) || res != "a" {
t.Errorf("Reduce() of 1 element = %v, want %v", got, optional.OfOk("a"))
}
if got := optional.Reduce(all([]string{"a", "b", "c"}), joiner); !got.Ok(&res) || res != "abc" {
t.Errorf("Reduce() = %v, want %v", got, optional.OfOk("abc"))
}
}

func TestSum(t *testing.T) {
if got := optional.Sum(all([]int{})); got.IsOk() {
t.Errorf("Sum() of empty slice = %v, want %v", got, optional.OfNotOk[int]())
}
var res int
if got := optional.Sum(all([]int{2})); !got.Ok(&res) || res != 2 {
t.Errorf("Sum() of 1 element = %v, want %v", got, optional.OfOk(2))
}
if got := optional.Sum(all([]int{1, 2, 3})); !got.Ok(&res) || res != 6 {
t.Errorf("Sum() = %v, want %v", got, optional.OfOk(6))
}
}

func TestAvg(t *testing.T) {
if got := optional.Avg(all([]int{})); got.IsOk() {
t.Errorf("Avg() of empty slice = %v, want %v", got, optional.OfNotOk[int]())
}
var res float64
if got := optional.Avg(all([]int{2})); !got.Ok(&res) || res != 2 {
t.Errorf("Avg() of 1 element = %v, want %v", got, optional.OfOk(2))
}
if got := optional.Avg(all([]int{1, 2, 3, 4})); !got.Ok(&res) || res != 2.5 {
t.Errorf("Avg() = %v, want %v", got, optional.OfOk(2.5))
}
}
7 changes: 6 additions & 1 deletion optional/value.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 phelmkamp. All rights reserved.
// Copyright 2023 phelmkamp. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -221,3 +221,8 @@ func Flatten[T any](val Value[Value[T]]) Value[T] {
}
return val.v
}

// Equal reports whether val and val2 contain the same value.
func Equal[T comparable](val, val2 Value[T]) bool {
return val.v == val2.v && val.ok == val2.ok
}
2 changes: 1 addition & 1 deletion optional/value_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 phelmkamp. All rights reserved.
// Copyright 2023 phelmkamp. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.

Expand Down
2 changes: 1 addition & 1 deletion result/doc.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 phelmkamp. All rights reserved.
// Copyright 2023 phelmkamp. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.

Expand Down
4 changes: 3 additions & 1 deletion result/result.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 phelmkamp. All rights reserved.
// Copyright 2023 phelmkamp. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.

Expand All @@ -25,7 +25,9 @@ type Result[T any] struct {
// Empty is an empty value.
// It's an alias for struct{} and is equivalent to struct{} in all ways.
// This is useful when a Result can only contain an error or nothing:
//
// result.OfError[result.Empty](err)
//
// Deprecated: use unit.Type instead.
type Empty = struct{}

Expand Down
2 changes: 1 addition & 1 deletion result/result_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 phelmkamp. All rights reserved.
// Copyright 2023 phelmkamp. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.

Expand Down
2 changes: 1 addition & 1 deletion tuple/four/four_tuple.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 phelmkamp. All rights reserved.
// Copyright 2023 phelmkamp. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.

Expand Down
2 changes: 1 addition & 1 deletion tuple/four/four_tuple_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 phelmkamp. All rights reserved.
// Copyright 2023 phelmkamp. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.

Expand Down
Loading