Skip to content

Instantly share code, notes, and snippets.

View jessitron's full-sized avatar
🚀
"I'm in"

Jessica Kerr jessitron

🚀
"I'm in"
View GitHub Profile
@jessitron
jessitron / speakerStats.scala
Created October 18, 2012 16:36
Play with the CodeMash API to analyze gender distribution of speakers
import scala.xml._
// curl http://rest.codemash.org/api/speakers > speakers.xml
val speakers = xml.XML.loadFile("codemashSpeakers.xml") \ "Speaker"
// curl http://rest.codemash.org/api/sessions > sessions.xml
val sessions = xml.XML.loadFile("sessions.xml") \ "Session"
val masculineWords = List("he","his")
val feminineWords = List("she","her","hers","lady")
def bioContainsAnyWord( strings : Seq[String], speakerNode : Node) =
@jessitron
jessitron / transformers
Created January 14, 2013 00:06
These methods perform transformations on their input. They'll be tied together in different ways separately. Look for the blog post at blog.jessitron.com.
// from https://github.com/jessitron/BlogCode/blob/master/optionAsFlow.scala
object transformers {
def openFile(n: String) : Option[java.io.File] = {
val f = new java.io.File(n) // catch IOException
if (f.exists)
Some(f)
else
None
@jessitron
jessitron / gist:4526987
Created January 14, 2013 00:22
The transformers now return a Result, with more detail on failure than an Option holds. See blog.jessitron.com for the purpose of this post.
// from https://github.com/jessitron/BlogCode/blob/master/optionToCustom.scala
object transformers {
def openFile(n: String) : Result[java.io.File] = {
val f = new java.io.File(n) // catch IOException
if (f.exists)
Success(f)
else
Failure("File does not exist: " + n)
@jessitron
jessitron / gist:5129618
Created March 10, 2013 17:45
Ruby: Hash.each treats blocks and lambdas differently when the arity is 2
#
# Ruby 2.0 hashes: they treat lambdas inconsistently from blocks.
# Pass in a block straight-up, and each (or each_pair) looks at the arity of the block.
# A block that accepts one argument gets an array of two elements, key and value.
# A block that accepts two arguments gets the key and value separately. How nice.
#
# But, if you pass in a lambda as a block, you always get just one argument.
# No key-value separation for you!
#
@jessitron
jessitron / gem_cleaner.rb
Last active December 17, 2015 05:09 — forked from kgrz/gem_cleaner.rb
require 'bundler'
# Required for Bundler::LockfileParser. This can be empty though.
`touch Gemfile` unless File.exists?("Gemfile")
# Add the paths to the Gemfile.lock files, the gems in which
# need to be protected
@jessitron
jessitron / ExplicitErrorReturning
Created June 15, 2013 17:58
This is in response to a giant twitter thread. Thanks to @CraigBuchek, @brianbuttonxp, @adkron, @heathborders, and @marioaquino for the discussion. Post is at blog.jessitron.com, titled "What's dirtier than comments? Exceptions!"
case class Error(message:String)
case class AuthLevel(value: Int)
object AuthLevel {
val GUEST = AuthLevel(0)
}
class SettingGetter {
def get_setting(settingName: String): Either[Error, String] = Right("Fred")

Writing jQuery Plugins for Fun and (non)Profit

We all love jQuery - it's easy, efficient, and there's tons of information online. But with that mass of information comes confusion: When you want to abstract some of your code into a plugin, how do you structure it? How do you take in options, and how can you be sure you haven't broken the way jQuery works? And when you're done, where do you put it and how do distribute it to the masses?

This talk will run through some of the basics and guide you in the right direction. In particular, we'll cover:

  • structuring your plugin code
  • making your code testable - and testing it!
  • tracking your code on github
  • getting your plugin on the jQuery site
@jessitron
jessitron / gist:8376139
Created January 11, 2014 20:15
scala: print all URLs on classpath
def urlses(cl: ClassLoader): Array[java.net.URL] = cl match {
case null => Array()
case u: java.net.URLClassLoader => u.getURLs() ++ urlses(cl.getParent)
case _ => urlses(cl.getParent)
}
val urls = urlses(getClass.getClassLoader)
println(urls.filterNot(_.toString.contains("ivy")).mkString("\n")
@jessitron
jessitron / gist:8777503
Created February 3, 2014 01:09
The magic of blocking { ... } in Scala's global ExecutionContext: it leads to the creation of more threads, so the CPUs don't get bored
val des = scala.concurrent.ExecutionContext.global
import scala.concurrent._
import duration._
def ct = Thread.currentThread.getName
val n = Runtime.getRuntime.availableProcessors
def hogThread(sec:Int) = future {
@jessitron
jessitron / gist:9191499
Created February 24, 2014 16:24
StackOverflow in scalaz-stream. My trampoline is not bouncy enough
import scalaz.stream._
import Process._
import scalaz.concurrent.Task
import scala.concurrent.duration._
// git bisect identifies the offending commit as
// https://github.com/scalaz/scalaz-stream/commit/721716ed7af0c126593e9ee227c0f36f21c5b7ed
object Test {