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

Seq trait #16

Merged
merged 5 commits into from
May 4, 2022
Merged
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
19 changes: 19 additions & 0 deletions pure/foldable/foldable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// Copyright (C) 2022 Dmitry Kolesnikov
//
// This file may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
// https://github.com/fogfish/golem
//

package foldable

import "github.com/fogfish/golem/pure/monoid"

/*

Foldable type trait
*/
type Foldable[F_, A any] interface {
Fold(monoid.Monoid[A], F_) A
}
54 changes: 54 additions & 0 deletions pure/monoid/monoid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// Copyright (C) 2022 Dmitry Kolesnikov
//
// This file may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
// https://github.com/fogfish/golem
//

package monoid

import "github.com/fogfish/golem/pure/semigroup"

/*

Monoid is an algebraic structure consisting of Semigroup and Empty element
*/
type Monoid[T any] interface {
semigroup.Semigroup[T]
Empty() T
}

/*

From is a combinator that lifts Semigroup to an instance of Monoid type trait
*/
func From[T any](empty T, combine semigroup.Semigroup[T]) Monoid[T] {
return monoid[T]{
Semigroup: combine,
empty: empty,
}
}

/*

FromOp is a combinator that lifts T ⟼ T ⟼ T function (binary operator) to
an instance of Monoid type trait
*/
func FromOp[T any](empty T, combine func(T, T) T) Monoid[T] {
return monoid[T]{
Semigroup: semigroup.From[T](combine),
empty: empty,
}
}

/*

Internal implementation of Monoid interface
*/
type monoid[T any] struct {
semigroup.Semigroup[T]
empty T
}

func (m monoid[T]) Empty() T { return m.empty }
38 changes: 38 additions & 0 deletions seq/foldable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// Copyright (C) 2022 Dmitry Kolesnikov
//
// This file may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
// https://github.com/fogfish/golem
//

package seq

import (
"github.com/fogfish/golem/pure/foldable"
"github.com/fogfish/golem/pure/monoid"
)

/*

Foldable Sequence
*/
type Foldable[F_, A any] struct{ Seq[F_, A] }

var _ foldable.Foldable[any, any] = Foldable[any, any]{}

/*

Fold sequence with Monoid
*/
func (f Foldable[F_, A]) Fold(m monoid.Monoid[A], seq F_) A {
x := m.Empty()
s := seq

for !f.Seq.IsEmpty(s) {
x = m.Combine(x, f.Seq.Head(s))
s = f.Seq.Tail(s)
}

return x
}
60 changes: 60 additions & 0 deletions seq/list/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//
// Copyright (C) 2022 Dmitry Kolesnikov
//
// This file may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
// https://github.com/fogfish/golem
//

package list

import "github.com/fogfish/golem/seq"

/*

Seq type build over slice data structure
*/
type Seq[A any] struct {
len int
list *list[A]
}

func (Seq[A]) HKT1(seq.Type) {}
func (Seq[A]) HKT2(A) {}

type list[A any] struct {
head A
tail *list[A]
}

/*

Trait implements seq.Seq type law for Seq type
*/
type Trait[A any] string

var _ seq.Seq[Seq[any], any] = Trait[any]("seq.any")

func (Trait[A]) New(seq ...A) Seq[A] {
var tail *list[A]

for i := len(seq) - 1; i >= 0; i-- {
tail = &list[A]{head: seq[i], tail: tail}
}

return Seq[A]{len: len(seq), list: tail}
}

func (Trait[A]) Cons(x A, seq Seq[A]) Seq[A] {
return Seq[A]{
len: seq.len + 1,
list: &list[A]{head: x, tail: seq.list},
}
}

func (Trait[A]) Head(seq Seq[A]) A { return seq.list.head }
func (Trait[A]) Tail(seq Seq[A]) Seq[A] { return Seq[A]{len: seq.len - 1, list: seq.list.tail} }

func (Trait[A]) Length(seq Seq[A]) int { return seq.len }

func (Trait[A]) IsEmpty(seq Seq[A]) bool { return seq.len == 0 }
38 changes: 38 additions & 0 deletions seq/list/list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// Copyright (C) 2022 Dmitry Kolesnikov
//
// This file may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
// https://github.com/fogfish/golem
//

package list_test

import (
"testing"

"github.com/fogfish/golem/seq/list"
"github.com/fogfish/golem/seq/seqtest"
)

var (
seqT = list.Trait[int]("seq.slice.int")

defCap int = 1000000
defSeq list.Seq[int] = seqT.New()
)

func TestList(t *testing.T) {
seqtest.TestSeq[list.Seq[int], int](t, seqT, seqT.New(1, 2, 3, 4, 5))
seqtest.TestFoldable[list.Seq[int]](t, seqT)
}

