Skip to content

Instantly share code, notes, and snippets.

View mgdelacroix's full-sized avatar
🎧
Working from home

Miguel de la Cruz mgdelacroix

🎧
Working from home
View GitHub Profile
@mgdelacroix
mgdelacroix / freqElement.groovy
Last active August 29, 2015 13:56
Most frequent element in a groovy list
// If there are two elements with the same freq, this code will take the first one
[1,2,2,3,3,3,4,4,4,4]
.countBy { it }
.max { it.value }
.key
@mgdelacroix
mgdelacroix / compareAndAppend.groovy
Created March 5, 2014 08:48
Append nonexistent keys of one properties file to another
def getPropertyFileKeys = { File file ->
file.readLines().collectEntries { Strin line ->
line.split('=').with{ [(first()):last()] }
}.keySet()
}
def compareAndAppend = { File source, File destination ->
sourceKeys = getPropertyFileKeys(source)
destinationKeys = getPropertyFileKeys(destination)
sourceKeys.each { key ->
@mgdelacroix
mgdelacroix / extending-handler.groovy
Created March 31, 2014 12:29
Testing how to extend a Ratpack handler
@Grab("io.ratpack:ratpack-groovy:0.9.2")
import static ratpack.groovy.Groovy.*
import ratpack.handling.Handler
import ratpack.handling.Context
ratpack {
handlers {
get {
println "hola mundo ${new Date()}"
context.insert(new MyHandler())
@mgdelacroix
mgdelacroix / handler-insert-test.groovy
Created March 31, 2014 13:01
Testing the Ratpack handler chain
@Grab("io.ratpack:ratpack-groovy:0.9.2")
import static ratpack.groovy.Groovy.*
import ratpack.handling.Handler
import ratpack.handling.Context
import static groovy.json.JsonOutput.toJson
import static ratpack.registry.Registries.registry
ratpack {
@mgdelacroix
mgdelacroix / basic-handler.groovy
Created March 31, 2014 17:15
Basic Ratpack application with a handler
@Grab("io.ratpack:ratpack-groovy:0.9.2")
import static ratpack.groovy.Groovy.*
import ratpack.handling.Handler
import ratpack.handling.Context
ratpack {
handlers {
get new TestHandler()
}
}
@mgdelacroix
mgdelacroix / autoloading.rb
Created April 13, 2014 17:57
Autoloading for rails 4 lib folder
# In config/application.rb
config.watchable_dirs['lib'] = [:rb]
# When requiring something, use require_dependency
# To use lib/myclass.rb
require_dependency 'myclass'
@mgdelacroix
mgdelacroix / client.groovy
Created May 20, 2014 13:02
Simple Groovy client-server comunication
def socket = new Socket('localhost', 5000)
socket.withStreams { input, output ->
println input.newReader().readLine()
output << 'Imma socket'
}
@mgdelacroix
mgdelacroix / SimpleEitherScript.groovy
Last active August 29, 2015 14:03
Either and fmap simple (and not formal) implementation
enum EitherStatus {
LEFT, RIGHT
}
class Either {
EitherStatus status = EitherStatus.RIGHT
Map data
String toString() {
return "[${this.status}] > ${this.data}"
@mgdelacroix
mgdelacroix / SqlTest.groovy
Created July 16, 2014 12:18
Groovy simple sql test
@GrabConfig(systemClassLoader=true)
@Grab('com.h2database:h2:1.4.180')
import groovy.sql.Sql
Sql sql = Sql.newInstance("jdbc:h2:/tmp/devDb", 'sa', '')
println "=================== TEST CONTENT"
println '>> You can invoke this script with two args to insert into test table (id, name)'
println '>> You can invoke this script with the argument CREATE or DESTROY to create or destroy the test table'
@mgdelacroix
mgdelacroix / fate_roll.py
Created August 16, 2014 11:00
A simple python script to calculate a FATE dice roll http://www.evilhat.com/home/fate-core/
import random
from functools import reduce
def to_fate(result):
if result < 3:
return '-'
elif result < 5:
return ' '
else:
return '+'