github.com/kazu/loncha@v0.6.3/README.md (about)

     1  # loncha
     2  [![Build Status](https://github.com/kazu/loncha/actions/workflows/go.yml/badge.svg)](https://github.com/kazu/loncha/actions/workflows/go.yml)
     3  [![Go Report Card](https://goreportcard.com/badge/github.com/kazu/loncha)](https://goreportcard.com/report/github.com/kazu/loncha)
     4  [![codecov](https://codecov.io/gh/kazu/loncha/branch/master/graph/badge.svg)](https://codecov.io/gh/kazu/loncha)
     5  [![GitHub go.mod Go version of a Go module](https://img.shields.io/github/go-mod/go-version/gomods/athens.svg)](https://github.com/kazu/loncha)
     6  [![GitHub release](https://img.shields.io/github/v/release/kazu/loncha.svg)](https://github.com/kazu/loncha)
     7  
     8  
     9  
    10  A high-performance slice Utilities for Go.
    11  * high-performance slice filter/finder.
    12  * linked-list generics template using [Gennry]
    13  
    14  ## Installation
    15  
    16  ### slice Utility 
    17  
    18      go get github.com/kazu/loncha
    19  
    20  ## QuickStart 
    21  
    22  ### slice Utility
    23  
    24  slice utility dosent use reflect/interface operation.
    25  
    26  ```go
    27      import "github.com/kazu/loncha"
    28  
    29      type GameObject struct {
    30          ID int
    31          Name string
    32          Pos []float
    33      }
    34  
    35      ...
    36  
    37      var objs []GameObject
    38  
    39  ```
    40  
    41  find object from slice
    42  
    43  ```go
    44  
    45      loncha.Find(&objs, func(i int) bool {
    46          return objs[i].ID == 6
    47      } 
    48  ```
    49  
    50  filter/delete object via condition function
    51  
    52  ```go
    53  
    54      loncha.Filter(&objs, func(obj *GameObject) bool {
    55          return obj.ID == 12
    56      } 
    57  
    58  	loncha.Delete(&objs, func(i int) bool {
    59  		return objs[i].ID == 555
    60  	})
    61  ```
    62  
    63  select object with condition function
    64  
    65  ```go
    66  
    67      // find one object with conditions.
    68      obj, err := Select(&objs, func(i int) bool {
    69  		return slice[i].ID < 50
    70  	})
    71  ```
    72  
    73  shuffle slice 
    74  
    75  ```go
    76      err = loncha.Shuffle(objs, 2)
    77  
    78      loncha.Reverse(objs)
    79  ```
    80  
    81  
    82  got intersection from two slices
    83  
    84  
    85  ```go
    86      var obj2 []GameObject
    87      intersectedObj := InsertSect(obj, obj2)
    88  
    89  
    90      sort.Slice(objs, func(i int) bool {
    91          return objs[i].ID >=  objs[j].ID 
    92      })
    93      sort.Slice(objs2, func(i int) bool {
    94          return objs[i].ID >=  objs[j].ID 
    95      })
    96  
    97      intersect2 := IntersectSorted(obj, obj2, func(s []GameObject, i int) int {
    98          return s[i].ID
    99      })
   100  ```
   101  
   102  subtraction from two slices
   103  ```go
   104      subtractObj := Sub(obj, obj2)
   105  
   106      subtract2 := SubSorted(obj, obj2, func(s []GameObject, i int) int {
   107          return s[i].ID
   108      })
   109  
   110  ```
   111  
   112  Returns an object formed from operands via function
   113  
   114  
   115  ```go
   116  	slice1 := []int{10, 6, 4, 2}
   117  
   118  	sum := Inject(slice1, func(sum *int, t int) int {
   119  		return *sum + t
   120  	})
   121  ```
   122  
   123  
   124  ## generate double-linked list of linux kernel list_head type
   125  
   126  define base struct
   127  
   128  ```
   129  package game_object
   130  
   131  import (
   132      "github.com/kazu/loncha/list_head"
   133  
   134  
   135  type Player struct {
   136      ID int
   137      Name string
   138      Hp int
   139      list_head.ListHead
   140  }
   141  ```
   142  
   143  generate linked-list
   144  
   145  ```console
   146      $ go get go get github.com/cheekybits/genny
   147      $ wget -q -O - "https://github.com/kazu/loncha/master/container_list/list.go" | genny  gen "ListEntry=Player" > player_list.go
   148      $ wget -q -O - "https://github.com/kazu/loncha/master/container_list/list_test.go" | genny  gen "ListEntry=Player" > player_list_test.go
   149  ```
   150  ## benchmark Result
   151  
   152  
   153  ### loncha.Uniq vs hand Uniq vs go-funk.Uniq
   154  ```
   155  loncha.Uniq-16         	    			1000	    997543 ns/op	  548480 B/op	   16324 allocs/op
   156  loncha.UniqWithSort-16 	    			1000	   2237924 ns/op	     256 B/op	       7 allocs/op
   157  loncha.UniqWithSort(sort)-16         	1000	    260283 ns/op	     144 B/op	       4 allocs/op
   158  hand_Uniq-16                          	1000	    427765 ns/op	  442642 B/op	       8 allocs/op
   159  hand_Uniq_iface-16                    	1000	    808895 ns/op	  632225 B/op	    6322 allocs/op
   160  go-funk.Uniq-16                       	1000	   1708396 ns/op	  655968 B/op	   10004 allocs/op
   161  ```
   162  
   163  ### loncha.Filter vs go-funk.Filter
   164  
   165  ```
   166  loncha.Filter-16         	     100	     89142 ns/op	   82119 B/op	       4 allocs/op
   167  loncha.Filter_pointer-16 	     100	       201 ns/op	       0 B/op	       0 allocs/op
   168  hand_Filter_pointer-16   	     100	     24432 ns/op	   81921 B/op	       1 allocs/op
   169  go-funk.Filter-16        	     100	   2370492 ns/op	  640135 B/op	   20004 allocs/op
   170  go-funk.Filter_pointer-16        100	      1048 ns/op	      64 B/op	       2 allocs/op
   171  ```
   172  
   173  
   174  ## References 
   175  
   176  - [Gennry]
   177  
   178  
   179  [Gennry]: https://github.com/cheekybits/genny