forked from twitter/algebird
-
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
Sam Ritchie
committed
Feb 14, 2013
1 parent
7f3d1e6
commit 8e38487
Showing
6 changed files
with
102 additions
and
145 deletions.
There are no files selected for viewing
34 changes: 0 additions & 34 deletions
34
algebird-util/src/main/scala/com/twitter/algebird/util/Algebras.scala
This file was deleted.
Oops, something went wrong.
54 changes: 0 additions & 54 deletions
54
algebird-util/src/main/scala/com/twitter/algebird/util/FutureAlgebra.scala
This file was deleted.
Oops, something went wrong.
56 changes: 0 additions & 56 deletions
56
algebird-util/src/main/scala/com/twitter/algebird/util/TryAlgebra.scala
This file was deleted.
Oops, something went wrong.
43 changes: 43 additions & 0 deletions
43
algebird-util/src/main/scala/com/twitter/algebird/util/UtilAlgebras.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,43 @@ | ||
/* | ||
* Copyright 2013 Twitter Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. You may obtain | ||
* a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.twitter.algebird.util | ||
|
||
import com.twitter.algebird._ | ||
import com.twitter.util.{ Future, Return, Try } | ||
|
||
object UtilAlgebras { | ||
implicit val futureMonad: Monad[Future] = new Monad[Future] { | ||
def apply[T](v: T) = Future.value(v); | ||
def flatMap[T, U](m: Future[T])(fn: T => Future[U]) = m.flatMap(fn) | ||
} | ||
implicit val tryMonad: Monad[Try] = new Monad[Try] { | ||
def apply[T](v: T) = Return(v); | ||
def flatMap[T,U](m: Try[T])(fn: T => Try[U]) = m.flatMap(fn) | ||
} | ||
|
||
implicit def futureSemigroup[T:Semigroup]: Semigroup[Future[T]] = new MonadSemigroup[T, Future] | ||
implicit def futureMonoid[T:Monoid]: Monoid[Future[T]] = new MonadMonoid[T, Future] | ||
implicit def futureGroup[T:Group]: Group[Future[T]] = new MonadGroup[T, Future] | ||
implicit def futureRing[T:Ring]: Ring[Future[T]] = new MonadRing[T, Future] | ||
implicit def futureField[T:Field]: Field[Future[T]] = new MonadField[T, Future] | ||
|
||
implicit def trySemigroup[T:Semigroup]: Semigroup[Try[T]] = new MonadSemigroup[T, Try] | ||
implicit def tryMonoid[T:Monoid]: Monoid[Try[T]] = new MonadMonoid[T, Try] | ||
implicit def tryGroup[T:Group]: Group[Try[T]] = new MonadGroup[T, Try] | ||
implicit def tryRing[T:Ring]: Ring[Try[T]] = new MonadRing[T, Try] | ||
implicit def tryField[T:Field]: Field[Try[T]] = new MonadField[T, Try] | ||
} |
47 changes: 47 additions & 0 deletions
47
algebird-util/src/test/scala/com/twitter/algebird/util/UtilAlgebraProperties.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,47 @@ | ||
/* | ||
* Copyright 2013 Twitter Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. You may obtain | ||
* a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.twitter.algebird.util | ||
|
||
import com.twitter.algebird.MonadLaws.monadLaws | ||
import com.twitter.util.{ Future, Throw, Return, Try } | ||
import org.scalacheck.{ Arbitrary, Properties } | ||
|
||
import Arbitrary.arbitrary | ||
|
||
object UtilAlgebraProperties extends Properties("UtilAlgebras") { | ||
import UtilAlgebras._ | ||
|
||
def toOption[T](f: Future[T]): Option[T] = | ||
if (f.isReturn) Some(f.get) else None | ||
|
||
implicit def futureA[T: Arbitrary]: Arbitrary[Future[T]] = | ||
Arbitrary { | ||
arbitrary[T].map { l => Future.value(l) } | | ||
Future.exception(new RuntimeException("fail!")) | ||
} | ||
|
||
implicit def returnA[T: Arbitrary]: Arbitrary[Try[T]] = | ||
Arbitrary { | ||
arbitrary[T].map { l => Return(l) } | | ||
Throw(new RuntimeException("fail!")) | ||
} | ||
|
||
property("futureMonad") = monadLaws[Future, Int, String, Long] { (f1, f2) => | ||
toOption(f1) == toOption(f2) | ||
} | ||
property("tryMonad") = monadLaws[Try, Int, String, Long]() | ||
} |
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