Skip to content

Instantly share code, notes, and snippets.

View alaz's full-sized avatar

Alexander Azarov alaz

View GitHub Profile
@topfunky
topfunky / gist:169876
Created August 18, 2009 18:45 — forked from wmoxam/gist:36116
Munin plugin for beanstalk queue
#!/opt/ruby-enterprise/bin/ruby
# MODIFIED: Minor fork to run smoothly under Ruby 1.8.6. Heredoc cleanup.
require 'rubygems'
require 'beanstalk-client'
if ARGV.length > 0 && ARGV[0] == 'config'
puts <<-END
graph_category App
@retronym
retronym / generalised-type-constraints.scala
Created November 8, 2009 08:02
Demo of generalised type constraints in Scala 2.8
// scala 2.7 simple type constraint. This can only constrain a type parameter of this function.
// Below, in ListW.sumint28, we can't use this approach because we want to constrain T,
// a type param of the enclosing trait.
def sumint27A[T <: Int](l: List[T]) : Int = l.reduceLeft((a: Int, b: Int) => a + b)
trait IntLike[X] extends (X => Int)
object IntLike {
implicit val intIntLike: IntLike[Int] = new IntLike[Int] { def apply(x: Int) = identity(x) }
}
scala> import collection.mutable._
scala> val m = new HashMap[Int, Set[String]] with MultiMap[Int, String]
m: scala.collection.mutable.HashMap[Int,scala.collection.mutable.Set[String]] with scala.collection.mutable.MultiMap[Int,String] = Map()
scala> m += ((1, Set("rc", "tb")))
res10: m.type = Map((1,Set(tb, rc)))
scala> m += ((2, Set("tb")))
res11: m.type = Map((2,Set(tb)), (1,Set(tb, rc)))
// for comprehension
scala> for {
| l <- List(2,5,10)
| m <- List(8,10,11)
| } yield l * m
res17: List[Int] = List(16, 20, 22, 40, 50, 55, 80, 100, 110)
// map a pure function into applicatives
scala> List(8,10,11) <*> (List(2,5,10) map (((_: Int) * (_: Int)).curried))
res18: List[Int] = List(16, 20, 22, 40, 50, 55, 80, 100, 110)
import scalaz._
object ValidationKleisli extends Application {
import Scalaz._
// ------------------------------------------------------------------------------------------------
// Monadic use of scalaz.Validation to sequence dependent computations
// e.g. processors in a route, see also Kleisli composition of processors in scalaz-camel: http://goo.gl/CUIlQ
// ------------------------------------------------------------------------------------------------
object AuthenticatedSession extends Loggable {
def logIn(credentialsOption: Option[Credentials]): Twitter = {
val (tw, credentials) = credentialsOption match {
case Some(cr) =>
(createTwitter(Some(cr)), cr)
case None =>
val twitter = createTwitter(None)
val requestToken = twitter.getOAuthRequestToken
DesktopUtil.browse(requestToken.getAuthorizationURL)
(twitter, Dialog.showInput(null, "Enter the PIN from the Twitter authorization page in your browser",
@viktorklang
viktorklang / PostStart.scala
Created March 5, 2011 23:13
PostStart implementation for Akka
trait PostStart { actor: Actor =>
def postStart: Unit
override def preStart {
actor.become {
case "PostStart" => try { postStart } finally { actor.unbecome }
}
actor.self ! "PostStart"
}
}
@jorgeortiz85
jorgeortiz85 / unsafe.scala
Created August 12, 2011 15:41
The bug from hell
// This code is unsafe to use in a multithreaded environment.
object A {
def foo = "foo"
B.bar
}
object B {
def bar = "bar"
A.foo
@alaz
alaz / LiftResourceBundleControl.scala
Created November 23, 2011 15:11
ResourceBundle.Control implementation based on Liftweb resources XML format
package com.osinka.i18n
import java.util.{Locale,ResourceBundle}
import collection.JavaConversions._
import net.liftweb.common.Box._
import net.liftweb.util.{BundleBuilder,PCDataXmlParser}
/**
* ResourceBundle.Control implementation for Liftweb resources
*
@fbettag
fbettag / mail.scala
Created February 12, 2012 17:54
mail.scala
val props: Properties = System.getProperties()
props.put("mail.smtp.from", "my@mail.from")
val session: Session = Session.getInstance(props, null)
val message = new MimeMessage(session)
message.setFrom(new InternetAddress("my@mail.from", "This is my Name"))
message.addRecipient(Message.RecipientType.TO, new InternetAddress("recipient@foo.org")))
message.setSubject("Blubber")