Trait

com.eharmony.aloha.io.multiple

SequenceMultipleReadable

Related Doc: package multiple

Permalink

trait SequenceMultipleReadable[A <: ReadableSource, APP[_], B] extends AnyRef

This mixin provides a way to sequence the multiple readables whose return value is a container of some sort. For more info, see A Traversable from Haskell wiki or look up sequence, etc. For now, let's provide an example. Assuming we have an object StrToIntList that takes a string and produces a try of a list of integers by splitting and then parsing the ints. The result is either a success or failure but either way, the result is wrapped in a try container. Notice, that we included in the definition, with SequenceMultipleReadable[ReadableSource, Try, List[Int]]. This is all that is required at definition time to get the sequencing that we want.

The calling code (once the proper type class instances are in imported into the implicit scope) simply passes the container of objects to fromSequencedMultipleSources and the result is a Try[Stream[List[Int]]].

object StrToIntList
  extends ReadableByString[Try[List[Int]]]
  with MultipleAlohaReadable[Try[List[Int]]]
  with SequenceMultipleReadable[ReadableSource, Try, List[Int]] {

  def fromString(s: String): Try[List[Int]] = Try {
    if (s.trim == "") List.empty
    else s.trim.split("""\s*,\s*""", -1).map(_.toInt).toList
  }
}

// ======   Calling code   ==============================================================================

import scalaz.std.stream.streamInstance                  // Provides (non-strict) Traverse type class for Stream
import com.eharmony.aloha.util.TryScalazMonad   // Provides Applicative type class for Try

val in = Stream("1, 11, 111",
                "2, BAD",
                "3, 33, 333")

// This is the thing to notice:  we get a Try[Stream[List[Int]]] and not a Stream[Try[List[Int]]].  This is
// why this function is here.  It commutes the container types.  And because the Stream is non-strict, there is no
// attempt to parse the 3rd string "3, 33, 333" since an error occurred before getting to the third element.
// This behaviour can change for strict Traverse object (like the Traverse for List).
//
val out: Try[Stream[List[Int]]] = StrToIntList.fromSequencedMultipleSources(in)

// Test the error occurred.
val exceptionMsg = """For input string: "BAD""""
require(out.isFailure && exceptionMsg == out.failed.get.getMessage, "Should have failed.")
APP

A applicative functor container type (see Functors, Monads, Applicatives – can be so simple).

B

The container type in the output

Self Type
SequenceMultipleReadable[A, APP, B] with MultipleReadable[A, APP[B]]
Linear Supertypes
AnyRef, Any
Known Subclasses
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. SequenceMultipleReadable
  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. final def asInstanceOf[T0]: T0

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

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

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

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

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  9. def fromSequencedMultipleSources[T[_]](readableTypes: T[A])(implicit t: Traverse[T], ap: Applicative[APP]): APP[T[B]]

    Permalink

    Produce multiple outputs from multiple inputs and sequence the data so that we commute that container types.

    Produce multiple outputs from multiple inputs and sequence the data so that we commute that container types. For an example, see that trait documentation.

    T

    the Traverse container type

    readableTypes

    A container of readable sources to convert to outputs.

    t

    a Traverse type class instance

    ap

    an applicative function type class instance

  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