Last active
August 29, 2015 14:27
-
-
Save atsuya046/b0e118f2402cf0bbfcf4 to your computer and use it in GitHub Desktop.
Fizz Buzz with Typed Clojure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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