func init() {
for n := 0; n < defCap; n++ {
defSeq = seqT.Cons(n, defSeq)
}
}

func BenchmarkList(b *testing.B) {
seqtest.Benchmark[list.Seq[int]](b, seqT, defSeq)
}
114 changes: 114 additions & 0 deletions seq/seqtest/seqtest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
//
// Copyright (C) 2022 Dmitry Kolesnikov
//
// This file may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
// https://github.com/fogfish/golem
//

package seqtest

import (
"testing"

"github.com/fogfish/golem/pure/monoid"
"github.com/fogfish/golem/seq"
"github.com/fogfish/it"
)

func TestSeq[F_ seq.Kind[A], A any](t *testing.T, seqT seq.Seq[F_, A], seed F_) {
nul := seqT.New()
one := seqT.New(*new(A))
few := seqT.New(*new(A), *new(A), *new(A))

t.Run("Seq.HKT", func(t *testing.T) {
seed.HKT1(seq.Type(nil))
seed.HKT2(*new(A))
})

t.Run("Seq.Length", func(t *testing.T) {
it.Ok(t).
If(seqT.Length(nul)).Equal(0).
If(seqT.Length(one)).Equal(1).
If(seqT.Length(few)).Equal(3)
})

t.Run("Seq.IsEmpty", func(t *testing.T) {
it.Ok(t).
If(seqT.IsEmpty(nul)).Equal(true).
If(seqT.IsEmpty(one)).Equal(false).
If(seqT.IsEmpty(few)).Equal(false)
})

t.Run("Seq.Head", func(t *testing.T) {
it.Ok(t).
If(seqT.Head(one)).Equal(*new(A)).
If(seqT.Head(few)).Equal(*new(A))
})

t.Run("Seq.Tail", func(t *testing.T) {
it.Ok(t).
If(seqT.Length(seqT.Tail(one))).Equal(0).
If(seqT.Length(seqT.Tail(few))).Equal(2)
})

t.Run("Seq.Cons", func(t *testing.T) {
for _, s := range []F_{nul, one, few} {
len := seqT.Length(s)
seqCons := seqT.Cons(seqT.Head(seed), s)

it.Ok(t).
If(seqT.Length(seqCons)).Equal(len + 1).
If(seqT.Head(seqCons)).Equal(seqT.Head(seed))
}
})
}

func TestFoldable[F_ any](t *testing.T, seqT seq.Seq[F_, int]) {
f := seq.Foldable[F_, int]{Seq: seqT}

t.Run("Foldable.Fold", func(t *testing.T) {
x := f.Fold(
monoid.FromOp(0, func(a, b int) int { return a + b }),
seqT.New(1, 2, 3, 4, 5),
)

it.Ok(t).If(x).Equal(15)
})
}

func Benchmark[F_ any](b *testing.B, seqT seq.Seq[F_, int], val F_) {
b.Run("Seq.Cons", func(b *testing.B) {
seq := seqT.New()

b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
seq = seqT.Cons(n, seq)
}
})

b.Run("Seq.Tail", func(b *testing.B) {
seq := seqT.New()

b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
if seqT.IsEmpty(seq) {
seq = val
}
seq = seqT.Tail(seq)
}
})

b.Run("Seq.Fold", func(b *testing.B) {
f := seq.Foldable[F_, int]{Seq: seqT}
m := monoid.FromOp(0, func(a, b int) int { return a + b })

b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
f.Fold(m, val)
}
})
}
41 changes: 41 additions & 0 deletions seq/slice/slice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// Copyright (C) 2022 Dmitry Kolesnikov
//
// This file may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
// https://github.com/fogfish/golem
//

package slice

import "github.com/fogfish/golem/seq"

/*

Seq type build over slice data structure
*/
type Seq[A any] []A

func (Seq[A]) HKT1(seq.Type) {}
func (Seq[A]) HKT2(A) {}

/*

Trait implements seq.Seq type law for Seq type
*/
type Trait[A any] string

var _ seq.Seq[Seq[any], any] = Trait[any]("seq.any")

func (Trait[A]) New(seq ...A) Seq[A] { return seq }

func (Trait[A]) Cons(x A, seq Seq[A]) Seq[A] {
return append([]A{x}, seq...)
}

func (Trait[A]) Head(seq Seq[A]) A { return seq[0] }
func (Trait[A]) Tail(seq Seq[A]) Seq[A] { return seq[1:] }

func (Trait[A]) Length(seq Seq[A]) int { return len(seq) }

func (Trait[A]) IsEmpty(seq Seq[A]) bool { return len(seq) == 0 }
Loading