Entry tags:
scala option map fuckup
scala> def f(x:String) = null
f: (x: String)Null
scala> Some("abc") map f
res3: Option[Null] = Some(null)
scala> def f(x:String) = null
f: (x: String)Null
scala> Some("abc") map f
res3: Option[Null] = Some(null)
sealed abstract class <:<[-From, +To] extends (From => To) with Serializable
private[this] final val singleton_<:< = new <:<[Any,Any] { def apply(x: Any): Any = x }
implicit def conforms[A]: A <:< A = singleton_<:<.asInstanceOf[A <:< A]
public enum ConfigValueType {
OBJECT, LIST, NUMBER, BOOLEAN, NULL, STRING
}
@Override
public Long getMilliseconds(String path) {
long ns = getNanoseconds(path);
long ms = TimeUnit.NANOSECONDS.toMillis(ns);
return ms;
}
@Override
public Long getNanoseconds(String path) {
Long ns = null;
try {
ns = TimeUnit.MILLISECONDS.toNanos(getLong(path));
} catch (ConfigException.WrongType e) {
ConfigValue v = find(path, ConfigValueType.STRING);
ns = parseDuration((String) v.unwrapped(), v.origin(), path);
}
return ns;
}
Props work well.
object Example {
trait Nested[A] { def dup: A => A }
object Nested {
implicit def nestedAnything[A] = new Nested[A] { def dup = identity }
implicit def nestedArray[A](implicit ev: Nested[A], mf: Manifest[A]) =
new Nested[Array[A]] { def dup = _ map ev.dup }
}
def deepDup[A](aa: Array[A])(implicit ev: Nested[Array[A]]) = ev dup aa
}
scala> val a = Array(Array(1,2,3), Array(4,5,6), Array(7,8))
a: Array[Array[Int]] = Array(Array(1, 2, 3), Array(4, 5, 6), Array(7, 8))
scala> val c = deepDup(a)
c: Array[Array[Int]] = Array(Array(1, 2, 3), Array(4, 5, 6), Array(7, 8))
scala> a(0)(0)=9; c // it's a copy, not a reference
res0: Array[Array[Int]] = Array(Array(1, 2, 3), Array(4, 5, 6), Array(7, 8))
scala> import collection.mutable.Queue import collection.mutable.Queue scala> val m: Map[Int, Queue[Int]] = Map().withDefaultValue(Queue[Int]()) m: Map[Int,scala.collection.mutable.Queue[Int]] = Map() scala> m(0) res0: scala.collection.mutable.Queue[Int] = Queue() scala> m(0).enqueue(1) scala> m(1) res2: scala.collection.mutable.Queue[Int] = Queue(1)
copy
case class Person(first: String, second: String)
val p1 = Person("John", "Waters")
val p2 = p1.copy(first="James")
def jsREPL(implicit prompt:String = "type your js") {
val here = Thread.currentThread().getStackTrace()(3)
println(s"$prompt [${here.getFileName}:${here.getLineNumber}]: ")
Source.fromInputStream(System.in).getLines.takeWhile(!_.isEmpty) foreach {
s => println(tryOr(runJS(s), (_:Exception).getMessage))
}
result match {
case Good(something) =>
something match {
case arr: Array[(String, String)] => println(s"aint that something ${arr mkString ", "})
case x => error(s"bad, bad response pretending to be good: $result")
}
case noGood => error(s"no good! $result)
}
def tryOr [T](eval: =>T, onError: Exception => T) =
try { eval } catch { case e: Exception => onError(e) }
def tryOr [T](eval: =>T, orElse: T) =
try { eval } catch { case e: Exception => orElse }
def attempt[T](eval: =>T): Option[T] = tryOr(Option(eval), None)
class BetterArray[T](array: Array[T]) {
def get(i: Int): Option[T] = attempt(array(i))
def getOrElse(i: Int, defaultValue: T) = get(i) getOrElse defaultValue
}
implicit def elementOf[T](array: Array[T]) = new BetterArray[T](array)
java.util.ArrayList, древний как доткомовский бум. А мы хочем функтор, но не можем написать ArrayList extends Functor; ну дык... дык и хер с ним; мы ж инженеры. В Скале можно. А зачем? А чтобы писать arrayList.map(someting=>something)val out = new FileOutputStream(myFile).getChannel val in: InputStream = sampleResourcePdfBSBCTX.openStream val ch = Channels.newChannel(in) try { while (true) { val nBytes = in.available out.transferFrom(ch, out.position, nBytes) out.position(out.position + nBytes) } } finally { out.close() } val text = PDF.extractTextFromImagesHiddenInPdf(pdf) text contains "Claim No.: 30l8507l5lSOX" mustBe true } }