Skip to content

Instantly share code, notes, and snippets.

@martinsson
martinsson / WebserviceHelper.java
Created February 5, 2011 11:36
Code puzzle : procedure duplication
package code.puzzles;
public class WebserviceHelper {
private final Webservice webservice = new Webservice();
public Products getProducts(final Integer category, final Integer quantity) throws WebserviceException{
Products products;
try {
products = webservice.getProducts(category, quantity);
} catch(RuntimeException e) {
throw new WebserviceException("failed invoking the webservice with category "+category+" and quantity "+quantity, e);
//webservicehelper with closures and generics
package code.puzzles;
public class WebserviceHelper {
private final Webservice webservice = new Webservice();
public Products getProducts(final Integer category, final Integer quantity) throws WebserviceException{
WebserviceInvoker<Products> invoker = new WebserviceInvoker<Products>() {
public Products invoke() {
return webservice.getProducts(category, quantity);
}
@martinsson
martinsson / gist:864943
Created March 10, 2011 21:16
ocp minimaliste
public class BillFormatter {
public String format(int price, Currency currency) throws UnknownCurrencyException {
String formattedPrice = "Your total is : ";
if (currency == Currency.EUROS) {
formattedPrice += price + "€";
} else if (currency == Currency.POUNDS) {
formattedPrice += "£" + price;
} else {
throw new UnknownCurrencyException();
@martinsson
martinsson / gist:923217
Created April 16, 2011 15:49
A solution to the duplication puzzle at the Grenoble Dojo
package code.puzzles;
import code.puzzles.TheRestOfTheCode.Products;
import code.puzzles.TheRestOfTheCode.Rebates;
import code.puzzles.TheRestOfTheCode.Webservice;
import code.puzzles.TheRestOfTheCode.WebserviceException;
public class WebserviceHelper {
private Webservice webservice;
public Products getProductsByCategoryAndQuantity(final Integer category, final Integer quantity) throws WebserviceException{
@martinsson
martinsson / ActionTest.php
Created July 3, 2011 10:24
Test setup for Symfony actions
<?php
class ActionTest extends PHPUnit_Framework_TestCase {
protected $action;
public function setUp() {
$debug = true;
$configuration = ProjectConfiguration::
getApplicationConfiguration('frontend', 'test', $debug);
$sfContext = sfContext::createInstance($configuration);
@martinsson
martinsson / BadCrawler.java
Created October 18, 2011 20:47
Simple != Simple to use
public class BatchCrawler {
public BatchCrawler(Job job) {
this.job = job;
this.timeout = ConfigurationService.getInt(SCAN_TIMEOUT);
httpCrawler = new RetryingHttpCrawler();
}
public CrawlResult crawl() {
CrawlResult crawlResult = new CrawlResult();
@martinsson
martinsson / medics.clj
Created October 19, 2011 13:16
Code from the grenoble dojo
(ns medics
(:use [clojure-tdd.core] :reload)
(:use [clojure.test])
(:use [midje.sweet]))
(unfinished box-size)
(defn ciel-quot [num div]
(inc (quot (dec num) div)))
@martinsson
martinsson / gist:1379537
Created November 19, 2011 23:32
Solution to code story
(def number)
(defn- for-all [fbq-digits text]
(let [extra-fbqs (for [d (str number) :when (fbq-digits d)] (fbq-digits d))]
(concat text extra-fbqs)))
(defn if-divisible-by [[div suffix] text]
(if (zero? (rem number div))
(conj text suffix)
text))
@martinsson
martinsson / gist:1639222
Created January 19, 2012 10:11
Solution si jamais on ne peut pas modifier InnerManager
import static org.hamcrest.Matchers.contains;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
@martinsson
martinsson / core.clj
Created January 19, 2012 14:30
clojure solution to checkout-kata
(ns lean-challenge.core
(:use [midje.sweet]))
; ==== application core
(defn cost [fruit]
({"b" 150 "a" 100 "p" 100 "m" 100 "c" 75} fruit))
(defn csv-to-col [s]
(seq (.split s ",")))