Skip to content

Commit

Permalink
fix: reorder fields to save more memory. Solves samber#1
Browse files Browse the repository at this point in the history
  • Loading branch information
samber committed May 20, 2022
1 parent 0311c49 commit edcae87
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# mo - Monads

[![tag](https://img.shields.io/github/tag/samber/mo.svg)](https://github.com/samber/mo/releases)
[![codecov](https://codecov.io/gh/samber/mo/branch/master/graph/badge.svg)](https://codecov.io/gh/samber/mo)
![Build Status](https://github.com/samber/mo/actions/workflows/go.yml/badge.svg)
[![GoDoc](https://godoc.org/github.com/samber/mo?status.svg)](https://pkg.go.dev/github.com/samber/mo)
![Build Status](https://github.com/samber/mo/actions/workflows/go.yml/badge.svg)
[![Go report](https://goreportcard.com/badge/github.com/samber/mo)](https://goreportcard.com/report/github.com/samber/mo)
[![codecov](https://codecov.io/gh/samber/mo/branch/master/graph/badge.svg)](https://codecov.io/gh/samber/mo)

🦄 **`samber/mo` brings monads and populars FP abstractions to Go projects. `samber/mo` uses the recent Go 1.18+ Generics.**

Expand Down Expand Up @@ -86,11 +86,11 @@ option3 := option1.Match(
func(i int) (int, bool) {
// when value is present
return i * 2, true
},
},
func() (int, bool) {
// when value is absent
return 0, false
}
}
)
// Some(42)
```
Expand Down Expand Up @@ -128,7 +128,7 @@ Methods:

### Result[T any]

`Result` respresent a result of an action having one of the following output: success or failure. An instance of `Result` is an instance of either `Ok` or `Err`.
`Result` respresent a result of an action having one of the following output: success or failure. An instance of `Result` is an instance of either `Ok` or `Err`. It could be compared to `Either[error, T]`.

Constructors:

Expand Down
10 changes: 5 additions & 5 deletions either.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@ var eitherMissingRightValue = fmt.Errorf("no such Right value")
// Left builds the left side of the Either struct, as opposed to the Right side.
func Left[L any, R any](value L) Either[L, R] {
return Either[L, R]{
left: value,
isLeft: true,
isRight: false,
left: value,
}
}

// Right builds the right side of the Either struct, as opposed to the Left side.
func Right[L any, R any](value R) Either[L, R] {
return Either[L, R]{
right: value,
isLeft: false,
isRight: true,
right: value,
}
}

// Either respresents a value of 2 possible types.
// An instance of Either is an instance of either A or B.
type Either[L any, R any] struct {
left L
right R

isLeft bool
isRight bool

left L
right R
}

// IsLeft returns true if Either is an instance of Left.
Expand Down
4 changes: 2 additions & 2 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ var optionNoSuchElement = fmt.Errorf("no such element")
// Some builds an Option when value is present.
func Some[T any](value T) Option[T] {
return Option[T]{
value: value,
isPresent: true,
value: value,
}
}

Expand All @@ -29,8 +29,8 @@ func TupleToOption[T any](v T, ok bool) Option[T] {
// Option is a container for an optional value of type T. If value exists, Option is
// of type Some. If the value is absent, Option is of type None.
type Option[T any] struct {
value T
isPresent bool
value T
}

// IsPresent returns true when value is absent.
Expand Down
3 changes: 2 additions & 1 deletion result.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ func TupleToResult[T any](v T, err error) Result[T] {
// Result respresent a result of an action having one
// of the following output: success or failure.
// An instance of Result is an instance of either Ok or Err.
// It could be compared to `Either[error, T]`.
type Result[T any] struct {
isErr bool
value T
err error
isErr bool
}

// IsOk returns true when value is valid.
Expand Down

0 comments on commit edcae87

Please sign in to comment.