Skip to content

Commit

Permalink
doc(examples): adding go playground examples (string, map, slice, con…
Browse files Browse the repository at this point in the history
…dition, math)
  • Loading branch information
samber committed Oct 3, 2022
1 parent 4fecc90 commit cc3aa22
Show file tree
Hide file tree
Showing 13 changed files with 1,171 additions and 35 deletions.
124 changes: 108 additions & 16 deletions README.md

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions condition.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lo

// Ternary is a 1 line if/else statement.
// Play: https://go.dev/play/p/t-D7WBL44h2
func Ternary[T any](condition bool, ifOutput T, elseOutput T) T {
if condition {
return ifOutput
Expand All @@ -10,6 +11,7 @@ func Ternary[T any](condition bool, ifOutput T, elseOutput T) T {
}

// TernaryF is a 1 line if/else statement whose options are functions
// Play: https://go.dev/play/p/AO4VW20JoqM
func TernaryF[T any](condition bool, ifFunc func() T, elseFunc func() T) T {
if condition {
return ifFunc()
Expand All @@ -24,6 +26,7 @@ type ifElse[T any] struct {
}

// 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}
Expand All @@ -34,6 +37,7 @@ func If[T any](condition bool, result T) *ifElse[T] {
}

// 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}
Expand All @@ -44,6 +48,7 @@ func IfF[T any](condition bool, resultF func() T) *ifElse[T] {
}

// 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
Expand All @@ -54,6 +59,7 @@ func (i *ifElse[T]) ElseIf(condition bool, result T) *ifElse[T] {
}

// 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()
Expand All @@ -64,6 +70,7 @@ func (i *ifElse[T]) ElseIfF(condition bool, resultF func() T) *ifElse[T] {
}

// Else.
// Play: https://go.dev/play/p/WSw3ApMxhyW
func (i *ifElse[T]) Else(result T) T {
if i.done {
return i.result
Expand All @@ -73,6 +80,7 @@ func (i *ifElse[T]) Else(result T) T {
}

// ElseF.
// Play: https://go.dev/play/p/WSw3ApMxhyW
func (i *ifElse[T]) ElseF(resultF func() T) T {
if i.done {
return i.result
Expand All @@ -88,6 +96,7 @@ type switchCase[T comparable, R any] struct {
}

// 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

Expand All @@ -99,6 +108,7 @@ func Switch[T comparable, R any](predicate T) *switchCase[T, R] {
}

// 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
Expand All @@ -109,6 +119,7 @@ func (s *switchCase[T, R]) Case(val T, result R) *switchCase[T, R] {
}

// 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()
Expand All @@ -119,6 +130,7 @@ func (s *switchCase[T, R]) CaseF(val T, cb func() R) *switchCase[T, R] {
}

// Default.
// Play: https://go.dev/play/p/TGbKUMAeRUd
func (s *switchCase[T, R]) Default(result R) R {
if !s.done {
s.result = result
Expand All @@ -128,6 +140,7 @@ func (s *switchCase[T, R]) Default(result R) R {
}

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

0 comments on commit cc3aa22

Please sign in to comment.