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
package fewchaz | |
import scala.util.{Failure, Success, Try} | |
import scala.concurrent.{CanAwait, ExecutionContext} | |
import java.util.concurrent._ | |
import scala.util.control.NonFatal | |
trait Fewcha[T] extends scala.concurrent.Future[T] { | |
def futr: Future[T] | |
private[this] final def toTry: Try[T] = try util.Success(futr.get()) catch { case NonFatal(t) => util.Failure(t)} |
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
//requires scala 2.10.x | |
//requires scalaz-2.10-7.0.x | |
package fpx | |
// Furnace types (in reality, these come from a Java API and are real!) | |
trait FurnaceService { | |
def createSession(user: String, password: String): FurnaceSession |
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 ReferenceDirective extends Enumeration { | |
val Replace, DoNotReplace = Value | |
def replace: Value = Replace | |
def doNotReplace: Value = DoNotReplace | |
} | |
import java.util.concurrent.atomic._ | |
import scalaz._ | |
/** |
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
package gsa.domain.scala.dan | |
import scalaz.Leibniz.=== | |
import scalaz._ | |
import Scalaz._ | |
import gsa.shared.scala._ //gets you Date, Instant types | |
/** | |
* We wish to use a DomainService to get a snapshot of our domain (e.g. Books and SubBusinessUnits). |
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 Lenses { | |
/* | |
This gist shows that LensP (modification inside a functor) is isomorphic to LensR (getter and setter) | |
In particular, it shows that you can derive the signature: S => A from (A => F[A]) => (S => F[S]) | |
This is puzzling at first glance, because it gives rise to the question: | |
"if I end up with an F[S], how can I get a A out of it?" | |
We use the Const type where Const[A, X] = X. Note that Const[_, X] is a functor, but will always return | |
the value X which was originally stored in it! |
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
//I have a Java interface which looks like this | |
trait ConsumableResource[A <: AnyRef] extends AutoCloseable { | |
/** returns `null` when we are ended */ | |
def take(): A | |
} | |
//I would like to make this viewable as a scala `TraversableOnce` where the resource is automatically closed at the end of the traversal |
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 it = Traversable(1); it foreach (i => println(s"$i and ${it.isEmpty}")) | |
1 and false | |
scala> val it = Iterator(1); it foreach (i => println(s"$i and ${it.isEmpty}")) | |
1 and true |
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 java.util.*; | |
class Unchecked { | |
static class Foo<A> { | |
Optional<A> doIt(Object o) { return Optional.of((A) o);} | |
} | |
public static void main(String[] args) { | |
System.out.println(new Foo<String>().doIt(3)); | |
} | |
} |
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 Unchecked extends App { | |
class Foo[A] { | |
def doIt(o: Any): Option[A] = Some(o.asInstanceOf[A]) | |
} | |
println(new Foo[String].doIt(3)) | |
} | |
/* compile it */ | |
//scalac Unchecked.scala |
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 java.util.stream.*; | |
import java.util.function.*; | |
class FMTS { | |
public static void main(String[] args) { | |
IntStream intStream1 = IntStream.of(1, 2); | |
IntStream intStream2 = IntStream.of(1, 2); | |
//FlatMap |