Skip to content

Instantly share code, notes, and snippets.

@heuristicfencepost
heuristicfencepost / SAMTest.scala
Created January 7, 2011 05:58
Samples of SAM handling in JRuby and Scala
package org.fencepost
import scala.collection.mutable._
import org.scalatest.Suite
import java.util.concurrent._
class SAMTest extends Suite {
@heuristicfencepost
heuristicfencepost / ForkJoinTest.scala
Created February 4, 2011 07:21
Implementation of various Scala tests to demonstrate integration with Java's fork/join framework
package org.fencepost.forkjoin
import scala.collection.mutable.LinkedList
import jsr166y._
import org.scalatest.Suite
class ForkJoinTest extends Suite {
@heuristicfencepost
heuristicfencepost / Euler10.rb
Created March 4, 2011 05:15
Solution to Project Euler problem #10 in various languages
class Euler10
include Enumerable
def initialize()
@primearr = [2]
@nat = 3
end
def each
yield 2
@heuristicfencepost
heuristicfencepost / euler10.hs
Created March 7, 2011 07:10
A better Ruby implementation of Project Euler problem #10 and a horrible implementation in Haskell
module Main where
-- Definition of our candidate set
tens = map (*10) [1..]
urcandidate(x:xs) = map (+x) [1,3,7,9] ++ urcandidate(xs)
candidates = urcandidate(tens)
-- Define the logic to be used by our predicate and utilize currying to support partial application
urpred(candidate,prime) = prime > (truncate (sqrt (fromIntegral candidate))) || candidate `mod` prime == 0
pred = curry urpred
@heuristicfencepost
heuristicfencepost / ThriftCassandraClient.scala
Created May 25, 2011 04:53
Experiments in interacting with Cassandra via Thrift
package org.fencepost.cassandra
import scala.collection.JavaConversions
import org.apache.thrift.protocol.TBinaryProtocol
import org.apache.thrift.transport._
import org.apache.cassandra.service._
import org.apache.cassandra.thrift._
object ThriftCassandraClient {
@heuristicfencepost
heuristicfencepost / generate_random_data.rb
Created July 27, 2011 07:05
Scripts for computing max and min values for a field in MongoDB
require 'yaml'
def generate_random_name(len)
1.upto(len).map { (65 + (rand 26)).chr }.join()
end
NUM_AUTHORS=10000
AUTHOR_NAME_LENGTH=24
POSTS_PER_AUTHOR=100
MAX_PAGE_VIEWS_PER_POST=2000
@heuristicfencepost
heuristicfencepost / antitails.hs
Created August 3, 2011 03:04
Implementations of "tails" function in Data.List as well as a vaguely related function
antitails x = reverse (foldr f1 [] x) ++ [""]
where f1 newchar acc = [[newchar]] ++ (map (newchar:) acc)
@heuristicfencepost
heuristicfencepost / add_employee_data.clj
Created September 2, 2011 22:44
Clojure and Ruby apps to populate mock user data into a Cassandra database and then retrieve it
; Populate data for a set of random users to a Cassandra instance.
;
; Users consist of the following set of data:
; - a username [String]
; - a user ID [integer]
; - a flag indicating whether the user is "active" [boolean]
; - a list of location IDs for each user [list of integer]
;
; User records are keyed by username rather than user IDs, mainly because at the moment
; we only support strings for key values. The Cassandra API exposes keys as byte arrays
@heuristicfencepost
heuristicfencepost / akka_sample1.rb
Created September 13, 2011 02:15
Sample Ruby code for working with Akka in JRuby
require 'java'
require 'akka-actor-1.2-RC6.jar'
require 'scala-library.jar'
java_import 'akka.actor.UntypedActor'
java_import 'akka.actor.Actors'
# Start with something simple. Implement the actor as a distinct
# class and start it up within the Akka runtime.
@heuristicfencepost
heuristicfencepost / comprehension1.clj
Created November 26, 2011 01:16
Code samples for blog post on list comprehensions
(defn euclidean [x y] (Math/sqrt (+ (Math/pow x 2) (Math/pow y 2))))
(println (for [
[x,y]
[[1,2],[2,3],[3,4]]
:when (> (euclidean x y) 3.0)]
[(euclidean x y),(Math/toDegrees (Math/atan (/ y x)))]))