Skip to content

Commit

Permalink
Added more details comments including Playground link for every funct…
Browse files Browse the repository at this point in the history
…ion (#11)

This pull request adds detailed example usage comments and playground links to several functions in the `guti.go`, `json.go`, and `list.go` files. These changes aim to improve the documentation and usability of the functions by providing clear examples and interactive playground links.

Documentation improvements:

* [`guti.go`](diffhunk://#diff-fef412a6cf8a1814a0ca84f54696854cd1b9e0cda937c5b9f22305746238ec9eR53-R71): Added example usage comments and a playground link to the `CompareStructs` function.
* [`json.go`](diffhunk://#diff-e848e95f31309994dbd646cda14ba74ae0239fba72b388bb9cc0928057c50980R22-R23): Added playground links to the `JSONToMap`, `JSONToString`, `JSONFileToMap`, and `DeepMergeJSON` functions. [[1]](diffhunk://#diff-e848e95f31309994dbd646cda14ba74ae0239fba72b388bb9cc0928057c50980R22-R23) [[2]](diffhunk://#diff-e848e95f31309994dbd646cda14ba74ae0239fba72b388bb9cc0928057c50980R43-R44) [[3]](diffhunk://#diff-e848e95f31309994dbd646cda14ba74ae0239fba72b388bb9cc0928057c50980R67-R68) [[4]](diffhunk://#diff-e848e95f31309994dbd646cda14ba74ae0239fba72b388bb9cc0928057c50980R109-R110)
* [`list.go`](diffhunk://#diff-71a171b5d5e0b997cd17d0c089fe66366ca924c35e229127f0b7ec0287e1e1efR18-R48): Added example usage comments and playground links to the `IsExist`, `Filter`, `Any`, `Reduce`, `Map`, and `IndexOf` functions. [[1]](diffhunk://#diff-71a171b5d5e0b997cd17d0c089fe66366ca924c35e229127f0b7ec0287e1e1efR18-R48) [[2]](diffhunk://#diff-71a171b5d5e0b997cd17d0c089fe66366ca924c35e229127f0b7ec0287e1e1efL61-R104) [[3]](diffhunk://#diff-71a171b5d5e0b997cd17d0c089fe66366ca924c35e229127f0b7ec0287e1e1efL72-R126) [[4]](diffhunk://#diff-71a171b5d5e0b997cd17d0c089fe66366ca924c35e229127f0b7ec0287e1e1efL82-R155) [[5]](diffhunk://#diff-71a171b5d5e0b997cd17d0c089fe66366ca924c35e229127f0b7ec0287e1e1efL93-R181) [[6]](diffhunk://#diff-71a171b5d5e0b997cd17d0c089fe66366ca924c35e229127f0b7ec0287e1e1efR191-R203)
  • Loading branch information
shahariaazam authored Dec 13, 2024
1 parent bd2cecf commit d4af759
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 7 deletions.
19 changes: 19 additions & 0 deletions guti.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,25 @@ func GetTypeName(myvar interface{}) string {
// The method is designed to work with any kind of struct, as long as it is represented
// as a map or a slice of interfaces. It can be used for testing, data validation,
// or any other use case where you need to compare two structs for equality.
//
// Example usage:
//
// type People struct {
// Age int
// }
// type ExampleStruct struct {
// Name string
// People People
// }
//
// struct1 := ExampleStruct{Name: "John Doe", People: People{Age: 30}}
// struct2 := ExampleStruct{Name: "John Doe", People: People{Age: 40}}
// struct3 := ExampleStruct{Name: "John Doe", People: People{Age: 30}}
//
// fmt.Println(guti.CompareStructs(struct1, struct3)) // should return true
// fmt.Println(guti.CompareStructs(struct1, struct2)) // should return false
//
// Playground: https://go.dev/play/p/GT_7bK_BRro
func CompareStructs(s1 interface{}, s2 interface{}) bool {
if reflect.TypeOf(s1) != reflect.TypeOf(s2) {
return false
Expand Down
8 changes: 8 additions & 0 deletions json.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
// result, err := JSONToMap(data)
// fmt.Println(result["name"]) // prints "John Doe"
// fmt.Println(result["age"]) // prints 30
//
// Playground: https://go.dev/play/p/Tcecnf8wlEb
func JSONToMap(data []byte) (map[string]interface{}, error) {
var result map[string]interface{}
err := json.Unmarshal(data, &result)
Expand All @@ -38,6 +40,8 @@ func JSONToMap(data []byte) (map[string]interface{}, error) {
// data := map[string]interface{}{"name": "John", "age": 30}
// result, err := JSONToString(data)
// fmt.Println(result) // prints '{"age":30,"name":"John"}'
//
// Playground: https://go.dev/play/p/PFc88pqp3CD
func JSONToString(data interface{}) (string, error) {
b, err := json.Marshal(data)
return string(b), err
Expand All @@ -60,6 +64,8 @@ func JSONToString(data interface{}) (string, error) {
// } else {
// fmt.Println(result)
// }
//
// Playground: https://go.dev/play/p/Tk0pqxI5E8A
func JSONFileToMap(filename string) (map[string]interface{}, error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
Expand Down Expand Up @@ -100,6 +106,8 @@ func JSONFileToMap(filename string) (map[string]interface{}, error) {
// }
// result := DeepMergeJSON(dst, src)
// fmt.Println(result)
//
// Playground: https://go.dev/play/p/zAZieDE3OUf
func DeepMergeJSON(dst, src map[string]interface{}) map[string]interface{} {
for key, srcVal := range src {
if dstVal, ok := dst[key]; ok {
Expand Down
114 changes: 107 additions & 7 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,37 @@ const epsilon = 1e-6
// and objects. It uses reflection to determine the type of the items in the slice, and to compare them
// to the search item. If the second argument is not a slice, it will panic. If the search item is not
// of the same type as the items in the slice, it will be skipped.
//
// Example usage:
//
// intSlice := []int{1, 2, 3, 4, 5}
// fmt.Println(guti.IsExist(3, intSlice)) // prints "true"
// fmt.Println(guti.IsExist(6, intSlice)) // prints "false"
//
// strSlice := []string{"foo", "bar", "baz"}
// fmt.Println(guti.IsExist("qux", strSlice)) // prints "false"
// fmt.Println(guti.IsExist("foo", strSlice)) // prints "true"
//
// objectSlice := []struct {
// Name string
// Age int
// }{
// {Name: "Alice", Age: 25},
// {Name: "Bob", Age: 25},
// {Name: "Charlie", Age: 35},
// }
// fmt.Println(guti.IsExist(struct {
// Name string
// Age int
// }{Name: "Bob", Age: 25}, objectSlice)) // prints "true"
//
// boolSlice := []bool{true, false}
// fmt.Println(guti.IsExist(true, boolSlice)) // prints "true"
//
// emptySlice := []int{}
// fmt.Println(guti.IsExist(1, emptySlice)) // prints "false"
//
// Playground: https://go.dev/play/p/jHua3iwd6xT
func IsExist(what interface{}, in interface{}) bool {
s := reflect.ValueOf(in)

Expand Down Expand Up @@ -58,7 +89,19 @@ func IsExist(what interface{}, in interface{}) bool {
return false
}

// Filter a function that filters a list based on a given predicate function. The function returns a new list with the elements that satisfy the predicate function.
// Filter returns a new list containing the elements of the input list that
// satisfy the given predicate function. The predicate function takes an input
// element of the list and returns true if the element should be included in the
// output list, and false otherwise. The input list can contain elements of any
// type, and the predicate function should take an argument of type interface{}.
//
// Example usage:
//
// data := []interface{}{1, 2, 3, 4, 5}
// isEven := func(x interface{}) bool { return x.(int)%2 == 0 }
// result := Filter(data, isEven) // result = []interface{}{2, 4}
//
// Playground: https://go.dev/play/p/haueBKmeb3e
func Filter(data []interface{}, predicate func(interface{}) bool) []interface{} {
result := []interface{}{}
for _, d := range data {
Expand All @@ -69,7 +112,18 @@ func Filter(data []interface{}, predicate func(interface{}) bool) []interface{}
return result
}

// Any a function that returns true if at least one element of a list satisfies a given predicate function.
// Any returns true if at least one element of the input list satisfies the given predicate function,
// and false otherwise. The predicate function takes an input element of the list and returns true
// if the element satisfies the predicate, and false otherwise. The input list can contain elements
// of any type, and the predicate function should take an argument of type interface{}.
//
// Example usage:
//
// data := []interface{}{1, 2, 3, 4, 5}
// isEven := func(x interface{}) bool { return x.(int)%2 == 0 }
// result := Any(data, isEven) // result = true
//
// Playground: https://go.dev/play/p/mVzWG6tTp_2
func Any(data []interface{}, predicate func(interface{}) bool) bool {
for _, d := range data {
if predicate(d) {
Expand All @@ -79,9 +133,26 @@ func Any(data []interface{}, predicate func(interface{}) bool) bool {
return false
}

// Reduce a function that applies a reducing function to a list and returns a
// single value. The reducing function takes two arguments, an accumulator and a
// value, and returns a new accumulator.
// Reduce applies a reducing function to a list and returns a single value.
// The reducing function takes two arguments, an accumulator and a value, and returns
// a new accumulator. The initial value of the accumulator is provided as an argument.
// The function can reduce lists of any type, including integers, floats, strings,
// and custom types. If the initial value is not of the same type as the elements of
// the list, it will panic. The function returns the final value of the accumulator.
//
// Example usage:
//
// data := []interface{}{1, 2, 3, 4, 5}
//
// reduceFunc := func(acc interface{}, value interface{}) interface{} {
// return acc.(int) + value.(int)
// }
//
// initial := 0
// result := guti.Reduce(data, reduceFunc, initial)
// fmt.Println(result) // should print 15
//
// Playground: https://go.dev/play/p/A7ZQrVp_uIk
func Reduce(data []interface{}, reduce func(interface{}, interface{}) interface{}, initial interface{}) interface{} {
acc := initial
for _, d := range data {
Expand All @@ -90,8 +161,24 @@ func Reduce(data []interface{}, reduce func(interface{}, interface{}) interface{
return acc
}

// Map a function that applies a transformation function to each element
// of a list and returns a new list with the transformed elements.
// Map applies a transformation function to each element of a slice and returns a new slice with the
// transformed elements. The transform function takes an element of the input slice as input and returns
// a transformed value. The input slice can contain elements of any type, but the transform function must
// be able to handle each element type appropriately. The returned slice has the same length as the input
// slice, and each element is the result of applying the transform function to the corresponding input element.
// The input slice is not modified by the function.
//
// Example usage:
//
// input := []interface{}{1, 2, 3, 4, 5}
//
// transform := func(d interface{}) interface{} {
// return d.(int) * 2
// }
// output := Map(input, transform)
// fmt.Println(output) // [2 4 6 8 10]
//
// Playground: https://go.dev/play/p/ZguMfToP0Xh
func Map(data []interface{}, transform func(interface{}) interface{}) []interface{} {
result := []interface{}{}
for _, d := range data {
Expand All @@ -101,6 +188,19 @@ func Map(data []interface{}, transform func(interface{}) interface{}) []interfac
}

// IndexOf returns the index of the first occurrence of a given element in a list. If the element is not found, it returns -1.
// The data parameter is a slice of interface{} type which can hold any type of data. The element parameter is the element whose index is to be searched in the slice.
// This function returns an integer value that represents the index of the first occurrence of the given element in the slice.
//
// Example usage:
//
// data := []interface{}{"apple", "banana", "cherry"}
//
// element := "banana"
//
// index := guti.IndexOf(data, element)
// fmt.Println("Index of", element, "is", index) // should output: Index of banana is 1
//
// Playground: https://go.dev/play/p/K7X-4_RbJPG
func IndexOf(data []interface{}, element interface{}) int {
for i, d := range data {
if d == element {
Expand Down

0 comments on commit d4af759

Please sign in to comment.