Skip to content

Instantly share code, notes, and snippets.

View hallettj's full-sized avatar

Jesse Hallett hallettj

View GitHub Profile
/* *** Arithmetic *** */
import Math._
import S99Int._
class S99Int(val start: Int) {
def isPrime: Boolean = {
(true /: List.range(2, start / 2)) { (decision, n) =>
decision && start % n != 0
}
/* Examples of using Stream to create lazily evaluated collections */
def integersFrom(n: Int): Stream[Int] =
Stream.cons(n, integersFrom(n + 1))
val naturals = integersFrom(1)
// From: http://www.scala-blogs.org/2007/12/project-euler-fun-in-scala.html
val fib: Stream[Int] =
Stream.cons(0, Stream.cons(1, fib.zip(fib.tail).map(p => p._1 + p._2)))
scala> val f: (Any*) => Unit = { (s, n) => println(s * n) }
<console>:4: error: wrong number of parameters; expected = 1
val f: (Any*) => Unit = { (s, n) => println(s * n) }
var times = 500000,
benchmarks = {},
objects,
sys = {
puts: function() { console.log.apply(console, arguments); }
};
function bm(label, fn) {
benchmarks[label] = fn;
};
/**
* CSS addon for Kongregate Asynchronous JavaScript Loader
*
* Adds support to KJS for loading CSS files.
*
* You can find KJS at:
* https://gist.github.com/388e70bccd3fdb8a6617
*
* Usage:
*
/**
* From the W3C working draft
* http://www.w3.org/TR/IndexedDB
*/
// synchronously set properties on a database and retrieve a record
var db = indexedDB.open('books', 'Book store', false);
if (db.version !== '1.0') {
var olddb = indexedDB.open('books', 'Book store');
@hallettj
hallettj / closure.groovy
Created February 6, 2011 06:05
Groovy supports true closures
def makeClos() {
def x = 0;
def close = { x += 1; println(x) }
close
}
def closA = makeClos()
def closB = makeClos()
closA() // prints 1
@hallettj
hallettj / groupBy.scala
Created February 21, 2011 23:41
how to use Set#groupBy in Scala 2.7
/*
* Set#groupBy() is not defined in Scala 2.7.
*/
class GroupableSet[A](val set: Set[A]) {
def groupBy[K](grouping: A => K): Map[K, Set[A]] = {
val init: Map[K, Set[A]] = Map()
set.foldLeft(init) { (map, e) =>
val key = grouping(e)
val group = map.getOrElse(key, Set())
map + (key -> (group + e))
@hallettj
hallettj / define.js
Created June 23, 2011 01:59
define()
/**
* Minimal implementation of Modules/AsynchronousDefinition as described
* in http://wiki.commonjs.org/wiki/Modules/AsynchronousDefinition
*
* For a more complete implementation use RequireJS
* <http://requirejs.org/>.
*/
var define = (function() {
var definitions = {},
@hallettj
hallettj / events-request.http
Created July 15, 2011 18:20
CSRF protection techniques
GET /__services/v2/rest/browserEvents/1310753774420?e=activityStream.poll
Accept: application/json, text/javascript, */*; q=0.01
Cookie: jive_wysiwygtext_height=845; jive.server.info="serverName=brewspace.jiveland.com:serverPort=443:contextPath=:localName=localhost.localdomain:localPort=9001:localAddr=127.0.0.1"; JSESSIONID=1310674633500msE1CFA7B08138FBC7B9618EC2273CC269.node02; SPRING_SECURITY_REMEMBER_ME_COOKIE=amVzc2UuaGFsbGV0dDoxMzExODg0MjMzNTAwOmJiODI3YzRkZGU3ZjRmMTA2NjZkYzJmMjBiYmY3YWFj; jive.user.loggedIn=true; demoStyle=mainCSS; __utma=194160894.284716925.1284424263.1310689961.1310753078.814; __utmb=194160894.5.10.1310753078; __utmc=194160894; __utmz=194160894.1310753078.814.65.utmcsr=jira.jivesoftware.com|utmccn=(referral)|utmcmd=referral|utmcct=/browse/JIVE-4471
Host: brewspace.jiveland.com
Referer: https://brewspace.jiveland.com/content
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.56 Safari/535.1
X-J-Token: 39fd41326e56a1e0a075
X-Requested-With: X