Skip to content

Instantly share code, notes, and snippets.

View SethTisue's full-sized avatar

Seth Tisue SethTisue

View GitHub Profile
@SethTisue
SethTisue / Typeclasses.scala
Created March 12, 2012 19:11
typeclass code from NEScala talk
case class Person(
name: String,
age: Int)
case class Restaurant(
name: String,
brunch: Boolean)
// "ser" could just be "apply"
@SethTisue
SethTisue / gist:2778894
Created May 24, 2012 01:25
frequency of NetLogo primitive usage in Models Library
46097 java.lang.Double
11799 java.lang.String
11458 _observervariable
11074 _set
7992 _letvariable
5257 _turtlevariable
5254 _call
4377 _patchvariable
4330 _breedvariable
3393 _ask
@SethTisue
SethTisue / gist:2942990
Created June 17, 2012 00:34
add zipWith to Iterable
// add zipWith to Iterable; thank you stackoverflow.com/q/3895813
implicit class RichIterable[A, CC[X] <: Iterable[X]](xs: CC[A]) {
type Builder[C] = collection.generic.CanBuildFrom[Nothing, C, CC[C]]
def zipWith[B, C](ys: Iterable[B])(f: (A, B) => C)(implicit cbf: Builder[C]): CC[C] =
xs.zip(ys).map(f.tupled)(collection.breakOut)
}
to-report pair [x l]
report map [list x ?] l
end
to-report pairs [l1 l2]
report reduce sentence map [pair ? l2] l1
end
@SethTisue
SethTisue / gist:3005156
Created June 27, 2012 16:17
Example of an optional command block in a NetLogo extension primitive
public static class Reflect
extends DefaultCommand
implements org.nlogo.nvm.CustomAssembled
{
public Syntax getSyntax()
{
int[] right = { Syntax.TYPE_NUMBER , Syntax.TYPE_BOOLEAN_BLOCK ,
Syntax.TYPE_COMMAND_BLOCK | Syntax.TYPE_OPTIONAL } ;
return Syntax.commandSyntax( right ) ;
}
@SethTisue
SethTisue / gist:3113182
Created July 14, 2012 20:16
extension methods in Scala 2.9 vs. 2.10
// BEFORE (Scala 2.9)
implicit def toRichLong(i: Long): MyRichLong = new MyRichLong(i)
class MyRichLong(i: Long) {
def digits: Seq[Int] = i.toString.map(_.asDigit)
}
// AFTER (Scala 2.10)
implicit class RichLong(i: Long) {
@SethTisue
SethTisue / Agents.scala
Created September 10, 2012 17:36
typeclasses: compile-time checking of restricting commands to certain agent types.
trait Command
case class Forward(distance: Double) extends Command
case class Print(message: String) extends Command
case object Die extends Command
trait Agent
case object Observer extends Agent
case class Turtle(id: Long, var heading: Double = 0, var x: Double = 0, var y: Double = 0) extends Agent
case class Patch(px: Int, py: Int) extends Agent
@SethTisue
SethTisue / plotting.scala
Created November 20, 2012 18:06
NetLogo: old and new plotting architecture
// The idea: instead of PlotListener with lots of methods,
// we have a PlotAction trait with lots of subclasses.
// what can we do with plot actions?
// - we can respond to them by mutating a plot on-screen
// but also:
// - we can write tests (are the right actions generated?)
// - we can record them for later replay (in e.g. recordable model runs)
// - we can broadcast them to a remote client
@SethTisue
SethTisue / JFlexRunner.scala
Created November 26, 2012 19:26
using JFlex from sbt
import java.io.File
import sbt._
import Keys._
object JFlexRunner {
val task =
(cacheDirectory, javaSource in Compile, streams) map {
(cacheDir, src, s) =>
val cache =
scala> def lin: Stream[Int] = { println("foo"); 1 #:: { println("bar"); lin.map(_ + 1) } }
lin: Stream[Int]
scala> lin.take(5).force
foo
bar
foo
bar
foo
bar