Skip to content

Instantly share code, notes, and snippets.

View deanwampler's full-sized avatar

Dean Wampler deanwampler

View GitHub Profile
@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
/**
* 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.
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
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]) {}
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)))
}
// 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
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()
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{
@deanwampler
deanwampler / GraphFramesExample.snb
Created March 4, 2016 13:03
Databrick's Python example for the new GraphFrame API ported to Scala and Spark Notebook
{
"metadata" : {
"name" : "GraphFramesExample",
"user_save_timestamp" : "1970-01-01T01:00:00.000Z",
"auto_save_timestamp" : "1970-01-01T01:00:00.000Z",
"language_info" : {
"name" : "scala",
"file_extension" : "scala",
"codemirror_mode" : "text/x-scala"
},
@deanwampler
deanwampler / ray-for-the-curious-full.py
Last active December 30, 2020 23:39
The full example in one listing for Ray for the curious
import numpy as np
import ray
import time
ray.init() # Start Ray
@ray.remote
class ParameterServer(object):
def __init__(self, dim):