-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
hamed
committed
Aug 5, 2022
1 parent
2278b5b
commit 88d7fd2
Showing
10 changed files
with
290 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
type Number interface{ | ||
int | int16 | int32 | int64 | float32 | float64 | ||
} | ||
|
||
func main() { | ||
x := Sum(2, 7) | ||
fmt.Printf("%d\n", x) | ||
|
||
y := Sum(2.5, 7.5) | ||
fmt.Printf("%f\n", y) | ||
} | ||
|
||
func SumInts(a, b int) int { | ||
return a + b | ||
} | ||
|
||
func SumFloats(a, b float64) float64 { | ||
return a + b | ||
} | ||
|
||
func Sum[T Number](a, b T) T { | ||
return a + b | ||
} | ||
|
||
|
||
// func Call[Input any, Output any](headers string, uri string, input Input) Output { | ||
// // http request() | ||
// // | ||
|
||
// } |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
type Number interface { | ||
int | float64 | ||
} | ||
|
||
func main() { | ||
|
||
mySlice := []int{1, 2, 3, 4, 5} | ||
|
||
fmt.Printf("%d\n", Sum(mySlice)) | ||
|
||
mySlice2 := []float64{1.5, 2.5, 3.5, 4.5, 5.5} | ||
|
||
fmt.Printf("%f\n", Sum(mySlice2)) | ||
} | ||
|
||
func Sum[T Number](slc []T) (sum T) { | ||
for _, v := range slc { | ||
sum += v | ||
} | ||
return | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package generics | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
type List[T any] struct { | ||
Items []T | ||
} | ||
|
||
func (list *List[T]) Add(item T) { | ||
list.Items = append(list.Items, item) | ||
} | ||
|
||
func (list *List[T]) InsertAt(index int, item T) { | ||
// 1 2 3 4 5 | ||
// 1 2 30 3 4 5 | ||
list.Items = append(list.Items, item) // 1 2 3 4 5 30 | ||
copy(list.Items[index+1:], list.Items[index:]) // 1 2 3 3 4 5 | ||
list.Items[index] = item // 1 2 30 3 4 5 | ||
|
||
} | ||
|
||
func (list *List[T]) RemoveAt(index int) { | ||
// 1 2 3 4 5 6 7 8 9 10 | ||
defer func() { | ||
if err := recover(); err != nil { | ||
fmt.Println(err) | ||
} | ||
}() | ||
list.Items = append(list.Items[:index*-1], list.Items[index+1:]...) | ||
} | ||
func (list *List[T]) Remove(item T) { | ||
index := list.Find(item) | ||
if index != -1 { | ||
list.RemoveAt(index) | ||
} | ||
} | ||
|
||
func (list *List[T]) Get(index int) T { | ||
return list.Items[index] | ||
} | ||
|
||
func (list *List[T]) Find(item T) int { | ||
for i, v := range list.Items { | ||
if cmp.Equal(v, item) { | ||
return i | ||
} | ||
} | ||
return -1 | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module genericList | ||
|
||
go 1.18 | ||
|
||
require github.com/google/go-cmp v0.5.8 // indirect |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= | ||
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"genericList/generics" | ||
) | ||
|
||
type Person struct { | ||
Name string | ||
Age int | ||
} | ||
|
||
func main() { | ||
|
||
genericInt() | ||
genericString() | ||
genericPerson() | ||
} | ||
|
||
func genericInt() { | ||
list1 := generics.List[int]{Items: []int{}} | ||
|
||
list1.Add(10) | ||
list1.Add(20) | ||
list1.Add(30) | ||
fmt.Printf("%v\n", list1.Items) | ||
|
||
list1.InsertAt(1, 15) | ||
fmt.Printf("%v\n", list1.Items) | ||
|
||
list1.RemoveAt(2) | ||
fmt.Printf("%v\n", list1.Items) | ||
|
||
list1.Remove(30) | ||
fmt.Printf("%v\n", list1.Items) | ||
} | ||
|
||
func genericString() { | ||
list1 := generics.List[string]{Items: []string{}} | ||
|
||
list1.Add("Ali") | ||
list1.Add("Reza") | ||
list1.Add("Milad") | ||
fmt.Printf("%v\n", list1.Items) | ||
|
||
list1.Remove("Reza") | ||
fmt.Printf("%v\n", list1.Items) | ||
|
||
list1.InsertAt(1, "Reza1") | ||
fmt.Printf("%v\n", list1.Items) | ||
|
||
} | ||
func genericPerson() { | ||
list1 := generics.List[Person]{Items: []Person{}} | ||
list1.Add(Person{Name: "Ali", Age: 30}) | ||
list1.Add(Person{Name: "Reza", Age: 20}) | ||
list1.Add(Person{Name: "Milad", Age: 10}) | ||
|
||
fmt.Printf("%v\n", list1.Items) | ||
|
||
list1.Remove(Person{Name: "Reza", Age: 20}) | ||
fmt.Printf("%v\n", list1.Items) | ||
|
||
list1.InsertAt(1, Person{Name: "Reza1", Age: 20}) | ||
fmt.Printf("%v\n", list1.Items) | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Launch Package", | ||
"type": "go", | ||
"request": "launch", | ||
"mode": "auto", | ||
"program": "${workspaceFolder}" | ||
} | ||
] | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module miniProject | ||
|
||
go 1.18 | ||
|
||
require github.com/google/go-cmp v0.5.8 // indirect |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= | ||
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
type List[T any] struct { | ||
Items []T | ||
} | ||
|
||
type Person struct { | ||
Name string | ||
Age int | ||
} | ||
|
||
func main() { | ||
|
||
lst := List[int]{Items: []int{}} | ||
lst.Add(2) | ||
lst.Add(10) | ||
lst.Add(11) | ||
lst.Add(12) | ||
lst.Add(13) | ||
lst.Add(14) | ||
|
||
fmt.Printf("%+v\n", lst.Items) | ||
|
||
lst.InsertAt(4, 20) | ||
lst.InsertAt(5, 30) | ||
lst.InsertAt(1, 40) | ||
|
||
fmt.Printf("%+v\n", lst.Items) | ||
|
||
lst1 := List[Person]{Items: []Person{}} | ||
|
||
lst1.Add(Person{Name: "John", Age: 30}) | ||
lst1.Add(Person{Name: "Jane", Age: 25}) | ||
lst1.Add(Person{Name: "Jack", Age: 27}) | ||
lst1.Add(Person{Name: "Jill", Age: 22}) | ||
lst1.Add(Person{Name: "Joe", Age: 28}) | ||
lst1.Add(Person{Name: "Jill", Age: 22}) | ||
lst1.Add(Person{Name: "Joe", Age: 28}) | ||
|
||
lst1.InsertAt(4, Person{Name: "Hamed", Age: 30}) | ||
|
||
lst1.RemoveAt(3) | ||
|
||
fmt.Printf("%+v\n", lst1.Items) | ||
|
||
} | ||
|
||
func (list *List[T]) Add(item T) { | ||
list.Items = append(list.Items, item) | ||
} | ||
|
||
func (list *List[T]) Remove(item T) { | ||
index := list.Find(item) | ||
list.RemoveAt(index) | ||
} | ||
|
||
func (list *List[T]) RemoveAt(index int) { | ||
list.Items = append(list.Items[:index*2], (list.Items[index+1:])...) | ||
} | ||
|
||
func (list *List[T]) InsertAt(index int, item T) { | ||
list.Items = append(list.Items, item) | ||
copy(list.Items[index+1:], list.Items[index:]) | ||
list.Items[index] = item | ||
} | ||
|
||
func (list *List[T]) Find(item T) (index int) { | ||
|
||
for i, v := range list.Items { | ||
if cmp.Equal(v, item) { | ||
index = i | ||
return | ||
} | ||
} | ||
return -1 | ||
} |