Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
/moPublic

🦄 Monads and popular FP abstractions, powered by Go 1.18+ Generics (Option, Result, Either...)

License

NotificationsYou must be signed in to change notification settings

samber/mo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tagGo VersionGoDocBuild StatusGo reportCoverageLicense

🦄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/lo: A Lodash-style Go library based on Go 1.18+ Generics
  • samber/do: A dependency injection toolkit based on Go 1.18+ Generics

Why this name?

I loveshort 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

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 importmo using:

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

Then use one of the helpers below:

option1:=mo.Some(42)// Some(42)option1.FlatMap(func (valueint)Option[int] {returnSome(value*2)    }).FlatMap(func (valueint)Option[int] {returnSome(value%2)    }).FlatMap(func (valueint)Option[int] {returnSome(value+21)    }).OrElse(1234)// 21option2:=mo.None[int]()// Noneoption2.OrElse(1234)// 1234option3:=option1.Match(func(iint) (int,bool) {// when value is presentreturni*2,true    },func() (int,bool) {// when value is absentreturn0,false    },)// Some(42)

More examples indocumentation.

Tips for lazy developers

I cannot recommend it, but in case you are too lazy for repeatingmo. 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 typeT. If value exists,Option is of typeSome. If the value is absent,Option is of typeNone.

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) Rdoc

Result[T any]

Result respresent a result of an action having one of the following output: success or failure. An instance ofResult is an instance of eitherOk orErr. It could be compared toEither[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) Rdoc
  • mo.Do[T any](fn func() T) (result mo.Result[T])doc

Either[L any, R any]

Either represents a value of 2 possible types. An instance ofEither is an instance of eitherA orB.

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) Rdoc

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

EitherX respresents a value of X possible types. For example, anEither3 value is eitherT1,T2 orT3.

Constructors:

  • mo.NewEitherXArgY()doc. Eg:
    • mo.NewEither3Arg1[A, B, C](A)
    • mo.NewEither3Arg2[A, B, C](B)
    • mo.NewEither3Arg3[A, B, C](C)
    • mo.NewEither4Arg1[A, B, C, D](A)
    • mo.NewEither4Arg2[A, B, C, D](B)
    • ...

Methods:

Future[T any]

Future represents a value which may or may not currently be available, but will be available at some point, or an exception if that value could not be made available.

Constructors:

  • mo.NewFuture()doc

Methods:

IO[T any]

IO represents a non-deterministic synchronous computation that can cause side effects, yields a value of typeR and never fails.

Constructors:

Methods:

IOEither[T any]

IO represents a non-deterministic synchronous computation that can cause side effects, yields a value of typeR and can fail.

Constructors:

  • mo.NewIOEither()doc
  • mo.NewIOEither1()doc
  • mo.NewIOEither2()doc
  • mo.NewIOEither3()doc
  • mo.NewIOEither4()doc
  • mo.NewIOEither5()doc

Methods:

Task[T any]

Task represents a non-deterministic asynchronous computation that can cause side effects, yields a value of typeR and never fails.

Constructors:

  • mo.NewTask()doc
  • mo.NewTask1()doc
  • mo.NewTask2()doc
  • mo.NewTask3()doc
  • mo.NewTask4()doc
  • mo.NewTask5()doc
  • mo.NewTaskFromIO()doc
  • mo.NewTaskFromIO1()doc
  • mo.NewTaskFromIO2()doc
  • mo.NewTaskFromIO3()doc
  • mo.NewTaskFromIO4()doc
  • mo.NewTaskFromIO5()doc

Methods:

TaskEither[T any]

TaskEither represents a non-deterministic asynchronous computation that can cause side effects, yields a value of typeR and can fail.

Constructors:

  • mo.NewTaskEither()doc
  • mo.NewTaskEitherFromIOEither()doc

Methods:

State[S any, A any]

State represents a function(S) -> (A, S), whereS is state,A is result.

Constructors:

  • mo.NewState()doc
  • mo.ReturnState()doc

Methods:

Foldable[T, U]

Foldable represents a type that can be folded into a single value based on its state.

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

🛩 Benchmark

// @TODO

This library does not usereflect package. We don't expect overhead.

🤝 Contributing

Don't hesitate ;)

With Docker

docker-compose run --rm dev

Without Docker

# Install some dev dependenciesmake tools# Run testsmaketest# ormake watch-test

👤 Contributors

Contributors

💫 Show your support

Give a ⭐️ if this project helped you!

GitHub Sponsors

📝 License

Copyright © 2022Samuel Berthe.

This project isMIT licensed.


[8]ページ先頭

©2009-2025 Movatter.jp