Skip to content

Instantly share code, notes, and snippets.

View tolitius's full-sized avatar

Anatoly tolitius

View GitHub Profile
@tolitius
tolitius / src.balanced-check.clj
Last active September 29, 2015 23:28
checks if a given code snippet parentheses are balanced
(ns balance-check.core)
(defn balanced? [s]
(-> (reduce (fn [[h & r :as stack] c]
(case c
\( (conj stack 42)
\) (if (= h 42)
r
(conj stack "unbalanced"))
stack))
@tolitius
tolitius / src.multi-method.clj
Created January 29, 2012 07:04
clojure bootcamp: "defmulti" example
;; inspired by suggestion in THE doc: http://java.ociweb.com/mark/clojure/article.html
(ns bootcamp.multi-method)
(defn measure-it [size]
(cond
(< size 3) :small
(< size 6) :medium
(< size 12) :large
:else :hard-to-measure ))
@tolitius
tolitius / bootcamp.factorial.clj
Created February 2, 2012 04:36
clojure bootcamp: loop/recur vs. reduce vs. recursion
(ns bootcamp.factorial)
(defn fast-factorial [number]
(loop [n number factorial 1]
(if (zero? n)
factorial
(recur (- n 1) (* factorial n)))))
(defn fast-no-loop-factorial
([number] (fast-no-loop-factorial number 1))
@tolitius
tolitius / zunionstore.py
Created February 27, 2012 06:20
Redis 'zunionstore' example for Chariot Day
import redis
r = redis.StrictRedis(host='localhost', port=6379, db=0)
r.zadd('votes:east', **{'riak': 1, 'couchdb': 1, 'cassandra': 1})
r.zadd('votes:north', **{'redis': 1, 'onetick': 1, 'couchdb': 1})
r.zadd('votes:west', **{'redis': 1, 'riak': 1, 'couchdb': 1})
r.zadd('votes:south', **{'voltdb': 1, 'mongodb': 1, 'hazelcast': 1})
r.zadd('votes:north:east', **{'hazelcast': 1, 'riak': 1, 'redis': 1})
r.zadd('votes:north:west', **{'redis': 1, 'cassandra': 1, 'onetick': 1})
@tolitius
tolitius / nosql-top-dogs.js
Created February 27, 2012 06:26
MongoDB Map/Reduce for Chariot Day
db.nosqlrep.insert( { _id : 1, votes : ['riak', 'couchdb', 'cassandra'] } );
db.nosqlrep.insert( { _id : 2, votes : ['redis', 'onetick', 'couchdb'] } );
db.nosqlrep.insert( { _id : 3, votes : ['redis', 'riak', 'couchdb'] } );
db.nosqlrep.insert( { _id : 4, votes : ['voltdb', 'mongodb', 'hazelcast'] } );
db.nosqlrep.insert( { _id : 5, votes : ['hazelcast', 'riak', 'redis'] } );
db.nosqlrep.insert( { _id : 6, votes : ['redis', 'cassandra', 'onetick'] } );
db.nosqlrep.insert( { _id : 7, votes : [] } );
print( "\ncommunity votes" )
print( "---------------" )
@tolitius
tolitius / validator-test.groovy
Created March 7, 2012 09:19
Groovy POGO Validation [adopted some time ago from a no longer existing Spock's groovy extensions]
class Person {
@Require( { it ==~ /[a-z A-Z]*/ } )
String name
@Require( { it in (0..130) } )
int age
}
def validator = new Validator()
@tolitius
tolitius / Makefile
Created April 3, 2012 18:08
Given a host name and the port, checks if the connection can be established
CFLAGS=-Wall -g
all: clean
make ccheck
clean:
rm -f ccheck
rm -rf ccheck.dSYM
@tolitius
tolitius / TrainSpec.scala
Created January 24, 2013 15:57
едем из Харькова в Париж через Сан Франциско
package com.typesafe.training.scalatrain
import org.specs2.mutable._
class TrainSpec extends Specification {
val kharkovStation = Station("Харьков")
val sfStation = Station("Сан Франциско")
val parisStation = Station("Париж")
val t1 = Time(11, 11)
def stopsAt(station: Station) = {
for {
train <- trainsAt( station )
schedule <- train.schedule.filter(_._2 == station)
} yield (schedule._1, train)
}
@tolitius
tolitius / MakingLove.scala
Created January 24, 2013 20:44
A Story of Human Multiple Inheritance and Accidental Override of Love
scala> trait Maker { def make: String }
defined trait Maker
scala> trait HumanMaker extends Maker { def make = "Human" }
defined trait HumanMaker
scala> trait LoveMaker extends Maker { override def make = "Love" }
defined trait LoveMaker
scala> class Human extends HumanMaker with LoveMaker