Giter Club home page Giter Club logo

monad-transformers's Introduction

Monad transformers

This is a port of Hamsters monad transformers to Scala 2.13.

Example : combine Future and Option types then make it work in a for comprehension. More information on why it's useful here.

FutureOption

import io.github.hamsters.FutureOption
import io.github.hamsters.MonadTransformers._
//import your execution context here too, for example import scala.concurrent.ExecutionContext.Implicits.global

def foa: Future[Option[String]] = Future(Some("a"))
def fob(a: String): Future[Option[String]] = Future(Some(a+"b"))

val composedAB: Future[Option[String]] = for {
  a <- FutureOption(foa)
  ab <- FutureOption(fob(a))
} yield ab

FutureTry

import io.github.hamsters.FutureTry
import io.github.hamsters.MonadTransformers._
//import your execution context here too, for example import scala.concurrent.ExecutionContext.Implicits.global

def foa: Future[Try[String]] = Future(Try("a"))
def fob(a: String): Future[Try[String]] = Future(Try(a+"b"))

val composedAB: Future[Try[String]] = for {
  a <- FutureTry(foa)
  ab <- FutureTry(fob(a))
} yield ab

FutureEither

import io.github.hamsters.FutureEither
import io.github.hamsters.MonadTransformers._
//import your execution context here too, for example import scala.concurrent.ExecutionContext.Implicits.global

def fea: Future[Either[String, Int]] = Future(Right(1))
def feb(a: Int): Future[Either[String, Int]] = Future(Right(a+2))

val composedAB: Future[Either[String, Int]] = for {
  a <- FutureEither(fea)
  ab <- FutureEither(feb(a))
} yield ab

OptionT and EitherT

If you use other combinations of F[Option[A]] or F[Either[L,R]], you can bring your own Monad implicit instance and work with OptionT or EitherT. Future and Option instances are provided by default.


Install

Add this to you build.sbt :

resolvers += Resolver.bintrayRepo("loicdescotte", "Hamsters") 
libraryDependencies += "io.github.scala-hamsters" %% "monad-transformers" % "1.0.0"

monad-transformers's People

Contributors

loicdescotte avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.