Skip to content

Commit f4c3051

Browse files
Merge pull request #119 from harrisb002/docs/separate-readme-examples-boolean
docs: Separate README and EXAMPLES for all packages (#103)
2 parents 91ebffa + e63a0c1 commit f4c3051

36 files changed

+3428
-3053
lines changed

EXAMPLES.md

Lines changed: 0 additions & 2792 deletions
This file was deleted.

README.md

Lines changed: 31 additions & 261 deletions
Large diffs are not rendered by default.

boolean/EXAMPLES.md

Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
## Boolean Function Examples
2+
3+
### Check if the provided string is a true
4+
5+
```go
6+
package main
7+
8+
import (
9+
"fmt"
10+
11+
boolutils "github.com/kashifkhan0771/utils/boolean"
12+
)
13+
14+
func main() {
15+
fmt.Println(boolutils.IsTrue("T"))
16+
fmt.Println(boolutils.IsTrue("1"))
17+
fmt.Println(boolutils.IsTrue("TRUE"))
18+
}
19+
```
20+
21+
#### Output:
22+
23+
```
24+
true
25+
true
26+
true
27+
```
28+
29+
### Toggle a boolean value
30+
31+
```go
32+
package main
33+
34+
import (
35+
"fmt"
36+
37+
boolutils "github.com/kashifkhan0771/utils/boolean"
38+
)
39+
40+
func main() {
41+
fmt.Println(boolutils.Toggle(true))
42+
fmt.Println(boolutils.Toggle(false))
43+
}
44+
```
45+
46+
#### Output:
47+
48+
```
49+
false
50+
true
51+
```
52+
53+
### Check if all values in a slice are true
54+
55+
```go
56+
package main
57+
58+
import (
59+
"fmt"
60+
61+
boolutils "github.com/kashifkhan0771/utils/boolean"
62+
)
63+
64+
func main() {
65+
fmt.Println(boolutils.AllTrue([]bool{true, true, true}))
66+
fmt.Println(boolutils.AllTrue([]bool{true, false, true}))
67+
fmt.Println(boolutils.AllTrue([]bool{}))
68+
}
69+
```
70+
71+
#### Output:
72+
73+
```
74+
true
75+
false
76+
false
77+
```
78+
79+
### Check if any value in a slice is true
80+
81+
```go
82+
package main
83+
84+
import (
85+
"fmt"
86+
87+
boolutils "github.com/kashifkhan0771/utils/boolean"
88+
)
89+
90+
func main() {
91+
fmt.Println(boolutils.AnyTrue([]bool{false, true, false}))
92+
fmt.Println(boolutils.AnyTrue([]bool{false, false, false}))
93+
fmt.Println(boolutils.AnyTrue([]bool{}))
94+
}
95+
```
96+
97+
#### Output:
98+
99+
```
100+
true
101+
false
102+
false
103+
```
104+
105+
### Check if none of the values in a slice are true
106+
107+
```go
108+
package main
109+
110+
import (
111+
"fmt"
112+
113+
boolutils "github.com/kashifkhan0771/utils/boolean"
114+
)
115+
116+
func main() {
117+
fmt.Println(boolutils.NoneTrue([]bool{false, false, false}))
118+
fmt.Println(boolutils.NoneTrue([]bool{false, true, false}))
119+
fmt.Println(boolutils.NoneTrue([]bool{}))
120+
}
121+
```
122+
123+
#### Output:
124+
125+
```
126+
true
127+
false
128+
true
129+
```
130+
131+
### Count the number of true values in a slice
132+
133+
```go
134+
package main
135+
136+
import (
137+
"fmt"
138+
139+
boolutils "github.com/kashifkhan0771/utils/boolean"
140+
)
141+
142+
func main() {
143+
fmt.Println(boolutils.CountTrue([]bool{true, false, true}))
144+
fmt.Println(boolutils.CountTrue([]bool{false, false, false}))
145+
fmt.Println(boolutils.CountTrue([]bool{}))
146+
}
147+
```
148+
149+
#### Output:
150+
151+
```
152+
2
153+
0
154+
0
155+
```
156+
157+
### Count the number of false values in a slice
158+
159+
```go
160+
package main
161+
162+
import (
163+
"fmt"
164+
165+
boolutils "github.com/kashifkhan0771/utils/boolean"
166+
)
167+
168+
func main() {
169+
fmt.Println(boolutils.CountFalse([]bool{true, false, true}))
170+
fmt.Println(boolutils.CountFalse([]bool{false, false, false}))
171+
fmt.Println(boolutils.CountFalse([]bool{}))
172+
}
173+
```
174+
175+
#### Output:
176+
177+
```
178+
1
179+
3
180+
0
181+
```
182+
183+
### Check if all values in a slice are equal
184+
185+
```go
186+
package main
187+
188+
import (
189+
"fmt"
190+
191+
boolutils "github.com/kashifkhan0771/utils/boolean"
192+
)
193+
194+
func main() {
195+
fmt.Println(boolutils.Equal(true, true, true))
196+
fmt.Println(boolutils.Equal(false, false, false))
197+
fmt.Println(boolutils.Equal(true, false, true))
198+
fmt.Println(boolutils.Equal())
199+
}
200+
```
201+
202+
#### Output:
203+
204+
```
205+
true
206+
true
207+
false
208+
false
209+
```
210+
211+
### Perform a logical AND operation on a slice
212+
213+
```go
214+
package main
215+
216+
import (
217+
"fmt"
218+
219+
boolutils "github.com/kashifkhan0771/utils/boolean"
220+
)
221+
222+
func main() {
223+
fmt.Println(boolutils.And([]bool{true, true, true}))
224+
fmt.Println(boolutils.And([]bool{true, false, true}))
225+
fmt.Println(boolutils.And([]bool{}))
226+
}
227+
```
228+
229+
#### Output:
230+
231+
```
232+
true
233+
false
234+
false
235+
```
236+
237+
### Perform a logical OR operation on a slice
238+
239+
```go
240+
package main
241+
242+
import (
243+
"fmt"
244+
245+
boolutils "github.com/kashifkhan0771/utils/boolean"
246+
)
247+
248+
func main() {
249+
fmt.Println(boolutils.Or([]bool{false, true, false}))
250+
fmt.Println(boolutils.Or([]bool{false, false, false}))
251+
fmt.Println(boolutils.Or([]bool{}))
252+
}
253+
```
254+
255+
#### Output:
256+
257+
```
258+
true
259+
false
260+
false
261+
```
262+
263+
---

boolean/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
### Boolean Functions
2+
3+
- **IsTrue**: Checks if the provided string represents a true value (e.g., "T", "1", "TRUE").
4+
- **Toggle**: Toggles the given boolean value, returning its negation. (e.g., `true` becomes `false` and vice versa).
5+
- **AllTrue**: Checks if all the values in a slice of booleans are `true`. Returns `false` if the slice is empty.
6+
- **AnyTrue**: Checks if at least one value in a slice of booleans is `true`. Returns `false` if the slice is empty.
7+
- **NoneTrue**: Checks if none of the values in a slice of booleans are `true`. Returns `true` if the slice is empty.
8+
- **CountTrue**: Counts the number of `true` values in a slice of booleans. Returns `0` for an empty slice.
9+
- **CountFalse**: Counts the number of `false` values in a slice of booleans. Returns `0` for an empty slice.
10+
- **Equal**: Checks if all the values in a variadic boolean argument are equal. Returns `true` if the slice contains only one or no elements.
11+
- **And**: Performs a logical AND operation on all the values in a slice of booleans. Returns `true` only if all values are `true`. Returns `false` for an empty slice.
12+
- **Or**: Performs a logical OR operation on all the values in a slice of booleans. Returns `true` if at least one value is `true`. Returns `false` for an empty slice.
13+
14+
## Examples:
15+
16+
For examples of each function, please checkout [EXAMPLES.md](/boolean/EXAMPLES.md)
17+
18+
---

caching/EXAMPLES.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
## Caching Function Examples
2+
3+
## `CacheWrapper`
4+
5+
### A non-thread-safe caching decorator
6+
7+
```go
8+
package main
9+
10+
import (
11+
"fmt"
12+
"math/big"
13+
"github.com/kashifkhan0771/utils/math"
14+
)
15+
16+
// Example function: Compute factorial
17+
func factorial(n int) *big.Int {
18+
result := big.NewInt(1)
19+
for i := 2; i <= n; i++ {
20+
result.Mul(result, big.NewInt(int64(i)))
21+
}
22+
return result
23+
}
24+
25+
func main() {
26+
cachedFactorial := utils.CacheWrapper(factorial)
27+
fmt.Println(cachedFactorial(10))
28+
}
29+
```
30+
31+
#### Output:
32+
33+
```
34+
3628800
35+
```
36+
37+
---
38+
39+
## SafeCacheWrapper
40+
41+
### A thread-safe caching decorator
42+
43+
```go
44+
package main
45+
46+
import (
47+
"fmt"
48+
"math/big"
49+
"github.com/kashifkhan0771/utils/math"
50+
)
51+
52+
// Example function: Compute factorial
53+
func factorial(n int) *big.Int {
54+
result := big.NewInt(1)
55+
for i := 2; i <= n; i++ {
56+
result.Mul(result, big.NewInt(int64(i)))
57+
}
58+
return result
59+
}
60+
61+
func main() {
62+
cachedFactorial := utils.SafeCacheWrapper(factorial)
63+
fmt.Println(cachedFactorial(10))
64+
}
65+
```
66+
67+
#### Output:
68+
69+
```
70+
3628800
71+
```

caching/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
### Caching
2+
3+
The caching package provides utilities for creating caching decorators to enhance the performance of functions by storing computed results. It includes both thread-safe and non-thread-safe implementations.
4+
5+
- **SafeCacheWrapper**: A thread-safe caching decorator that safely memoizes function results in concurrent environments.
6+
7+
- Uses `sync.Map` to ensure thread-safety
8+
- Caches all results indefinitely (no eviction)
9+
- Best suited for pure functions with limited input domains
10+
- Safe for concurrent access but may impact performance under high contention
11+
12+
- **CacheWrapper**: A non-thread-safe caching decorator that memoizes function results.
13+
- Caches all results indefinitely (no eviction)
14+
- Best suited for pure functions with limited input domains
15+
- Not safe for concurrent access
16+
- Use SafeCacheWrapper for concurrent scenarios
17+
18+
## Examples:
19+
20+
For examples of each function, please checkout [EXAMPLES.md](/caching/EXAMPLES.md)
21+
22+
---

0 commit comments

Comments
 (0)