Last active
February 2, 2017 21:44
-
-
Save Xananax/c411e45490a62c45bd905f2fee954e62 to your computer and use it in GitHub Desktop.
FizzBuzz things
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
const multiple = m => n => n % m === 0 | |
const getStringIfPredicate = (predicate,str) => n => predicate(n) ? str : '' | |
const fizz = getStringIfPredicate(multiple(3),'fizz') | |
const buzz = getStringIfPredicate(multiple(5),'buzz') | |
const fizzbuzz = n => | |
( n | |
? fizz(n)+buzz(n) || n+'' | |
: '0' | |
) | |
module.exports = () => fizzbuzz | |
/* TEST * / | |
const assert = require('assert') | |
const test = | |
[ [ fizzbuzz(1) , '1' ] | |
, [ fizzbuzz(3) , 'fizz' ] | |
, [ fizzbuzz(6) , 'fizz' ] | |
, [ fizzbuzz(5) , 'buzz' ] | |
, [ fizzbuzz(0) , '0' ] | |
, [ fizzbuzz() , '0' ] | |
, [ fizzbuzz(15), 'fizzbuzz' ] | |
].forEach( | |
([left,right],i) => | |
{ try | |
{ assert(left === right) | |
} | |
catch(e) | |
{ console.error(`ERROR: Item ${i} does not match!\n\t(left: '${left}', right: '${right}')`) | |
; process.exit(1) | |
} | |
} | |
) | |
/**/ |
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
const isMultiple = (m,n) => n % m === 0 | |
const fizzbuzz = n => | |
( !!!n | |
? '0' | |
: ( isMultiple(5,n) && isMultiple(3,n) | |
? 'fizzbuzz' | |
: ( isMultiple(5,n) | |
? 'buzz' | |
: ( isMultiple(3,n) | |
? 'fizz' | |
: n+'' | |
) | |
) | |
) | |
) |
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
import Html exposing (text) | |
import List exposing (map,range) | |
import String exposing (isEmpty,join,concat) | |
main = | |
map fizzbuzz (range 1 100) |> join " " |> text | |
isMultiple : Int -> Int -> Bool | |
isMultiple m n = | |
if n % m == 0 then True else False | |
getStringIfPredicate : String -> (Int -> Bool) -> Int -> String | |
getStringIfPredicate res predicate subject = | |
if predicate subject then | |
res | |
else | |
"" | |
fizz : Int -> String | |
fizz = getStringIfPredicate "fizz" (isMultiple 3) | |
buzz : Int -> String | |
buzz = getStringIfPredicate "buzz" (isMultiple 5) | |
fizzbuzz : Int -> String | |
fizzbuzz n = | |
let | |
response = (fizz n)++(buzz n) | |
in | |
if | |
isEmpty response | |
then | |
toString n | |
else | |
response |
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
import Html exposing (text) | |
import List exposing (map,range) | |
import String exposing (join) | |
type FizzBuzzResponse | |
= Fizz | |
| Buzz | |
| FizzBuzz | |
| NoFizzBuzz Int | |
main = | |
map fizzbuzz (range 1 100) |> join " " |> text | |
isMultiple : Int -> Int -> Bool | |
isMultiple m n = | |
if n % m == 0 then | |
True | |
else | |
False | |
getFizzBuzz : Int -> FizzBuzzResponse | |
getFizzBuzz n = | |
if (isMultiple 5 n) && (isMultiple 3 n) then | |
FizzBuzz | |
else if isMultiple 3 n then | |
Fizz | |
else if isMultiple 5 n then | |
Buzz | |
else | |
NoFizzBuzz n | |
fizzbuzzToString : FizzBuzzResponse -> String | |
fizzbuzzToString response = | |
case response of | |
FizzBuzz -> | |
"fizzbuzz" | |
Fizz -> | |
"fizz" | |
Buzz -> | |
"buzz" | |
NoFizzBuzz n -> | |
toString n | |
fizzbuzz : Int -> String | |
fizzbuzz n = | |
fizzbuzzToString (getFizzBuzz n) | |
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
import Html exposing (text) | |
import List exposing (map,range) | |
import String exposing (join) | |
main = | |
map fizzbuzz (range 1 100) |> join " " |> text | |
fizzbuzz num = | |
if num % 15 == 0 then | |
"fizzbuzz" | |
else if num % 3 == 0 then | |
"fizz" | |
else if num % 5 == 0 then | |
"buzz" | |
else | |
toString num |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment