Skip to content

Commit

Permalink
Add Map.Copy and Set.Copy
Browse files Browse the repository at this point in the history
  • Loading branch information
kamstrup committed Jun 2, 2023
1 parent 45a25f4 commit 3abeea5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
9 changes: 9 additions & 0 deletions seq/map.go
Expand Up @@ -241,3 +241,12 @@ func (a Map[K, V]) Get(k K) opt.Opt[V] {
}
return opt.Empty[V]()
}

// Copy returns a copy of this map
func (a Map[K, V]) Copy() Map[K, V] {
dup := make(Map[K, V], len(a))
for k, v := range a {
dup[k] = v
}
return dup
}
9 changes: 9 additions & 0 deletions seq/set.go
Expand Up @@ -252,3 +252,12 @@ func (s Set[K]) Intersect(other Set[K]) Seq[K] {
}
return other.Where(s.Contains)
}

// Copy returns a copy of this set
func (s Set[K]) Copy() Set[K] {
dup := make(Set[K], len(s))
for k := range s {
dup[k] = struct{}{}
}
return dup
}
23 changes: 13 additions & 10 deletions seq/slice.go
Expand Up @@ -16,20 +16,23 @@ import (
//
// # Examples:
//
// // Slices can be created as literals
// mySlice := seq.Slice[string]{"one", "two"}
// // Slices can be created as literals
// mySlice := seq.Slice[string]{"one", "two"}
//
// // They can be allocated with make()
// emptySliceWithCap10 := make(seq.Slice[string], 0, 10)
// // They can be allocated with make()
// emptySliceWithCap10 := make(seq.Slice[string], 0, 10)
//
// // You can call len()
// fmt.Println("Length of mySlice:", len(mySlice))
// // You can call len()
// fmt.Println("Length of mySlice:", len(mySlice))
//
// // You can iterate with an idiomatic for-loop
// for i, v := range mySlice { fmt.Println("Index:", i, "Value:", v) }
// // You can iterate with an idiomatic for-loop
// for i, v := range mySlice { fmt.Println("Index:", i, "Value:", v) }
//
// // You can access elements by index
// twoString := mySlice[1]
// // You can access elements by index
// twoString := mySlice[1]
//
// // You can use slicing
// subSlice := mySlice[0:1]
type Slice[T any] []T

// Empty returns an empty seq
Expand Down

0 comments on commit 3abeea5

Please sign in to comment.