Skip to content

Instantly share code, notes, and snippets.

@chirino
chirino / minscalaactors.scala
Created May 24, 2012 16:51 — forked from viktorklang/minscalaactors.scala
Minimalist Scala Actors using HawtDispatch
// ©2012 Viktor Klang
// ©2012 Hiram Chirino
object Actor {
import org.fusesource.hawtdispatch._
type Behavior = Any => Effect
sealed trait Effect extends (Behavior => Behavior)
case object Stay extends Effect { def apply(old: Behavior): Behavior = old }
case class Become(like: Behavior) extends Effect { def apply(old: Behavior): Behavior = like }
final val Die = Become(msg => { println("Dropping msg [" + msg + "] due to severe case of death."); Stay }) // Stay Dead plz
Tuple3 {
inline def forEach(fn: Function<Any?,Unit>):Unit = {
fn.invoke(this._1)
fn.invoke(this._2)
fn.invoke(this._3)
}
}
kotlin code:
val builder = HtmlBuilder(usersLocale).useLongDates()
"foo $a bar $b".build(builder)
Creates an anonymous class
class AnonymousTemplate(a: String, b: Foo) : (String, Foo) {
fun toString() = "foo " + a + " bar " + b
inline fun build(out: Appender): Unit {
kotlin code: "foo $a bar $b"
by default is turned into...
StringTemplate(array("foo ", " bar"), array(a, b))
if a custom compiler plugin knew you wanted a Html template, say, it could change that code to
MyHtmlTemplate(a, b)
/**
* helper method to return all create features available for the palette
*
* @param fp the feature provider
* @return an array of create features for the palette
*/
public static ICreateFeature[] getCreateFeatures(IFeatureProvider fp) {
return new ICreateFeature[] {
new CreateFigureFeature<Endpoint>(fp, Messages.paletteEndpointTitle, Messages.paletteEndpointDescription, Endpoint.class)
#for (n <- nodeDefinitions)
@chirino
chirino / Web.scala
Created August 8, 2011 22:44 — forked from yang/Web.scala
import org.scalatra._
import scalate.ScalateSupport
object Web extends App {
import org.mortbay.jetty.Server
import org.mortbay.jetty.webapp.WebAppContext
val webapp = new WebAppContext
webapp.setDescriptor("src/main/webapp/WEB-INF/web.xml")
webapp.setResourceBase("src/main/webapp/")
val server = new Server(8080)
---- index.jade ----
- attributes("layout") = "/WEB-INF/nested.jade"
- attributes("left") = capture
h1 Hello
- attributes("right") = capture
p This is the content
---- /WEB-INF/nested.jade ----
#
val name = "Foo"
val blah = "Something"
class #{name} extends #{interfaces.mkString(" with ")}
# for( method <- methods )
def #{method.name}() =
println
:string
Here
Doc style
object Elvis {
private class ElvisNull extends RuntimeException
final class ElvisAny[T](val value: T) extends Proxy {
def self: Any = value
def ? : T = if( value==null ) throw new ElvisNull else value
def :| (func: =>T): T = {
try {
object transformer extends Transformer {
$(".people") { node =>
// extract a template fragment from the html.. removes it
val odd = node.$template("tr.odd")
val even = node.$template("tr.even")
var c=0
people.flatMap { p =>
c += 1
node << transform(if (c%2 == 0) even else odd) { t =>
$(".name").contents = p.name