Skip to content

Instantly share code, notes, and snippets.

@zcox
Created September 6, 2011 05:22
Show Gist options
  • Save zcox/1196660 to your computer and use it in GitHub Desktop.
Save zcox/1196660 to your computer and use it in GitHub Desktop.
FluentPipeline Scala Example
import com.tinkerpop.blueprints.pgm._
import com.tinkerpop.blueprints.pgm.impls.tg._
import com.tinkerpop.pipes._
import com.tinkerpop.pipes.util._
import scala.collection.JavaConversions.asScalaIterator
import java.lang.{Boolean => JBoolean}
val graph = TinkerGraphFactory.createTinkerGraph()
//Adapter from T => Boolean to PipeClosure along with an implicit conversion
class Function1PipeClosure[T](f: T => Boolean) extends AbstractPipeClosure[JBoolean, Pipe[_, _]] {
override def compute(objects: Object*): JBoolean = f(objects(0).asInstanceOf[T])
}
implicit def fToFunction1PipeClosure[T](f: T => Boolean) = new Function1PipeClosure[T](f)
//Now we can pass a function literal instead of an AbstractPipeClosure to filter
val fp = new FluentPipeline[Vertex, String](graph getVertex 1) out "knows" property "name" filter { s: String => s startsWith "j" }
for (friendName <- fp) println(friendName)
//output is just "josh"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment