Skip to content

Instantly share code, notes, and snippets.

import scalaz._
import Scalaz._
import simulacrum._
import stalactite._
import scala.concurrent.Future
import scala.io.StdIn.readLine
object Main {
trait Terminal[C[_]] {
import scalaz._
import Scalaz._
import simulacrum._
import stalactite._
import scala.concurrent.Future
class Main {
trait Terminal[C[_]] {
def read: C[String]
abstract class Element(text: String) {
def accept(visitor: Element => Unit): Unit =
visitor(this)
}
case class Title(text: String) extends Element(text)
case class Text(text: String) extends Element(text)
case class Hyperlink(text: String, val url: String) extends Element(text)
trait Visitor {
def visit(title: Title)
def visit(text: Text)
def visit(hyperlink: Hyperlink)
}
abstract class Element(val text: String) {
def accept(visitor: Visitor)
}
@philipschwarz
philipschwarz / Listing_4_7_GeneratePrime.java
Last active July 19, 2017 05:43
Robert Martin in clean code: "I wrote the module in Listing 4-7...[as an] example of bad coding and commenting style. Kent Beck then refactored this code into a much more pleasant form in front of several dozen enthusiastic students. ...See how many different comment problems you can find...In Listing 4-8 you can see a refactored version"
/**
* This class Generates prime numbers up to a user specified
* maximum. The algorithm used is the Sieve of Eratosthenes.
* <p>
* Eratosthenes of Cyrene, b. c. 276 BC, Cyrene, Libya --
* d. c. 194, Alexandria. The first man to calculate the
* circumference of the Earth. Also known for working on
* calendars with leap years and ran the library at Alexandria.
* <p>
* The algorithm is quite simple. Given an array of integers
public class Address {
private final String addressLine; // never null
private final String city; // never null
private final String postcode; // optional, thus may be null
// constructor ensures non-null fields really are non-null
// optional field can just be stored directly, as null means optional
public Address(String addressLine, String city, String postcode) {
this.addressLine = Preconditions.chckNotNull(addressLine);
import org.jooq.lambda.tuple.Tuple2;
import static java.util.stream.Stream.iterate;
import static org.jooq.lambda.tuple.Tuple.tuple;
public class Main
{
public static void main(String[] args)
{
iterate(
package test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.LineNumberReader;
import java.util.Collections;
import java.util.Iterator;
(defmulti area
"Calculate the area of a shape"
:type)
(defmethod area :rectangle [shape]
(* (:length shape) (:width shape)))
(defmethod area :circle [shape]
(* (. Math PI) (:radius shape) (:radius shape)))
@philipschwarz
philipschwarz / Java8SyntacticSugar.java
Last active August 29, 2015 14:13
tasting the lambda syntactic sugar that the Java 8 Compiler lets you write in place of an instance of a required functional interface
IntStream.of(1, 2, 3).forEach( new IntConsumer() { public void accept(int value) { System.out.println(value); } } );
IntStream.of(1, 2, 3).forEach( new IntConsumer() { public void accept(int n) { System.out.println(n); } } );
IntStream.of(1, 2, 3).forEach( (int n) -> { System.out.println(n); } );
IntStream.of(1, 2, 3).forEach( (int n) -> System.out.println(n) );
IntStream.of(1, 2, 3).forEach( n -> System.out.println(n) );
IntStream.of(1, 2, 3).forEach( System.out::println );