Entry tags:
cliff click code
public static void info( String s, boolean stdout ) //... //... if (printLogSeparatorToStdout) Log.info("Additional column information only sent to log file...");
(that was the only case when this flag was used)
public static void info( String s, boolean stdout ) //... //... if (printLogSeparatorToStdout) Log.info("Additional column information only sent to log file...");
scala> type λ = Any; type λλ=λ=>λ; type λλλ=λ=>λλ;
defined type alias λ
defined type alias λλ
defined type alias λλλ
scala> val I:λλ = (x:λ)=>x
I: λ => λ = <function1>
scala> val K:λλλ = (x:λ)=>(y:λ)=>x
K: λ => (λ => λ) = <function1>
scala> val S = (x:λλλ)=>(y:λλ)=>(z:λ)=>x(z)(y(z))
S: (λ => (λ => λ)) => ((λ => λ) => (λ => λ)) = <function1>
class StrangeIntQueue extends Queue[Int] { override def enqueue[Int](x:Int) = { println( x * x ) super.enqueue(x).asInstanceOf[Queue[Int]] } }
val text = "\n\n\nHEADER\n\ncontent\n\n" val cleanedup = text.dropWhile("\n"==)
scala>val input = for {i <- 1 to 10 view; _ = println(s"--$i--")} yield i scala> input find (5==) --1-- --2-- --3-- --4-- --5-- res3: Option[Int] = Some(5)
view
in the right placedef toSource(x: Any): String = { x match { case null => "null" case s: String => "\"" + s.replaceAll("\"", "\\\"") + "\"" case list:List[_] => "List(" + list.map(toSource).mkString(", ") + ")" case array:Array[_] => "Array(" + array.map(toSource).mkString(", ") + ")" case map:Map[_, _] => "Map(" + map.map{case(k,v) => toSource(k) + "->" + toSource(v)}.mkString(", ") + ")" case other => other.toString } }
def questionHTML(i: Int, q: String) = <span> <h3>Question {i+1}</h3>{q} <br/><br/><br/><br/><br/><br/> </span> def variantHTML(v: List[String]) = <p style="page-break-after:always;"> <h1><center>{title}</center></h1> {v.zipWithIndex map {case(q,j) => questionHTML(j,q)}} </p> def html(variants: List[List[String]]) = { <html><body>{ variants map variantHTML }</body></html> }
def findFurthest(current: List[Index])(collection: List[Index]) = collection.map(i => (distance(current)(i), i)).max(order)._2 def sortByDistance(source: List[Index], target:List[Index] = Nil): List[Index] = source match { case Nil => target case more => { val furthest = findFurthest(target)(source) sortByDistance(source.filterNot(furthest==), furthest::target) } } val sorted = sortByDistance(allIndexes).reverse
def from(location: String) = { def loadTable(name: String, ignoring: String*) { def ignoringColumns(row: String Map String) = row.filterKeys(k => !(ignoring contains k)) val src = Source.fromFile(s"$location/$name.csv").getLines().mkString("\n") val data = parseText(src) map ignoringColumns insertInto(name, data) } }
case class from(location: String) { self => def loadTable(name: String, ignoring: String*): from = { // actually, the load order is opposite to deletion order update(s"delete from $name") def ignoringColumns(row: String Map String) = row.filterKeys(k => !(ignoring contains k)) val src = Source.fromFile(s"$location/$name.csv").getLines().mkString("\n") val data = parseText(src) map ignoringColumns toList insertInto(name, data) self } }
from(location). loadTable("users"). loadTable("abusers","historyOfAbuse"). loadTable("WMD","rocks","stones"). loadTable("passwordAndKeys")
def parameters(href): Map[String, String] = href split("\\?",2) toList match { case url::params::Nil => params.split("&") map (_ split "=" toList) collect { case k::v::Nil => k->v } toMap case otherwise => Map.empty }
scala> "my number is 100 15 90" filter (_.isDigit) res5: String = 1001590
var
to val
(it was an ancient idea of having variable id, something very weird). Now I had to use reflection, so I did.override def insert(o: T): T = { val fields = fieldsOf(o.getClass) val idField = fields("id") idField.set(o, globalID) globalID += 1 update(o) o } } private def fieldsOf(clasz: Class[_]):Map[String, Field] = { def listFields(c: Class[_]): List[Field] = if (c==null) Nil else c.getDeclaredFields .filter (!_.getName.contains("$") ) .toList ++ listFields(c.getSuperclass) val fields = listFields(clasz) fields foreach (_.setAccessible(true)) fields map (f => f.getName -> f) toMap }
scala> case class MyCase(name: String, value:Int) defined class MyCase scala> val factory = MyCase factory: MyCase.type = MyCase scala> factory("cool", 42) res0: MyCase = MyCase(cool,42)
Seq[T]
, and two predicates, p0:T=>Bool
, p1:T=>Bool
, extract segments of the sequence that start with elements satisfying p0
and end with elements satisfying p1
.def extractChunks[T](src:Seq[T], p0:T=>Boolean,p1:T=>Boolean) = { val found:(List[T], List[List[T]]) = ((Nil:List[T], Nil:List[List[T]])/:src) { case ((Nil, out), x) => if (p0(x)) (List(x), out) else (Nil, out) case ((seg, out), x) => if (p1(x)) (Nil, (x::seg)::out) else (x::seg, out) } found._2 map (_.reverse) reverse }
println(extractChunks[Char]("abracadabra", 'b'==, 'a'==))
Will print List(List(b, r, a), List(b, r, a))
.
def groupByRelationship[T](p: ((T,T) => Boolean)) = { new (List[T] => List[List[T]]) { self => def apply(xs: List[T]): List[List[T]] = xs match { case List() => List() case (x:T) :: _ => val pairs:Iterator[Seq[T]] = xs.sliding(2) val (ps1, ps2) = pairs.span(_.toList match { case a::b::Nil => a==b || p(a, b) case _ => false }) val chain:List[T] = x::(ps1 map (_(1))).toList val tail = ps2.toList collect {case a::b::Nil => b} chain :: self(tail) } } }
def groupListBy[T,U](xs: List[T])(f: T => U): List[List[T]] = groupListByRelationship[T](xs)((x,y) => f(x)==f(y))
val g1 = groupByRelationship[Int](_ < _)(List(1,2,4,2)) g1 must_== List(List(1,2,4), List(2)) val g2 = groupByRelationship[Int](_ > _)(List(1,2,4,2)) g2 must_== List(List(1),List(2), List(4,2))
lazy val eobOpt: Result[EOB] = ... ... def items: Result[Traversable[EOB_item]] = { val listOfParsedItems: Traversable[Result[EOB_item]] = itemsData map { item => { eobOpt flatMap (buildItem(_)(item)) } } Result.traverse(listOfParsedItems).map(_.toList) }
flatMap
, should not have an alias name, like =>> or something? I can juggle things around, but what I'm looking for is simple beauty, or beautiful simplicity, same thing.sealed trait Result[+T] { ... def <*>[U](other: Result[U]): Result[(T,U)] ... } case class Good[T](value:T) extends Result[T] { ... def <*>[U](other: Result[U]): Result[(T, U)] = other.flatMap(u => Good((value, u))) ... } case class Bad[T](errors:Traversable...) extends Result[T] { ... def <*>[U](other: Result[U]): Result[(T, U)] = Bad(errors ++ other.errors) ... } scala> val op = Good((1,"one")) op: Good[(Int, String)] = Good((1,one)) scala> for ((i,s) <- op) println(s"i=$i, s=$s") i=1, s=one
Result[T]
, and it is applicative, so Result[T] <*> Result[U]
produces Result[(T,U)]
, I want to use the result, naming individual fields. Like in val username: Result[String] = getUserNameFromTheCloud(userId) val prize: Result[Prize] = rollTheDice val letter:Result[(String, Prize)] = for ((u, p) <- username <*> prize) yield congratulate(u, p) // etc
for(u <- username; p <- prize) yield congratulate(u,p)
val claimsListSelector = "#claimsTable" val claimList = "_$$('"+claimsListSelector+" a')" ... val clicked = js(s"clickOn($claimList[$i])")
val patience = 10 case class UploadProgress(uploaded: Set[File] = Set.empty, failed: Set[Result[File]] = Set.empty, badLuckStreak: Int = 0) { def errors = Result.traverse(failed) def +(file:File) = { if (badLuckStreak > patience) this else { uploadOneFile(file) match { case Good(file) => UploadProgress(uploaded + file, failed, 0) case bad => UploadProgress(uploaded, failed + bad, badLuckStreak + 1) } } } } def uploadScheduledFiles():UploadProgress = { if (!client.isAlive) UploadProgress(Set.empty, Set(Result.error("Upload server is dead, sorry")), 0) else { (UploadProgress() /: listFilesForUpload)(_ + _) } }