com.eharmony.aloha.io.multiple

SequenceMultipleReadable

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
Learn more about member selection
Visibility
  1. Public
  2. All

Value Members

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

    Definition Classes
    AnyRef
  2. final def !=(arg0: Any): Boolean

    Definition Classes
    Any
  3. final def ##(): Int

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

    Definition Classes
    AnyRef
  5. final def ==(arg0: Any): Boolean

    Definition Classes
    Any
  6. final def asInstanceOf[T0]: T0

    Definition Classes
    Any
  7. def clone(): AnyRef

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

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

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

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

    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

    returns

  12. final def getClass(): Class[_]

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

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

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

    Definition Classes
    AnyRef
  16. final def notify(): Unit

    Definition Classes
    AnyRef
  17. final def notifyAll(): Unit

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

    Definition Classes
    AnyRef
  19. def toString(): String

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

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

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

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from AnyRef

Inherited from Any

Ungrouped