Skip to content

Instantly share code, notes, and snippets.

Philip philipschwarz

Block or report user

Report or block philipschwarz

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View FizzBuzz.hs
fizzbuzz :: [String]
fizzbuzz =
let fizzes = cycle ["", "", "fizz"]
buzzes = cycle ["", "", "", "", "buzz"]
pattern = zipWith (++) fizzes buzzes
in zipWith combine pattern [1..] where
combine word number =
if null word then show number else word
View FoldableLaws.scala
import scalaz.std.vector.vectorMonoid
import scalaz.syntax.foldable.ToFoldableOps
import scalaz.syntax.semigroup._
import scalaz.std.list.listInstance
// Vector append and prepend functions
assert( Vector(1,2) :+ 3 == Vector(1,2,3) )
assert( 1 +: Vector(2,3) == Vector(1,2,3) )
View FunctionalAndReactiveDomainModelingCreatingAccountUsingSmartConstructors.scala
import java.util.{Date, Calendar}
import util.{Try, Success, Failure}
type Amount = BigDecimal
def today = Calendar.getInstance.getTime
case class Balance(amount: Amount = 0)
sealed trait Account {
View Main.scala
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.{Await, Future}
import scala.concurrent.duration._
import scala.io.StdIn.readLine
object Main {
trait Terminal[C[_]] {
def read: C[String]
def write(t: String): C[Unit]
View Main.scala
import scalaz._
import Scalaz._
import simulacrum._
import stalactite._
import scala.concurrent.Future
import scala.io.StdIn.readLine
object Main {
trait Terminal[C[_]] {
View Main.scala
import scalaz._
import Scalaz._
import simulacrum._
import stalactite._
import scala.concurrent.Future
import scala.io.StdIn.readLine
object Main {
trait Terminal[C[_]] {
View Main.scala
import scalaz._
import Scalaz._
import simulacrum._
import stalactite._
import scala.concurrent.Future
class Main {
trait Terminal[C[_]] {
def read: C[String]
View ScalaEnabledVisitorPattern.scala
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)
View TraditionalVisitorPattern.scala
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 Jul 19, 2017
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"
View Listing_4_7_GeneratePrime.java
/**
* 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
You can’t perform that action at this time.