This file contains hidden or 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
scala> import scalaz._, Scalaz._ | |
import scalaz._ | |
import Scalaz._ | |
scala> def str(x: Int): Option[String] = Some(x.toString) | |
str: (x: Int)Option[String] | |
scala> def toInt(x: String): Option[Int] = Some(x.toInt) | |
toInt: (x: String)Option[Int] |
This file contains hidden or 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
scala> import scalaz._ | |
import scalaz._ | |
scala> import Scalaz._ | |
import Scalaz._ | |
scala> def check(i: Int, divisor: Int, s: String): Option[String] = if (i % divisor == 0) s.some else None | |
check: (i: Int, divisor: Int, s: String)Option[String] | |
scala> (1 to 50) map (i => (check(i, 3, "Fizz") |+| check(i, 5, "Buzz")).getOrElse(i)) |
This file contains hidden or 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
public class GenericVariance { | |
class A {} | |
class B extends A {} | |
@Test | |
public void checkTypes() { | |
assertTrue(new B() instanceof A); | |
final List<A> listOfA = new ArrayList<>(); |
This file contains hidden or 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
scala> :power | |
** Power User mode enabled - BEEP BOOP SPIZ ** | |
** :phase has been set to 'typer'. ** | |
** scala.tools.nsc._ has been imported ** | |
** global._ and definitions._ also imported ** | |
** Try :help, vals.<tab>, power.<tab> ** | |
scala> :paste | |
// Entering paste mode (ctrl-D to finish) |
This file contains hidden or 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
object RefactorPuzzle { | |
case class IntRdr[+A](read: Int => A) { | |
def map[B](f: A => B): IntRdr[B] = | |
IntRdr(f compose read) | |
def flatMap[B](f: A => IntRdr[B]): IntRdr[B] = | |
IntRdr(n => f(read(n)).read(n)) | |
} | |
object IntRdr { |
This file contains hidden or 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
import scala.concurrent._ | |
import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.concurrent.duration._ | |
def fut(name: String, waitTime: Duration, result: Int): Future[Int] = { | |
Future { | |
println(s"$name started") | |
Thread.sleep(waitTime.toMillis) | |
println(s"$name finished") | |
result |
This file contains hidden or 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
import scalaz._ | |
import syntax.std.option._ | |
import OptionT._ | |
import scala.concurrent._ | |
import scala.concurrent.duration._ | |
import scala.concurrent.ExecutionContext.Implicits.global | |
val o1 = 1.some | |
val o2 = 2.some |
This file contains hidden or 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
import scalaz._ | |
import Scalaz._ | |
import \&/._ | |
sealed trait Error | |
case class NotFatalError(reason: String) extends Error | |
case class FatalError(reason: String) extends Error | |
def function(i: Int): List[Error] \&/ Int = i match { |
This file contains hidden or 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
scala> val FL = Functor[List] | |
FL: scalaz.Functor[List] = scalaz.std.ListInstances$$anon$1@575f20e3 | |
scala> val FF = Functor[Future] | |
FF: scalaz.Functor[scala.concurrent.Future] = scalaz.std.FutureInstance@678bc1a9 | |
scala> val FF_FL = FF compose FL | |
FF_FL: scalaz.Functor[[α]scala.concurrent.Future[List[α]]] = scalaz.Functor$$anon$1@49487384 | |
scala> val values = Future(List(1, 2, 3, 4, 5)) |
This file contains hidden or 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
scala> usernameToUserIdLens.get("Alan").foreach(println) | |
** SELECT ID FROM USERS WHERE NAME = Alan | |
1 | |
scala> userIdToStreetNameLens.get(1).foreach(println) | |
** SELECT STREET_NAME FROM ADDRESS WHERE USER_ID = 1 | |
High Street | |
scala> val usernameToStreetNameLens = usernameToUserIdLens >=> userIdToStreetNameLens |
OlderNewer