Skip to content

Instantly share code, notes, and snippets.

View softprops's full-sized avatar
®️
Rustling

Doug Tangren softprops

®️
Rustling
View GitHub Profile
class Git(x: Any) {
def fork = "forking %s..." format x
}
implicit def any2Git(x: Any) = new Git(x)
"eelco".fork
# how to resolve a class constant from a string
# via http://www.rubyquiz.com/quiz113.html
class A
end
module B
class C
end
end
import scala.util.parsing.combinator._
class JsonParsers extends JavaTokenParsers {
def value: Parser[Any] = obj | arr |
stringLiteral |
floatingPointNumber |
"null" | "true" | "false"
def obj: Parser[Any] = "{"~repsep(member, ",")~"}"
def arr: Parser[Any] = "["~repsep(value, ",")~"]"
def member: Parser[Any] = stringLiteral~":"~value
> ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) 6144
file size (blocks, -f) unlimited
max locked memory (kbytes, -l) unlimited
max memory size (kbytes, -m) unlimited
open files (-n) 256
pipe size (512 bytes, -p) 1
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
// unless: ala ruby
def unless(bool: => Boolean)(fn: => Any) = if(!bool) fn
val milkIsSpoiled = false
unless(milkIsSpoiled) {
println("mmmm. milk")
}
(List(1,2,3,4,5) /: (0 to 5)) ((l,r) => r :: l)
// List[Int] = List(5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5)
@softprops
softprops / RangeList.rb
Created November 24, 2009 04:48
how to get a subset of a collection given a range
list = [1,2,3,4,5,6,7,8,9]
list[0..3] # => [1, 2, 3, 4]
type Quacker = { def apply(msg: String):String }
val quacking = new Function[String,String] {
def apply(msg: String) = "quack! I am a fn given a %s" format msg
}
class Duck {
def apply(msg: String) = "quack! I am a class given a %s" format msg
}
if (!Array.prototype.any) {
Array.prototype.any = function() {
return this.length != 0;
};
}
if(!Array.prototype.empty) {
Array.prototype.empty = function() {
return this.length == 0;
}
// how do you convert a Map to a List and back to a Map?
val m = Map(
'unagi -> "big cat",
'maki -> "lil cat"
)
val l = m.toList // List(('unagi,"big cat"), ('maki,"lil cat"))
val m2 = Map(l: _*) // Map('unagi -> "big cat",'maki -> "lil cat")