@@ -18,15 +18,15 @@ go install github.com/logic-building/functional-go/gofp
18
18
```
19
19
20
20
### dep Gopkg.toml entry
21
- ```
21
+ ``` toml
22
22
[[constraint ]]
23
23
name = " github.com/logic-building/functional-go"
24
24
version = " 8.13.1"
25
25
```
26
26
27
27
### Quick Start
28
28
#### For Data types available in golang
29
- ```
29
+ ``` go
30
30
import " github.com/logic-building/functional-go/fp"
31
31
32
32
fp.MapInt (square, []int {1 , 2 , 3 , 4 }) // Returns: [1 4 9 16]
@@ -39,7 +39,7 @@ func square(num int) int {
39
39
#### Four variants of function. 1 is given above and 3 are given below
40
40
##### MapInt, MapIntPtr, MapIntErr, MapIntPtrErr
41
41
### MapIntPtr
42
- ```
42
+ ``` go
43
43
package main
44
44
45
45
import (
@@ -69,7 +69,7 @@ output:
69
69
```
70
70
71
71
### MapIntErr
72
- ```
72
+ ``` go
73
73
package main
74
74
75
75
import (
@@ -98,7 +98,7 @@ output:
98
98
```
99
99
100
100
### MapIntPtrErr
101
- ```
101
+ ``` go
102
102
package main
103
103
104
104
import (
@@ -138,19 +138,19 @@ Design 2: All functional code in one place
138
138
```
139
139
### Design 1: Functional code distributed within different package
140
140
#### Generate functional code for struct - Employee
141
- ```
141
+ ``` go
142
142
type Employee struct {
143
143
id int
144
144
name string
145
145
salary float64
146
146
}
147
147
```
148
148
#### 1. Add line given below in the file where struct resides
149
- ```
149
+ ``` go
150
150
// go:generate gofp -destination fp.go -pkg employee -type "Employee"
151
151
```
152
152
##### Example:
153
- ```
153
+ ``` go
154
154
// go:generate gofp -destination fp.go -pkg employee -type "Employee"
155
155
type Employee struct {
156
156
id int
@@ -178,7 +178,7 @@ Design 2: All functional code in one place
178
178
go generate ./...
179
179
```
180
180
#### You are done. Enjoy the functional code
181
- ```
181
+ ``` go
182
182
emp1 := employee.Employee {1 , " A" , 1000 }
183
183
emp2 := employee.Employee {2 , " B" , 1000 }
184
184
emp3 := employee.Employee {3 , " C" , 1000 }
@@ -840,7 +840,7 @@ SetStrSync
840
840
```
841
841
842
842
### Example1 - Map : return the list of the square of each items in the list
843
- ```
843
+ ``` go
844
844
squareList := fp.MapInt (squareInt, []int {1 , 2 , 3 }) // see the map_test.go for detail
845
845
846
846
func squareInt (num int ) int {
@@ -855,7 +855,7 @@ output
855
855
```
856
856
857
857
### Example2 - Filter: filter all the even numbers in the list
858
- ```
858
+ ``` go
859
859
filteredList := fp.FilterInt (isEven, []int {1 , 2 , 3 , 4 })
860
860
861
861
func isEven (num int ) bool {
@@ -868,7 +868,7 @@ output:
868
868
```
869
869
870
870
### Example3 - fp.FilterMap: Multiply all positive numbers in the list by 2
871
- ```
871
+ ``` go
872
872
filteredList := FilterMapInt (isPositive, multiplyBy2, []int {-1 , 0 , 2 , 4 })
873
873
874
874
func isPositive (num int ) bool {
@@ -883,7 +883,7 @@ output:
883
883
```
884
884
885
885
### Example4 - Every: Test if every number in the list is even
886
- ```
886
+ ``` go
887
887
list := []bool {true , true , true , true }
888
888
fp.EveryBool (fp.True , list) // Returns true
889
889
@@ -892,25 +892,25 @@ fp.EveryInt(isEven, list1) // Returns true
892
892
```
893
893
894
894
### Example5 - Exists: Test if number presents in the list
895
- ```
895
+ ``` go
896
896
list1 := []int {8 , 2 , 10 , 4 }
897
897
fp.ExistsInt (8 , list1) // returns true
898
898
```
899
899
900
900
### Example6 - Max: Get max number in the list
901
- ```
901
+ ``` go
902
902
list := []int {8 , 2 , 10 , 4 }
903
903
max := fp.MaxInt (list) // returns 10
904
904
```
905
905
906
906
### Example7 - Min: Get min number in the list
907
- ```
907
+ ``` go
908
908
list := []int {8 , 2 , 10 , 4 }
909
909
min := fp.MinInt (list) // returns 2
910
910
```
911
911
912
912
### Example8 - Returns a new list after dropping the given item
913
- ```
913
+ ``` go
914
914
newList := fp.DropInt (1 , []int {1 , 2 , 3 , 1 }) // returns [2, 3]
915
915
916
916
To drop multiple items:
@@ -919,26 +919,26 @@ newList := fp.DropInts([]int{1, 2}, []int{1, 2, 3, 1}) // returns [3]
919
919
```
920
920
921
921
### Example9 - Distinct: returns distinct list
922
- ```
922
+ ``` go
923
923
list := []int {8 , 2 , 8 , 0 , 2 , 0 }
924
924
distinct := fp.DistinctInt (list) // returns [8, 2, 0]
925
925
```
926
926
927
927
### Example10 - Set : Create set objects and apply union operation
928
- ```
928
+ ``` go
929
929
mySet1 := set.NewInt ([]int {10 , 20 , 30 , 20 })
930
930
mySet2 := set.NewInt ([]int {30 , 40 , 50 })
931
931
mySet3 := mySet1.Union (mySet2) // Returns [10, 20, 30, 40, 50]
932
932
```
933
933
934
934
### Example11 - Range : accepts lower and end int and returns range list
935
- ```
935
+ ``` go
936
936
fp.Range (1 , 5 ) // returns [1, 2, 3, 4]
937
937
fp.Range (1 , 5 , 2 ) // returns [1, 3]
938
938
```
939
939
940
940
### Example12 - Remove :
941
- ```
941
+ ``` go
942
942
NewList := RemoveInt (isEven, []int {1 , 2 , 3 , 4 }) // returns [1, 3]
943
943
944
944
func isEven (num int ) bool {
@@ -948,7 +948,7 @@ distinct := fp.DistinctInt(list) // returns [8, 2, 0]
948
948
```
949
949
950
950
### Example13 - DropWhileInt :
951
- ```
951
+ ``` go
952
952
NewList := DropWhile (isEven, []int {4 , 2 , 3 , 4 }) // returns [3, 4]
953
953
954
954
func isEven (num int ) bool {
@@ -958,7 +958,7 @@ distinct := fp.DistinctInt(list) // returns [8, 2, 0]
958
958
```
959
959
960
960
### Example14 - TakeWhileInt :
961
- ```
961
+ ``` go
962
962
NewList := TakeWhile (isEven, []int {4 , 2 , 3 , 4 }) // returns [4, 2]
963
963
964
964
func isEven (num int ) bool {
0 commit comments