Skip to content

Commit

Permalink
Renamed optional package to opt and pointer package to ptr; r…
Browse files Browse the repository at this point in the history
…enamed methods in both packages for clarity.
  • Loading branch information
jpfourny committed Jan 31, 2024
1 parent 89871f2 commit 866a2ba
Show file tree
Hide file tree
Showing 32 changed files with 436 additions and 438 deletions.
16 changes: 8 additions & 8 deletions internal/kvstore/kvstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package kvstore

import (
"github.com/jpfourny/papaya/pkg/cmp"
"github.com/jpfourny/papaya/pkg/optional"
"github.com/jpfourny/papaya/pkg/opt"
"slices"
)

// Store represents a container of key-value pairs.
// Used internally for key-grouping and key-joining operations.
type Store[K, V any] interface {
Get(key K) optional.Optional[V]
Get(key K) opt.Optional[V]
Put(key K, value V)
ForEach(func(key K, value V) bool) bool
}
Expand Down Expand Up @@ -49,11 +49,11 @@ func SortedMaker[K any, V any](compare cmp.Comparer[K]) Maker[K, V] {
// The key type K must be comparable.
type mappedStore[K comparable, V any] map[K]V

func (s mappedStore[K, V]) Get(key K) optional.Optional[V] {
func (s mappedStore[K, V]) Get(key K) opt.Optional[V] {
if v, ok := s[key]; ok {
return optional.Of(v)
return opt.Of(v)
}
return optional.Empty[V]()
return opt.Empty[V]()
}

func (s mappedStore[K, V]) Put(key K, value V) {
Expand All @@ -77,11 +77,11 @@ type sortedStore[K any, V any] struct {
values []V
}

func (s *sortedStore[K, V]) Get(key K) optional.Optional[V] {
func (s *sortedStore[K, V]) Get(key K) opt.Optional[V] {
if i, ok := s.indexOf(key); ok {
return optional.Of(s.values[i])
return opt.Of(s.values[i])
}
return optional.Empty[V]()
return opt.Empty[V]()
}

func (s *sortedStore[K, V]) Put(key K, value V) {
Expand Down
22 changes: 11 additions & 11 deletions internal/kvstore/kvstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package kvstore
import (
"github.com/jpfourny/papaya/internal/assert"
"github.com/jpfourny/papaya/pkg/cmp"
"github.com/jpfourny/papaya/pkg/optional"
"github.com/jpfourny/papaya/pkg/opt"
"testing"
)

Expand Down Expand Up @@ -33,7 +33,7 @@ func TestOrderedStore_Get(t *testing.T) {
t.Run("empty", func(t *testing.T) {
ks := NewSorted[int, string](cmp.Natural[int]())
got := ks.Get(0)
want := optional.Empty[string]()
want := opt.Empty[string]()
if got != want {
t.Fatalf("got %#v, want %#v", got, want)
}
Expand All @@ -47,22 +47,22 @@ func TestOrderedStore_Get(t *testing.T) {
ks.Put(3, "three")
ks.Put(2, "dos")
got := ks.Get(1)
want := optional.Of("uno")
want := opt.Of("uno")
if got != want {
t.Fatalf("got %#v, want %#v", got, want)
}
got = ks.Get(2)
want = optional.Of("dos")
want = opt.Of("dos")
if got != want {
t.Fatalf("got %#v, want %#v", got, want)
}
got = ks.Get(3)
want = optional.Of("three")
want = opt.Of("three")
if got != want {
t.Fatalf("got %#v, want %#v", got, want)
}
got = ks.Get(4)
want = optional.Empty[string]()
want = opt.Empty[string]()
if got != want {
t.Fatalf("got %#v, want %#v", got, want)
}
Expand Down Expand Up @@ -119,7 +119,7 @@ func TestMappedStore_Get(t *testing.T) {
t.Run("empty", func(t *testing.T) {
ks := NewMapped[int, string]()
got := ks.Get(0)
want := optional.Empty[string]()
want := opt.Empty[string]()
if got != want {
t.Fatalf("got %#v, want %#v", got, want)
}
Expand All @@ -133,22 +133,22 @@ func TestMappedStore_Get(t *testing.T) {
ks.Put(3, "three")
ks.Put(2, "dos")
got := ks.Get(1)
want := optional.Of("uno")
want := opt.Of("uno")
if got != want {
t.Fatalf("got %#v, want %#v", got, want)
}
got = ks.Get(2)
want = optional.Of("dos")
want = opt.Of("dos")
if got != want {
t.Fatalf("got %#v, want %#v", got, want)
}
got = ks.Get(3)
want = optional.Of("three")
want = opt.Of("three")
if got != want {
t.Fatalf("got %#v, want %#v", got, want)
}
got = ks.Get(4)
want = optional.Empty[string]()
want = opt.Empty[string]()
if got != want {
t.Fatalf("got %#v, want %#v", got, want)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmp/comparer.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func Slice[E any](compare Comparer[E]) Comparer[[]E] {
//
// Example:
//
// s := []*int{pointer.Ref(3), nil, pointer.Ref(1), pointer.Ref(2)}
// s := []*int{ptr.Ref(3), nil, ptr.Ref(1), ptr.Ref(2)}
// sort.Slice(s, cmp.DerefNilFirst(cmp.Natural[int]())) // [nil, 1, 2, 3]
func DerefNilFirst[E any](compare Comparer[E]) Comparer[*E] {
return func(a, b *E) int {
Expand All @@ -282,7 +282,7 @@ func DerefNilFirst[E any](compare Comparer[E]) Comparer[*E] {
//
// Example:
//
// s := []*int{pointer.Ref(3), nil, pointer.Ref(1), pointer.Ref(2)}
// s := []*int{ptr.Ref(3), nil, ptr.Ref(1), ptr.Ref(2)}
// sort.Slice(s, cmp.DerefNilLast(cmp.Natural[int]())) // [1, 2, 3, nil]
func DerefNilLast[E any](compare Comparer[E]) Comparer[*E] {
return func(a, b *E) int {
Expand Down
22 changes: 11 additions & 11 deletions pkg/cmp/comparer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"

"github.com/jpfourny/papaya/pkg/pair"
"github.com/jpfourny/papaya/pkg/pointer"
"github.com/jpfourny/papaya/pkg/ptr"
)

type Person struct {
Expand Down Expand Up @@ -208,31 +208,31 @@ func TestDerefNilFirst(t *testing.T) {
t.Errorf("DerefNilFirst()(nil, nil): expected %d, got %d", want, got)
}

got = c(nil, pointer.Ref(1))
got = c(nil, ptr.Ref(1))
want = -1
if got != want {
t.Errorf("DerefNilFirst()(nil, 1): expected %d, got %d", want, got)
}

got = c(pointer.Ref(1), nil)
got = c(ptr.Ref(1), nil)
want = 1
if got != want {
t.Errorf("DerefNilFirst()(1, nil): expected %d, got %d", want, got)
}

got = c(pointer.Ref(1), pointer.Ref(2))
got = c(ptr.Ref(1), ptr.Ref(2))
want = -1
if got != want {
t.Errorf("DerefNilFirst()(1, 2): expected %d, got %d", want, got)
}

got = c(pointer.Ref(2), pointer.Ref(1))
got = c(ptr.Ref(2), ptr.Ref(1))
want = 1
if got != want {
t.Errorf("DerefNilFirst()(2, 1): expected %d, got %d", want, got)
}

got = c(pointer.Ref(1), pointer.Ref(1))
got = c(ptr.Ref(1), ptr.Ref(1))
want = 0
if got != want {
t.Errorf("DerefNilFirst()(1, 1): expected %d, got %d", want, got)
Expand All @@ -247,31 +247,31 @@ func TestDerefNilLast(t *testing.T) {
t.Errorf("DerefNilLast()(nil, nil): expected %d, got %d", want, got)
}

got = c(nil, pointer.Ref(1))
got = c(nil, ptr.Ref(1))
want = 1
if got != want {
t.Errorf("DerefNilLast()(nil, 1): expected %d, got %d", want, got)
}

got = c(pointer.Ref(1), nil)
got = c(ptr.Ref(1), nil)
want = -1
if got != want {
t.Errorf("DerefNilLast()(1, nil): expected %d, got %d", want, got)
}

got = c(pointer.Ref(1), pointer.Ref(2))
got = c(ptr.Ref(1), ptr.Ref(2))
want = -1
if got != want {
t.Errorf("DerefNilLast()(1, 2): expected %d, got %d", want, got)
}

got = c(pointer.Ref(2), pointer.Ref(1))
got = c(ptr.Ref(2), ptr.Ref(1))
want = 1
if got != want {
t.Errorf("DerefNilLast()(2, 1): expected %d, got %d", want, got)
}

got = c(pointer.Ref(1), pointer.Ref(1))
got = c(ptr.Ref(1), ptr.Ref(1))
want = 0
if got != want {
t.Errorf("DerefNilLast()(1, 1): expected %d, got %d", want, got)
Expand Down
5 changes: 5 additions & 0 deletions pkg/opt/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Package opt defines types for working with opt values, inspired by Java's `Optional` class.
// An opt value either contains a value of a certain type (Some), or it is empty (None).
// This package provides a way to safely handle such values without the risk of null ptr dereferences.
// It includes functions for creating opt values, checking if they are present, and retrieving the value if it is present.
package opt
Loading

0 comments on commit 866a2ba

Please sign in to comment.