MCPcopy
hub / github.com/samber/mo

github.com/samber/mo @v1.17.0 sqlite

repository ↗ · DeepWiki ↗ · release v1.17.0 ↗
546 symbols 1,394 edges 44 files 311 documented · 57%
README

mo - Monads

tag Go Version GoDoc Build Status Go report Coverage License

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

Inspired by:

  • Scala
  • Rust
  • FP-TS

See also:

  • samber/ro: Reactive Programming for Go: declarative and composable API for event-driven applications
  • samber/lo: A Lodash-style Go library based on Go 1.18+ Generics
  • samber/do: A dependency injection toolkit based on Go 1.18+ Generics
  • samber/cc-skills-golang: AI Agent Skills for Golang

💖 Sponsored by:

  <img width="200" alt="dbos" src="https://github.com/user-attachments/assets/d583cb62-7735-4d3c-beb7-e6ef1a5faf49" />






  DBOS - Durable workflow orchestration library for Go


image

Why this name?

I love short name for such utility library. This name is similar to "Monad Go" and no Go package uses this name.

💡 Features

We currently support the following data types:

  • Option[T] (Maybe)
  • Result[T]
  • Either[A, B]
  • EitherX[T1, ..., TX] (With X between 3 and 5)
  • Future[T]
  • IO[T]
  • IOEither[T]
  • Task[T]
  • TaskEither[T]
  • State[S, A]

🚀 Install

go get github.com/samber/mo@v1

# AI Agent Skill
npx skills add https://github.com/samber/cc-skills-golang --skill golang-samber-mo

This library is v1 and follows SemVer strictly.

No breaking changes will be made to exported APIs before v2.0.0.

This library has no dependencies except the Go std lib.

💡 Quick start

You can import mo using:

import (
    "github.com/samber/mo"
)

Quick example using the option sub-package Pipe3 to compose transformations:

import (
    "github.com/samber/mo"
    "github.com/samber/mo/option"
)

out := option.Pipe3(
    mo.Some(21),
    option.Map(func(v int) int { return v * 2 }),
    option.FlatMap(func(v int) mo.Option[int] { return mo.None[int]() }),
    option.Map(func(v int) int { return v + 21 }),
)
// out == None[int]

Then use one of the helpers below:

option1 := mo.Some(42)
// Some(42)

option1.
    FlatMap(func (value int) Option[int] {
        return Some(value*2)
    }).
    FlatMap(func (value int) Option[int] {
        return Some(value%2)
    }).
    FlatMap(func (value int) Option[int] {
        return Some(value+21)
    }).
    OrElse(1234)
// 21

option2 := mo.None[int]()
// None

option2.OrElse(1234)
// 1234

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)

More examples in documentation.

Tips for lazy developers

I cannot recommend it, but in case you are too lazy for repeating mo. everywhere, you can import the entire library into the namespace.

import (
    . "github.com/samber/mo"
)

I take no responsibility on this junk. 😁 💩

🤠 Documentation and examples

GoDoc: https://godoc.org/github.com/samber/mo

Option[T any]

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.

Implements: - mo.Foldable[T, U]

Constructors:

Methods:

Other:

  • mo.Fold[T, U, R any](f Foldable[T, U], successFunc func(U) R, failureFunc func(T) R) R doc

Sub-package option (transformations and pipes):

  • option.Map()() doc
  • option.FlatMap()() doc
  • option.Match()() doc
  • option.FlatMatch()() doc
  • option.Pipe1..Pipe10() docs

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. It could be compared to Either[error, T].

Implements: - mo.Foldable[T, U]

Constructors:

Methods:

Other:

  • mo.Fold[T, U, R any](f Foldable[T, U], successFunc func(U) R, failureFunc func(T) R) R doc
  • mo.Do[T any](fn func() T) (result mo.Result[T]) doc

Sub-package result (transformations and pipes):

  • result.Map()() doc
  • result.FlatMap()() doc
  • result.Match()() doc
  • result.FlatMatch()() doc
  • result.Pipe1..Pipe10() docs

Either[L any, R any]

Either represents a value of 2 possible types. An instance of Either is an instance of either A or B.

Implements: - mo.Foldable[T, U]

Constructors:

  • mo.Left() doc
  • mo.Right() doc

Methods:

  • .IsLeft() doc
  • .IsRight() doc
  • .Left() doc
  • .Right() doc
  • .MustLeft() doc
  • .MustRight() doc
  • .Unpack() doc
  • .LeftOrElse() doc
  • .RightOrElse() doc
  • .LeftOrEmpty() doc
  • .RightOrEmpty() doc
  • .Swap() doc
  • .ForEach() doc
  • .Match() doc
  • .MapLeft() doc
  • .MapRight() doc

Other:

  • mo.Fold[T, U, R any](f Foldable[T, U], successFunc func(U) R, failureFunc func(T) R) R doc

Sub-package either (transformations and pipes):

  • either.MapLeft()() doc
  • either.MapRight()() doc
  • either.Match()() doc
  • either.Swap()() doc
  • either.Pipe1..Pipe10() docs

EitherX[T1, ..., TX] (With X between 3 and 5)

EitherX respresents a value of X possible types. For example, an Either3 value is either T1, T2 or T3.

Constructors:

  • mo.NewEitherXArgY() doc. Eg:
  • mo.NewEither3Arg1[A, B, C](A)
  • mo.NewEither3Arg2[A, B, C](B)
  • `mo.

Extension points exported contracts — how you extend this code

Foldable (Interface)
Foldable represents a type that can be folded into a single value based on its state. - T: the type of the value in the
fold.go
Monoid (Interface)
(no doc)
typeclass/monoid.go
Filterable (Interface)
(no doc)
typeclass/filterable.go
Foldable (Interface)
(no doc)
typeclass/foldable.go
Monadic (Interface)
(no doc)
typeclass/monadic.go

Core symbols most depended-on inside this repo

Some
called by 31
option.go
Error
called by 30
result.go
OrEmpty
called by 30
result.go
Ok
called by 26
result.go
Get
called by 19
state.go
IsError
called by 15
result.go
OrElse
called by 15
result.go
Right
called by 15
either.go

Shape

Function 277
Method 217
Struct 28
FuncType 18
Interface 6

Languages

Go100%

Modules by API surface

either5.go43 symbols
option.go39 symbols
either4.go36 symbols
option_example_test.go35 symbols
result_example_test.go33 symbols
either_example_test.go30 symbols
either3.go29 symbols
either.go26 symbols
result.go25 symbols
task.go24 symbols
types.go18 symbols
io_either.go18 symbols

Dependencies from manifests, versioned

github.com/davecgh/go-spewv1.1.2-0.20180830191 · 1×
github.com/kr/prettyv0.3.1 · 1×
github.com/pmezard/go-difflibv1.0.1-0.20181226105 · 1×
github.com/rogpeppe/go-internalv1.12.0 · 1×
gopkg.in/check.v1v1.0.0-2020113013444 · 1×
gopkg.in/yaml.v3v3.0.1 · 1×

For agents

$ claude mcp add mo \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact