Skip to content

Commit

Permalink
ref(lo): trim lo (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
plastikfan committed Aug 12, 2024
1 parent c151c7c commit 48ca4e5
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 1,258 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"astrolib",
"Berthe",
"bodyclose",
"Clonable",
"cmds",
"coverpkg",
"coverprofile",
Expand Down
5 changes: 5 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,15 @@ tasks:

cover:
cmds:
- mkdir -p ./coverage
- mkdir -p ./internal/ants/coverage
- mkdir -p ./locale/coverage
- ginkgo --json-report
./ginkgo.report
-coverpkg=./...
-coverprofile=./coverage/coverage.out -r
- go tool cover -html=./coverage/coverage.out -o ./coverage/coverage.html
- open ./coverage/coverage.html

# === i18n =================================================

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
github.com/onsi/ginkgo/v2 v2.20.0
github.com/onsi/gomega v1.34.1
github.com/snivilised/extendio v0.7.0
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56
)

require (
Expand All @@ -20,6 +19,7 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/samber/lo v1.39.0 // indirect
github.com/snivilised/lorax v0.6.1 // indirect
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sys v0.23.0 // indirect
golang.org/x/text v0.17.0 // indirect
Expand Down
166 changes: 83 additions & 83 deletions internal/lo/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,131 +20,131 @@ func TernaryF[T any](condition bool, ifFunc, elseFunc func() T) T {
return elseFunc()
}

type IfElse[T any] struct {
result T
done bool
}
// type IfElse[T any] struct {
// result T
// done bool
// }

// If
// Play: https://go.dev/play/p/WSw3ApMxhyW
func If[T any](condition bool, result T) *IfElse[T] {
if condition {
return &IfElse[T]{result, true}
}
// func If[T any](condition bool, result T) *IfElse[T] {
// if condition {
// return &IfElse[T]{result, true}
// }

var t T
return &IfElse[T]{t, false}
}
// var t T
// return &IfElse[T]{t, false}
// }

// IfF
// Play: https://go.dev/play/p/WSw3ApMxhyW
func IfF[T any](condition bool, resultF func() T) *IfElse[T] {
if condition {
return &IfElse[T]{resultF(), true}
}
// func IfF[T any](condition bool, resultF func() T) *IfElse[T] {
// if condition {
// return &IfElse[T]{resultF(), true}
// }

var t T
return &IfElse[T]{t, false}
}
// var t T
// return &IfElse[T]{t, false}
// }

// ElseIf
// Play: https://go.dev/play/p/WSw3ApMxhyW
func (i *IfElse[T]) ElseIf(condition bool, result T) *IfElse[T] {
if !i.done && condition {
i.result = result
i.done = true
}
// func (i *IfElse[T]) ElseIf(condition bool, result T) *IfElse[T] {
// if !i.done && condition {
// i.result = result
// i.done = true
// }

return i
}
// return i
// }

// ElseIfF
// Play: https://go.dev/play/p/WSw3ApMxhyW
func (i *IfElse[T]) ElseIfF(condition bool, resultF func() T) *IfElse[T] {
if !i.done && condition {
i.result = resultF()
i.done = true
}
// func (i *IfElse[T]) ElseIfF(condition bool, resultF func() T) *IfElse[T] {
// if !i.done && condition {
// i.result = resultF()
// i.done = true
// }

return i
}
// return i
// }

// Else
// Play: https://go.dev/play/p/WSw3ApMxhyW
func (i *IfElse[T]) Else(result T) T {
if i.done {
return i.result
}
// func (i *IfElse[T]) Else(result T) T {
// if i.done {
// return i.result
// }

return result
}
// return result
// }

// ElseF
// Play: https://go.dev/play/p/WSw3ApMxhyW
func (i *IfElse[T]) ElseF(resultF func() T) T {
if i.done {
return i.result
}
// func (i *IfElse[T]) ElseF(resultF func() T) T {
// if i.done {
// return i.result
// }

return resultF()
}
// return resultF()
// }

type SwitchCase[T comparable, R any] struct {
predicate T
result R
done bool
}
// type SwitchCase[T comparable, R any] struct {
// predicate T
// result R
// done bool
// }

// Switch is a pure functional switch/case/default statement.
// Play: https://go.dev/play/p/TGbKUMAeRUd
func Switch[T comparable, R any](predicate T) *SwitchCase[T, R] {
var result R
// func Switch[T comparable, R any](predicate T) *SwitchCase[T, R] {
// var result R

return &SwitchCase[T, R]{
predicate,
result,
false,
}
}
// return &SwitchCase[T, R]{
// predicate,
// result,
// false,
// }
// }

// Case
// Play: https://go.dev/play/p/TGbKUMAeRUd
func (s *SwitchCase[T, R]) Case(val T, result R) *SwitchCase[T, R] {
if !s.done && s.predicate == val {
s.result = result
s.done = true
}
// func (s *SwitchCase[T, R]) Case(val T, result R) *SwitchCase[T, R] {
// if !s.done && s.predicate == val {
// s.result = result
// s.done = true
// }

return s
}
// return s
// }

// CaseF
// Play: https://go.dev/play/p/TGbKUMAeRUd
func (s *SwitchCase[T, R]) CaseF(val T, cb func() R) *SwitchCase[T, R] {
if !s.done && s.predicate == val {
s.result = cb()
s.done = true
}
// func (s *SwitchCase[T, R]) CaseF(val T, cb func() R) *SwitchCase[T, R] {
// if !s.done && s.predicate == val {
// s.result = cb()
// s.done = true
// }

return s
}
// return s
// }

// Default
// Play: https://go.dev/play/p/TGbKUMAeRUd
func (s *SwitchCase[T, R]) Default(result R) R {
if !s.done {
s.result = result
}
// func (s *SwitchCase[T, R]) Default(result R) R {
// if !s.done {
// s.result = result
// }

return s.result
}
// return s.result
// }

// DefaultF
// Play: https://go.dev/play/p/TGbKUMAeRUd
func (s *SwitchCase[T, R]) DefaultF(cb func() R) R {
if !s.done {
s.result = cb()
}
// func (s *SwitchCase[T, R]) DefaultF(cb func() R) R {
// if !s.done {
// s.result = cb()
// }

return s.result
}
// return s.result
// }
Loading

0 comments on commit 48ca4e5

Please sign in to comment.