Object

com.github.tarao.bullet.Join

Monadic

Related Doc: package Join

Permalink

object Monadic

A factory of a monad for joining object field.

To initially create a monad instance, use Join.Monadic.apply() together with a source object of the mapping and an instance of Join[] which describes a field mapping and a way of joining fields. For instance, a monad for joining a Car and an Engine into a CarWithEngine with respect to CarId described by a Join[CarWithEngine, CarId, Car, Engine] can be generated as the following.

type HasEngine = Join[CarWithEngine, CarId, Car, Engine]
val hasEngine: HasEngine = ???
val car: Car = ???
val m = Join.Monadic(car, hasEngine)

A monad can implicitly be converted to an option value of the target type of the join by internally invoking methods of Join[] in the conversion.

val enginedCar: Option[CarWithEngine] = m

Since it is a monad, the value can mapped to another value in a for comprehension. (Assume that a CarWithEngine has engine field of type Engine in the next example.)

val engine: Option[Engine] = for {
  car <- m
} yield (car.engine)

Note that the car in the above example, i.e., the argument of a function passed to flatMap() or map() of the monad, is a CarWithEngine. You don't need to look inside the option value in this way. If the resulting option value is None, i.e., there is no target value to join, then the body of the function passed to flatMap() or map() never be invoked.

A list of monadic values can also be converted to a list of the target list type.

val cars: Seq[Car] = ???
val ms = cars.map { car => Join.Monadic(car, hasEngine) }
val enginedCars: Seq[CarWithEngine] = ms

In this case, cars are mapped to enginedCars by methods in Join[] all at once. You can of course use a for comprehension for further operations.

val engines: Seq[Engine] = ms.map { m => for {
  car <- m
} yield (car.engine) }

If you aim to achieve an efficient object join by converting a list value at once via methods in Join --- this is the most common reason for using the monadc object join ---, DO NOT trigger the implicit conversion inside the loop.

val engines: Seq[Engine] = ms.map { m =>
  val e: Option[Engine] =
    /* !!! methods in Join[] are called here !!! */ for {
      car <- m
    } yield (car.engine)
  e
}.flatten

It is not difficult to notice the misuse in this way since the object must be recieved as an Option[Engine] not an Engine to trigger the conversion for each element.

Source
Join.scala
Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. Monadic
  2. AnyRef
  3. Any
  1. Hide All
  2. Show all
Visibility
  1. Public
  2. All

Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  4. def apply[R, Key, Left, Right](value: Left, join: Join[R, Key, Left, Right])(implicit fallback: Fallback[Right]): Monad.Resolve[R, Left]

    Permalink
  5. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  6. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  7. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  8. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  9. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  10. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  11. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  12. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  13. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  14. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  15. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  16. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  17. def toString(): String

    Permalink
    Definition Classes
    AnyRef → Any
  18. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  19. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  20. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from AnyRef

Inherited from Any

Ungrouped