Last active
August 10, 2021 03:58
-
-
Save peerreynders/946e03d7f3a3df6c4971c2e9888e9003 to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
**Very Rough** approximation of | |
fizzbuzz :: Int → String | |
fizzbuzz n = (test 3 "fizz" ◦ test 5 "buzz") id (show n) | |
where | |
test d s x | n ‘mod‘ d ≡ 0 = const (s ++ x "") | |
| otherwise = x | |
test :: Int → String → (String → String) → String → String | |
from Maciej Piróg's | |
"FizzBuzz in Haskell by Embedding a Domain-Specific Language" | |
https://themonadreader.files.wordpress.com/2014/04/fizzbuzz.pdf | |
*/ | |
function fizzbuzz(n) { | |
const test = (d, s) => | |
x => n % d === 0 ? | |
_dontCare => s + x('') : | |
x; | |
const fb = compose(test(3, 'fizz'), test(5, 'buzz')); | |
return fb(id)(show(n)); | |
} | |
// polyfills | |
function show(n) { | |
// convert argument to a string | |
return n.toString(); | |
} | |
function compose (g,f) { | |
// compose functions | |
return x => g(f(x)); | |
} | |
function id(x) { | |
// identity function | |
return x; | |
} | |
// script | |
for(let i=1; i <= 100; ++i){ | |
console.log(fizzbuzz(i)); | |
} |
This file contains 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
/* | |
Ported C++ version from | |
"The Forgotten Art of Structured Programming" - Kevlin Henney | |
https://youtu.be/SFv8Wm2HdNM?t=2520 | |
*/ | |
function fizzbuzz(n) { | |
const test = (d, s) => (fn) => n % d === 0 ? (_ignore) => s + fn('') : fn; | |
const fizz = test(3, 'fizz'); | |
const buzz = test(5, 'buzz'); | |
const id = (s) => s; | |
return fizz(buzz(id))(n.toString()); | |
} | |
// script | |
for (let i = 1; i <= 100; ++i) { | |
console.log(fizzbuzz(i)); | |
} |
This file contains 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
// Simplified JS version | |
function makeFizzbuzz() { | |
const fizz = test(3, 'fizz'); | |
const buzz = test(5, 'buzz'); | |
const show = v => v.toString(); | |
return n => { | |
const tuple = [n, show]; | |
const [, f] = fizz(buzz(tuple)); | |
return f(n); | |
}; | |
} | |
function test(d,s) { | |
return x => { | |
const [n, f] = x; | |
return n % d === 0 ? [n, _ => s + f('')] : x; | |
}; | |
} | |
// script | |
const fizzbuzz = makeFizzbuzz(); | |
for(let i=1; i <= 100; ++i){ | |
console.log(fizzbuzz(i)); | |
} |
This file contains 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
// More imperative JS version | |
function fizzbuzz(n) { | |
return (fizz(buzz([n, show]))[1])(n); | |
} | |
function buzz(tuple) { | |
const [n, f] = tuple; | |
return n % 5 === 0 ? [n, _ => 'buzz'] : tuple; | |
} | |
function fizz(tuple) { | |
const [n, f] = tuple; | |
return n % 3 === 0 ? [n, _ => 'fizz' + f('')] : tuple; | |
} | |
function show(v) { | |
return v.toString(); | |
} | |
// script | |
for(let i=1; i <= 100; ++i){ | |
console.log(fizzbuzz(i)); | |
} |
This file contains 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
// Another derivative JS version | |
function fizzbuzz(n) { | |
let record = { | |
key: 'result', | |
n, | |
alt: '', | |
result: n.toString(), | |
}; | |
record = test(3, 'fizz', test(5, 'buzz', record)); | |
return record[record.key]; | |
} | |
function test(d, s, record) { | |
const {n, alt} = record; | |
return n % d === 0 ? | |
{ ...record, key: 'alt', alt: s + alt } : | |
record; | |
} | |
// script | |
for(let i=1; i <= 100; ++i){ | |
console.log(fizzbuzz(i)); | |
} |
This file contains 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
// Stripped down even further | |
function fizzbuzz(n) { | |
let result = n.toString(); | |
let alt = ''; | |
const test = (d, s) => { | |
if (n % d !== 0) return; | |
alt += s; | |
result = alt; | |
}; | |
test(3, 'fizz'); | |
test(5, 'buzz'); | |
return result; | |
} | |
// script | |
for(let i=1; i <= 100; ++i){ | |
console.log(fizzbuzz(i)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment