Package

com.github.tarao

bullet

Permalink

package bullet

Visibility
  1. Public
  2. All

Type Members

  1. trait HasA[Self, Other] extends AnyRef

    Permalink

    Defines an object mapping from Self to Other.

    Defines an object mapping from Self to Other.

    Implementing this interface provides a mapping from Self to Other, which can be used in HasA.Monadic[]. For instance, to declare that a Car has an Engine, implementing HasA[Car, Engine] and passing it to HasA.Monadic defines an accessor from a Car to an Engine.

    object HasEngine extends HasA[Car, Engine] {
      def map(from: Seq[Car]): Seq[Engine] =
        ??? // implement your mapping
    }
    
    // An accessor provider
    case class CarRelation(car: Car) {
      def toEngine = HasA.Monadic(car, HasEngine)
    }

    Although the defined accessor is for a single instance, the mapping defines a list-to-list mapping. This allows you to define an efficient way to retrieve multiple objects at once. The resulting list may NOT be in the same order as the input list or it may lack some elements correspond to those in the input, i.e., the mapping is actually a set-to-set mapping and the mapping function may not be total.

    In the above example, you may want to have an implicit conversion from a Car to CarRelation to allow accessing an Engine via a Car.

    implicit def carRelation(car: Car): CarRelation = CarRelation(car)
    
    // Single object mapping
    val car: Car = ???
    val engine: Option[Engine] = car.toEngine
    
    // Multiple object mapping
    val cars: Seq[Car] = ???
    val engines: Seq[Engine] = cars.map(_.toEngine)

    See the documentation of HasA.Monadic for the detail of the accessor behavior.

  2. trait Implicits extends AnyRef

    Permalink

    A trait to allow resolving monads on an implicit conversion.

  3. trait Join[R, Key, Left, Right] extends HasA[Left, Right]

    Permalink

    Defines an object join of Left and Right by Key resulting R.

    Defines an object join of Left and Right by Key resulting R.

    Implementing this interface provides a join of Left and Right to R with respect to Key, which can be used in Join.Monadic[]. For instance, to declare that a Car has an Engine with respect to CarId and they can be merged to CarWithEngine, implementing Join[CarWithEngine, Carid, Car, Engine] and passing it to Join.Monadic defines an accessor from a Car to a CarWithEngine.

    object HasEngine
        extends Join[CarWithEngine, CarId, Car, Engine] {
      def leftKey(car: Car): CarId = ???
      def rightKey(engine: Engine): CarId = ???
      def map(from: Seq[Car]): Seq[Engine] = ???
      def merge(car: Car, engine: Engine): CarWithEngine = ???
    }
    
    // An accessor provider
    case class CarRelation(car: Car) {
      def withEngine = Join.Monadic(car, HasEngine)
    }

    You need to implement four methods. First, leftKey() and rightKey() defines how to resolve a Key from a Left and a Right. Second, map() which maps values of Left to values of Right. Finally, merge() defines how to merge two objects into a value of R.

    Although the defined accessor is for a single instance, the mapping defines a list-to-list mapping. This allows you to define an efficient way to retrieve multiple objects at once. The resulting list may NOT be in the same order as the input list or it may lack some elements correspond to those in the input, i.e., the mapping is actually a set-to-set mapping and the mapping function may not be total. If a value lacks a mapping result, that value is excluded from the merged result. If there are multiple values in the resulting list with the same Key (which is resolved by rightKey), then a source value with the Key will be merged with one of them.

    In the above example, you may want to have an implicit conversion from a Car to an CarRelation to allow merging an Engine to a Car.

    implicit def carRelation(car: Car): CarRelation = CarRelation(car)
    
    // Single object mapping
    val car: Car = ???
    val enginedCar: Option[CarWithEngine] = car.withEngine
    
    // Multiple object mapping
    val cars: Seq[Car] = ???
    val enginedCars: Seq[CarWithEngine] = cars.map(_.withEngine)

    See the documentation of Join.Monadic for the detail of the accessor behavior.

  4. sealed trait Monad[R] extends AnyRef

    Permalink

    A monad to resolve a collection all together.

    A monad to resolve a collection all together.

    A monad instance can implicitly be converted into an option value of a resolved object. A list of monads can implicitly be converted into a list of resolved objects.

Value Members

  1. object HasA

    Permalink
  2. object Implicits extends Implicits

    Permalink

    Implicits to allow resolving monads on an implicit conversion.

  3. object Join

    Permalink
  4. object Monad

    Permalink

Ungrouped