Skip to content

Instantly share code, notes, and snippets.

View erikkaplun's full-sized avatar

Erik Kaplun erikkaplun

  • UXO
  • Tallinn, Estonia
View GitHub Profile
trait Extractor[T, U] { def unapply(x: T): Option[U] }
object Extractor {
def apply[T, U](f: PartialFunction[T, U]) = new Extractor[T, U] {
def unapply(x: T) = f.lift(x)
}
}
val EmployedAt = Extractor[Person, Company] {
case Person(_, _, Some(Position(_, Some(c)))) => c
}
$ rm -f *.class; fsc -shutdown
[Compile server exited]
$ time scala test.scala
real 0m5.779s <--------
user 0m0.375s
sys 0m0.069s
--------------------------
~/code/scala/testtask$ fsc # käivitab fsc daemoni
#####################
# git
#####################
alias g=git
alias s='g status'
alias gs='g status'
alias a='g add'
alias ga='g add'
alias gap='ga -p'
alias gf='g fetch'
package prob
import scalaz._
import Scalaz._
import org.scalacheck.{ Arbitrary, Gen }
/** Types for working with probabilities, i.e. values of type `Double` in range `[0.0, 1.0]`. */
object `package` {
global
log 127.0.0.1 local0
# log 127.0.0.1 local1 notice
#log loghost local0 info
#maxconn 4096
chroot /usr/share/haproxy
user haproxy
group haproxy
daemon
#debug
import scala.io.Source
import scala.xml.XML
val ServiceUrl = "http://services.postcodeanywhere.co.uk/PostcodeAnywhereInternational/interactive/RetrieveByPostalCode/v2.20/xmla.ws"
val params = Map(
"key" -> "UP26-TZ93-TK96-PJ69",
"country" -> "United Kingdom".toUpperCase,
"postalcode" -> "11312".replaceAll(" ", ""))
// have
val params = (
(postalCode |> notNone("postal code missing")) |@|
(countryCode |> notNone("country code missing")).flatMap(validIso2CountryCode("country code invalid"))
)
// would like
val params = (
import shapeless._
import poly._
import syntax.std.tuple._
type Va[+A] = Validation[String, A]
type Vali[+A, -B] = Kleisli[Va, A, B]
def Vali[A, B](fn: A => Va[B]): Vali[A, B] = Kleisli[Va, A, B](fn)
implicit val vaBinding = new Bind[Va] {
def map[A, B](fa: Va[A])(f: A => B): Va[B] = fa.map(f)
[error] found : scalaz.syntax.ApplicativeBuilder[[X]scalaz.Validation[scalaz.NonEmptyList[String],X],String,String]
[error] required: scalaz.ValidationNel[String,String]
[error] (which expands to) scalaz.Validation[scalaz.NonEmptyList[String],String]
[error] params.map(_.toValidationNel).reduce(_ |@| _) { (code, countryCode3) =>
[error] ^
// libraryDependencies ++= Seq(
// "org.scalaz" %% "scalaz-core" % "7.1.0",
// "com.chuusai" %% "shapeless" % "2.0.0",
// "org.typelevel" %% "shapeless-scalacheck" % "0.3",
// "org.typelevel" %% "shapeless-spire" % "0.3",
// "org.typelevel" %% "shapeless-scalaz" % "0.3"
// )
import shapeless._, contrib.scalaz._, syntax.std.tuple._