Skip to content

Instantly share code, notes, and snippets.

View deanwampler's full-sized avatar

Dean Wampler deanwampler

View GitHub Profile
case class Error[E,A](e:Either[E,A]) {
def flatMap[B](f:A => Error[E,B]):Error[E,B] ={
Error(e.right.flatMap(a=> f(a).e))
}
def map[B](f:A=>B):Error[E,B]={
Error(e.right.map(f))
}
}
object ErrorUtils{
object looksLikeAFunction{
def apply(s:String):Int=1
}
object nowAFunction extends Function[String,Int]{
def apply(s:String):Int=1
}
object TestMethods {
implicit def functionToAnother(f:Function[String,Int]):Function[Int,String] =
(i:Int)=> f(i.toString()).toString()
// When you run "scala colors.scala", it's like you compiled the following script,
// where the contents of colors.scala is embedded inside the "new Anyref {...}".
object Script {
def main(args: Array[String]): Unit = {
new AnyRef {
// If you change "object" to "class" and remove the
// "type Color = Value" declaration, it compiles.
class Color extends Enumeration {
type Color = Value
val Empty = Value
case class Point(x: Int, y: Int) {
require(x >= 0)
require(y >= 0)
}
case class Piece(ps: Set[Point]) {
def flip = {
val mx = spanx
Piece(ps.map(p => Point(mx - p.x, p.y)))
}
import javax.swing.SwingWorker
class A {
def loadAll(f:(Int) => List[String]): List[String] = List("a")
def getFriendsIds(page: Int): List[String] = List("a")
def getFollowersIds(page: Int): List[String] = List("a")
}
class B {
def setFollowers(followers: List[String]) {}
class MySwingWorker extends SwingWorker[List[TwitterUserId], Object](ids: List[String]) {
def doInBackground = twitterSession.loadAll(ids)
override def done = streams.setFollowerIds(get map (_.id.toString()))
}
(new MySwingWorker(twitterSession.getFollowersIds)).execute
(new MySwingWorker(twitterSession.getFriendsIds)).execute
/**
* http://gist.github.com/161702.
* Start with this Groovy example, http://gist.github.com/160492
* and Scala variants suggested by others, http://gist.github.com/161159
* and http://gist.github.com/162389
*
* Here is what I came up with, but it's somewhat different. The others assume
* that Cucumber will parse the regex's and invoke the anonymous function
* specified with the Given declaration. The problem is that it's difficult to
* define a generic function signature.
@deanwampler
deanwampler / foobar.rb
Created January 14, 2009 15:37 — forked from nicksieger/foobar.rb
I prefer the send form.
# Not clear in the rdocs what the behavior will be when you pass nil!
def foo(val = nil)
val ||= "default"
end
# This one provides much better documentation to the reader, especially "rdocs"
# (At least, I *think* rdocs show the default values...)
def bar(val = "default")
val
end