Skip to content

Instantly share code, notes, and snippets.

@atsuya046
Last active August 29, 2015 14:27
Show Gist options
  • Save atsuya046/b0e118f2402cf0bbfcf4 to your computer and use it in GitHub Desktop.
Save atsuya046/b0e118f2402cf0bbfcf4 to your computer and use it in GitHub Desktop.
Fizz Buzz with Typed Clojure
(ns fp.match
(:require [clojure.core.match :refer (match)]))
(defprotocol TFizzBuzz
(to-string [this]))
(deftype Fizz [number] TFizzBuzz
(to-string [this] "Fizz"))
(deftype Buzz [number] TFizzBuzz
(to-string [this] "Buzz"))
(deftype FizzBuzz [number] TFizzBuzz
(to-string [this] "FizzBuzz"))
(deftype Other [number] TFizzBuzz
(to-string [this] (str number)))
(defn matcher [x]
(match [(mod x 3) (mod x 5)]
[0 0] (FizzBuzz. x)
[0 _] (Fizz. x)
[_ 0] (Buzz. x)
:else (Other. x)))
(defn fizzbuzz [x]
(-> (matcher x)
(to-string)))
; user=> (fizzbuzz 1)
; "1"
; user=> (fizzbuzz 3)
; "Fizz"
; user=> (fizzbuzz 5)
; "Buzz"
; user=> (fizzbuzz 15)
; "FizzBuzz"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment