Skip to content

Instantly share code, notes, and snippets.

Avatar

Przemek Pokrywka przemek-pokrywka

View GitHub Profile
@przemek-pokrywka
przemek-pokrywka / WrASSE_3_GateActor.scala
Created Nov 15, 2010
Here is how to capture both Gate states: open and closed without using GoF State Pattern. It looks like a thread, but it doesn't block any most of the time. Most of the previously hardcoded things are actor parameters now.
View WrASSE_3_GateActor.scala
package wrasse3
import scala.actors.Actor._
import System.{currentTimeMillis => now}
import actors.{Actor, TIMEOUT}
class GateActor[R](sendRequest: => R,
errorResponse: R,
maxErrors: Int,
closePeriod: Int) extends Actor {
@przemek-pokrywka
przemek-pokrywka / Thread hanging (kestrel 1.2)
Created Dec 1, 2010
What does the serverActor do? It never responds to "!?"
View Thread hanging (kestrel 1.2)
Thread 10: (state = BLOCKED)
- java.lang.Object.wait(long) @bci=0 (Compiled frame; information may be imprecise)
- java.lang.Object.wait() @bci=2, line=485 (Interpreted frame)
- scala.actors.Actor$class.suspendActor(scala.actors.Actor) @bci=21, line=731 (Interpreted frame)
- scala.actors.Actor$class.receive(scala.actors.Actor, scala.PartialFunction) @bci=109, line=431 (Interpreted frame)
- my.innocent.actor.that.simply.wants.to.send.message.via.Kestrel.receive(scala.PartialFunction) @bci=2, line=18 (Interpreted frame)
- scala.actors.Channel.receive(scala.PartialFunction) @bci=16, line=91 (Interpreted frame)
- scala.actors.Actor$class.$bang$qmark(scala.actors.Actor, java.lang.Object) @bci=29, line=599 (Interpreted frame)
- scala.actors.Actor$$anon$1.$bang$qmark(java.lang.Object) @bci=2, line=94 (Interpreted frame)
- net.lag.smile.MemcacheConnection.store(java.lang.String, java.lang.String, byte[], int, int) @bci=19, line=115 (Interpreted frame)
@przemek-pokrywka
przemek-pokrywka / gist:981598
Created May 19, 2011
A quick-hack-dirty-fix for Smile hanging threads' problem described at https://github.com/robey/kestrel/issues/26
View gist:981598
index 2cb9192..42bea4b 100644
--- a/src/main/scala/net/lag/smile/MemcacheConnection.scala
+++ b/src/main/scala/net/lag/smile/MemcacheConnection.scala
@@ -119,7 +119,7 @@ class MemcacheConnection(val hostname: String, val port: Int, val weight: Int) {
@throws(classOf[MemcacheServerException])
def store(query: String, key: String, value: Array[Byte], flags: Int, expiry: Int): Boolean = {
- serverActor !? Store(query, key, flags, expiry, value) match {
+ serverActor.!?(20 * 1000l, Store(query, key, flags, expiry, value)).getOrElse(Error("Unable to send message to server due to a temporary local problem")) match {
case Timeout =>
@przemek-pokrywka
przemek-pokrywka / ReaderMonadDIWhenFunsDependOnVariousThings.scala
Created Jan 21, 2013
Reader Monad dependency injection when each of injected functions depends on something other. The goal is to assess, how good does Reader Monad fit into this context.
View ReaderMonadDIWhenFunsDependOnVariousThings.scala
case class Reader[Conf, Res](obtainResult: Conf => Res) {
def map[NewRes](transform: Res => NewRes) =
Reader {
(config: Conf) =>
transform(this obtainResult config)
}
def flatMap[NewRes](createReader: Res => Reader[Conf, NewRes]) =
Reader {
View gist:5327732

Let me welcome you with some Scala code:

object Hello extends App {
  println("Hello!")
}

And now the same, but with standard Markdown formatting:

object Hello extends App {

@przemek-pokrywka
przemek-pokrywka / StackTraceHelper.scala
Created Sep 19, 2013
Small utility to clean up longish stacktraces. All comments how to make it more generic warmly welcome.
View StackTraceHelper.scala
object StackTraceHelper
{
def retainWithBorder [T] (items: Seq[T],
valuable: (T => Boolean)): Seq[T] =
{
if (items.size < 3) return items
val retained = for
{
@przemek-pokrywka
przemek-pokrywka / gist:9315172
Created Mar 2, 2014
How to get None when reduction was not applied and Some(value) when it did in scala.rx
View gist:9315172
package frp
import rx._
import rx.ops._
object Demo extends App {
case class Val[+T](value: T, reduced: Boolean = false) {
def toOpt = if (reduced) Some(value) else None
}
@przemek-pokrywka
przemek-pokrywka / Scala-style underscore in Python.py
Last active Sep 20, 2015
I've stumbled upon wonderful https://github.com/kachayev/fn.py library. One thing I missed was the ability to use `_.method(args)` syntax. Following quick hack aims to fix that. CAVEAT: it's a hack, it'll work unpredictably for cases different than `_.method(args)`.
View Scala-style underscore in Python.py
class Underscore:
def __getattr__(self, methodname):
return lambda *args, **kwargs: \
lambda o: eval("o." + methodname)(*args, **kwargs)
_ = Underscore()
filter(_.startswith("A"), ["Africa", "America", "Europe"])
map(_.upper(), ["hello", "world"])
map(_.count("I"), ["TEAM", "TIM"])
@przemek-pokrywka
przemek-pokrywka / hello-coursier-ammonite-play.sh
Last active May 7, 2016
Serve a webpage using Play framework with a simple script. Nothing more, than Linux and Java required. Thanks to brilliant work of Alexandre Archambault, @li_haoyi and Play developers.
View hello-coursier-ammonite-play.sh
#!/bin/bash
test -e ~/.coursier/cr || (mkdir -p ~/.coursier && wget -q -O ~/.coursier/cr https://git.io/vgvpD && chmod +x ~/.coursier/cr)
CLASSPATH="$(~/.coursier/cr fetch -q -p \
\
com.typesafe.play:play-netty-server_2.11:2.5.0 \
com.typesafe.play:play_2.11:2.5.0 \
com.lihaoyi:ammonite-repl_2.11.7:0.5.2 \
\
)" java \
-Dplay.crypto.secret=foo.bar.baz \
@przemek-pokrywka
przemek-pokrywka / scraper.scala
Created Sep 4, 2016
The example shown at Scalapolis 2016 in Wrocław (with small improvements). HTML scraping of the conference page done as a one-file executable Scala script.
View scraper.scala
trait valid_both_in_bash_and_in_scala /* 2>/dev/null
# ^^^ can be replaced by anything that's harmless in Bash and valid top-file-def in Scala.
# Kudos to that attendee (sorry, I don't know your name), who noticed that opportunity. Cheers!
# Making sure Coursier is available
cr=~/.coursier
test -e $cr/cr || (mkdir $cr && wget -q -O $cr/cr https://git.io/vgvpD && chmod +x $cr/cr)
dependencies=(
org.jsoup:jsoup:1.7.2