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
val listOfAnys = List("x", 33, 21, "y", 55, 2.0, "z") | |
//> listOfAnys : List[Any] = List(x, 33, 21, y, 55, 2.0, z) | |
// 2 hard wired ways to filter by type | |
listOfAnys.collect{ case s:String=>s} //> res0: List[String] = List(x, y, z) | |
listOfAnys.filter{_.isInstanceOf[String]} //> res1: List[Any] = List(x, y, z) | |
// more generic manner |
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
val li=List(1,2,3,4,5,6,7,8) | |
val nums=for { | |
i<-li.toIterator | |
_=println(" "+i) | |
} yield i | |
// note how we bail early | |
println(nums.exists{_>3}) | |
// nums is now an iterator, so is mutable, we continue from where we left off |
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.collection.generic.FilterMonadic | |
object stuff { | |
def functor[X, Y,F[X] <: Traversable[X]](f: X => Y) = | |
(arg: F[X]) => arg.map(f) //> functor: [X, Y, F[X] <: Traversable[X]](f: X => Y)F[X] => Traversable[Y] | |
val f=(x:Int)=>x*2 //> f : Int => Int = <function1> | |
val f2=functor{ f } //> f2 : Traversable[Int] => Traversable[Int] = <function1> |
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 stuff { | |
println("xx") //> xx | |
trait Plus[A] { | |
def plus(a1: A, a2: A): A | |
} | |
trait PlusOp[A] { | |
val F: Plus[A] |
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.Future | |
import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.concurrent.Await | |
import scala.concurrent.duration.Duration | |
object Fut3 extends App { | |
def logF(st: String) = Future { | |
Thread.sleep(500) |
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 nashorn | |
import javax.script.{Invocable, ScriptEngineManager, SimpleBindings} | |
import java.io.InputStreamReader | |
import scala.collection.JavaConverters._ | |
object Mustache { | |
private val engineManager = new ScriptEngineManager() | |
val engine = engineManager.getEngineByName("nashorn") | |
val invocable = engine.asInstanceOf[Invocable] |
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 kamonstuff | |
import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.concurrent.Future | |
import akka.actor.Actor | |
import akka.actor.ActorSystem | |
import akka.actor.Props | |
import kamon.Kamon | |
import kamon.metric.SubscriptionsDispatcher.TickMetricSnapshot |
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 akkastream | |
import akka.stream._ | |
import akka.stream.scaladsl._ | |
import akka.NotUsed | |
import akka.actor._ | |
import scala.concurrent._ | |
import scala.concurrent.duration._ | |
import akka.util.ByteString |
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 HttpServerMetrics extends GenericEntityRecorder { | |
public static final Logger log = LoggerFactory.getLogger(HttpServerMetrics.class); | |
public static final EntityRecorderFactory<HttpServerMetrics> FACTORY = EntityRecorderFactory$.MODULE$.create("akka-http-server", HttpServerMetrics::new); | |
private final Counter statusInformational; | |
private final Counter statusSuccess; | |
private final Counter statusRedirection; | |
private final Counter statusClientError; | |
private final Counter statusServerError; |
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
sealed trait Result[+A] { | |
def map[B](f: A => B): Result[B] = | |
this match { | |
case success@Success(value) => Success(f(value)) | |
case warning@Warning(value, message) => warning.copy(value=f(value)) | |
case failure@Failure(message) => failure | |
} | |
} | |
final case class Success[A](value: A) extends Result[A] |