Skip to content

Instantly share code, notes, and snippets.

@joseraya
joseraya / life.clj
Created December 3, 2011 22:34
Game of life clojure implementation
(ns life.test.core
(:use [life.core])
(:use [clojure.test]))
(def alive 1)
(def dead 0)
(defn cell-at [board x y]
(nth (nth board y []) x 0))
@joseraya
joseraya / Roman.scala
Created April 14, 2012 15:02
Roman Numerals Kata
package kata
import org.scalatest.matchers.ShouldMatchers
import org.scalatest.{BeforeAndAfterEach, FlatSpec}
class Roman extends FlatSpec with ShouldMatchers {
case class Symbol(latin:Int, roman:String)
var symbols = List(Symbol(5,"V"), Symbol(4, "IV"), Symbol(1,"I"))
@joseraya
joseraya / Roman.scala
Created April 14, 2012 15:47
Roman Numerals Scala (2)
A more functional version would be:
package kata
import org.scalatest.matchers.ShouldMatchers
import org.scalatest.{BeforeAndAfterEach, FlatSpec}
class Roman extends FlatSpec with ShouldMatchers {
case class Symbol(latin:Int, roman:String)
var symbols = List(Symbol(5,"V"), Symbol(4, "IV"), Symbol(1,"I"))
@joseraya
joseraya / build.sbt
Created April 17, 2012 09:46
Empty sbt-idea project
organization := "com.agilogy"
name := "<<whatever>>"
version := "0.0.1"
scalaVersion := "2.9.1"
libraryDependencies += "org.scalatest" %% "scalatest" % "1.7.1" % "test"
@joseraya
joseraya / core_test.clj
Created June 8, 2012 12:33
Encapsulation in clojure
(ns encapsulation.core-test
(:use midje.sweet)
(:require [clj-time.core :as time]))
;My point here is to show how we can achieve encapsulation in clojure
;I assume that we may need encapsulation for two purposes:
; - Hide the internal representation of data (so that it can change)
; - Ensure the consistency of the data (by applying integrity constraints)
;Let's say that we have a "class" Person with an age.
@joseraya
joseraya / watch
Created November 26, 2012 12:31
Watch a directory and execute something whenever a file is changed
#!/bin/sh
check() {
chsum1=chsum2
while [[ true ]]
do
chsum2=`find . -type f -exec md5 {} \;`
if [[ $chsum1 != $chsum2 ]] ; then
echo "Change detected ..."
@joseraya
joseraya / MockedApp.scala
Created October 3, 2013 18:57
Play framework mocked application: All the controllers are mocks and do nothing. Useful for testing routes. Defines a mocekdApp method that should be used like: "wathever" in mockedapp { //here you can call route(FakeRequest(...)) and use Mockito.verify or mustInvoke[Controllerclass].method(params) }
import play.api.test.Helpers._
import play.api.test._
import play.api.mvc.Result
import play.api.libs.json.JsValue
import play.api.libs.json.Json
import play.api.GlobalSettings
import org.mockito.stubbing.OngoingStubbing
import scala.reflect.ClassTag
import org.specs2.mock.Mockito
import play.api.mvc.Call
@joseraya
joseraya / ConfigureBitbucketHookForJenkins.md
Created October 5, 2013 08:19
Configure Bitbucket hook for jenkins

In order to configure the bitbucket hook we need two things: The user's API Token and the project token. In order to retrieve the first one whe should click on our username (on the top right corner of the page), click on configure and then click on "Show API Token". This API token will be used as password for our user.

For the project's token we need to go to the job configuration (/job/{job-name}/configure), "build triggers" and enable "trigger remote builds" and, there, input some random text as token.

Once we have these two tokens we can go to bitbucket, repo administration, hooks section, and add a new jenkins hook that we will configure like:

  • Endpoint: http://{username}:{APIToken}@{hostname}/{prefix_if_you_have_one}
  • Module name: Whatever (I usually leave this one blank)
  • Project name: The job name
  • Token: The project token
@joseraya
joseraya / snapshot-crawler.js
Created January 21, 2014 20:15
A node script that crawls a web site and stores snapshots (taken with zombie.js) to the file system. Based on code from this article: http://www.ng-newsletter.com/posts/serious-angular-seo.html
var Browser = require('zombie'),
url = require('url'),
fs = require('fs'),
$q = require('Q'),
saveDir = __dirname + '/_snapshots';
var scriptTagRegex = /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;
var stripScriptTags = function(html) {
@joseraya
joseraya / parallel.js
Created March 26, 2014 15:13
promises fun
var Q = require('q');
function printNumber(n) {
return Q.fcall(function () {
console.log("Number: ", n);
return n;
});
}
function printNumbers(numbers) {