-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
114 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
core/src/test/scala/org/nevair/containers/ContainerTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package org.nevair.containers | ||
|
||
import org.scalatest.{FlatSpec, Matchers} | ||
import Container.ops._ | ||
|
||
import scala.language.higherKinds | ||
|
||
class ContainerTest extends FlatSpec with Matchers { | ||
behavior of "List Container" | ||
|
||
it should "call map on list" in { | ||
def mapIt[C[_]: Container](lContainer: C[Int])(f: Int => String): C[String] = lContainer.map(f) | ||
|
||
val l = List(1, 2, 3, 4) | ||
|
||
mapIt(l)(_.toString) should contain theSameElementsInOrderAs l.map(_.toString) | ||
} | ||
|
||
it should "call flatMap on list" in { | ||
def flatMapIt[C[_]: Container](lContainer: C[(Int, Int)]): C[Int] = lContainer.flatMap(t => Seq(t._1, t._2)) | ||
|
||
val l = List((1, 2), (3, 4)) | ||
|
||
flatMapIt(l) should contain theSameElementsInOrderAs l.flatMap(t => Seq(t._1, t._2)) | ||
} | ||
|
||
it should "do nothing when calling repartition on list" in { | ||
def repartitionIt[C[_]: Container](lContainer: C[Int]): C[Int] = lContainer.repartition(1000) | ||
|
||
val l = List(1, 2, 3, 4) | ||
|
||
repartitionIt(l) should contain theSameElementsInOrderAs l | ||
} | ||
|
||
it should "count all elements without filter" in { | ||
def countItAll[C[_]: Container](lContainer: C[Int]): Long = lContainer.count(_ => true) | ||
|
||
val l = List(1, 2, 3, 4) | ||
|
||
countItAll(l) should be(l.size) | ||
} | ||
|
||
it should "count elements fulfilling filter" in { | ||
def countIt[C[_]: Container](lContainer: C[Int])(p: Int => Boolean): Long = lContainer.count(p) | ||
|
||
val l = List(1, 2, 3, 4) | ||
|
||
countIt(l)(_ % 2 == 0) should be(l.count(_ % 2 == 0)) | ||
} | ||
|
||
it should "call reduce on list" in { | ||
def reduceIt[C[_]: Container](lContainer: C[Int])(f: (Int, Int) => Int): Int = lContainer.reduce(f) | ||
|
||
val l = List(1, 2, 3, 4) | ||
|
||
reduceIt(l)(_ + _) should be(l.sum) | ||
} | ||
|
||
behavior of "List PairContainer" | ||
|
||
it should "reduce by key properly" in { | ||
def sumIt[C[_]: PairContainer](lContainer: C[(String, Int)]): C[(String, Int)] = lContainer.reduceByKey(_ + _) | ||
|
||
val l = List(("a", 1), ("b", 1), ("a", 1), ("c", 1)) | ||
|
||
sumIt(l) should contain theSameElementsAs List(("a", 2), ("b", 1), ("c", 1)) | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
example/src/main/scala/org/nevair/containers/example/ExampleContainer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.nevair.containers.example | ||
|
||
import org.nevair.containers.Container | ||
|
||
import scala.reflect.ClassTag | ||
|
||
object ExampleContainer { | ||
implicit object SetContainer extends Container[Set] { | ||
override def map[A, B: ClassTag](c: Set[A])(f: (A) => B): Set[B] = c.map(f) | ||
|
||
override def repartition[A](c: Set[A])(repNumber: Int): Set[A] = c | ||
|
||
override def flatMap[A, B: ClassTag](c: Set[A])(f: (A) => TraversableOnce[B]): Set[B] = c.flatMap(f) | ||
|
||
override def filter[A](c: Set[A])(p: (A) => Boolean): Set[A] = c.filter(p) | ||
|
||
override def count[A](c: Set[A])(p: (A) => Boolean = (_: A) => true): Long = c.count(p).toLong | ||
|
||
override def reduce[A](c: Set[A])(f: (A, A) => A): A = c.reduce(f) | ||
} | ||
|
||
implicit object VectorContainer extends Container[Vector] { | ||
override def map[A, B: ClassTag](c: Vector[A])(f: (A) => B): Vector[B] = c.map(f) | ||
|
||
override def repartition[A](c: Vector[A])(repNumber: Int): Vector[A] = c | ||
|
||
override def flatMap[A, B: ClassTag](c: Vector[A])(f: (A) => TraversableOnce[B]): Vector[B] = c.flatMap(f) | ||
|
||
override def filter[A](c: Vector[A])(f: (A) => Boolean): Vector[A] = c.filter(f) | ||
|
||
override def count[A](c: Vector[A])(p: (A) => Boolean = (_: A) => true): Long = c.count(p).toLong | ||
|
||
override def reduce[A](c: Vector[A])(f: (A, A) => A): A = c.reduce(f) | ||
} | ||
} |
6 changes: 3 additions & 3 deletions
6
...oam/shaish/containers/example/Logic.scala → ...org/nevair/containers/example/Logic.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
...shaish/containers/example/LogicTest.scala → ...nevair/containers/example/LogicTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...haish/containers/spark/RDDContainer.scala → ...air/containers/spark/SparkContainer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters