This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func TestSet(t *testing.T) { | |
set := NewSet() | |
set.Add("the") | |
set.Add("quick") | |
set.Add("brown") | |
set.Add("fox") | |
set.Add("the") | |
if !set.Has("quick") { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type StringSet struct { | |
items map[string]int | |
} | |
func NewSet() StringSet { | |
return StringSet{items: make(map[string]int)} | |
} | |
func (s *StringSet) Add(item string) { | |
s.items[item] = 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func TestTStackInt(t *testing.T) { | |
stack := &TStack[int]{} | |
stack.Push(100) | |
stack.Push(200) | |
stack.Push(300) | |
stack.Push(400) | |
if stack.Peek() != 400 { | |
t.Error(stack.Peek()) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func TestTStack(t *testing.T) { | |
stack := &TStack[string]{} | |
stack.Push("the") | |
stack.Push("quick") | |
stack.Push("brown") | |
stack.Push("fox") | |
if stack.Peek() != "fox" { | |
t.Error(stack.Peek()) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type TStack[T constraints.Ordered] struct { | |
items []T | |
} | |
func (s *TStack[T]) Push(item T) { | |
s.items = append(s.items, item) | |
} | |
func (s *TStack[T]) Pop() (item T) { | |
if !s.IsEmpty() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func TestStringStack(t *testing.T) { | |
stack := &StringStack{} | |
stack.Push("the") | |
stack.Push("quick") | |
stack.Push("brown") | |
stack.Push("fox") | |
if stack.Peek() != "fox" { | |
t.Error(stack.Peek()) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type StringStack struct { | |
items []string | |
} | |
func (s *StringStack) Push(item string) { | |
s.items = append(s.items, item) | |
} | |
func (s *StringStack) Pop() (item string) { | |
if !s.IsEmpty() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "golang.org/x/exp/constraints" | |
type Number interface { | |
constraints.Integer | constraints.Float | |
} | |
func AddNumbers[T Number](a, b T) T { | |
return a + b | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func Add[T int | float64](a, b T) T { | |
return a + b | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Integer struct { | |
... | |
} | |
func (i Integer) Plus(a Number) Number { | |
... | |
} |