LoginSignup
2
0

More than 5 years have passed since last update.

ES6でFizzBuzz

Last updated at Posted at 2017-04-13

ES6の標準的な書き方でFizzBuzz書くメモ

関数をくっつけるためのシンタックスシュガーが標準で欲しい

  • 変更(4/15)ykztsさんがspread構文で書いたらシンプルになることを教えてくれました
fizzbuzz.js
const compose = ([f, ...fs]) => f? f(compose(fs)): (x) => x;
const num = () => (x) => x.toString();
const fizz = (f) => (x) =>  x % 3 == 0? 'Fizz': f(x);
const buzz = (f) => (x) =>  x % 5 == 0? 'Buzz': f(x);
const fizzbuzz = (f) => (x) => x % 3 == 0 && x % 5 == 0? 'FizzBuzz' : f(x);

const f = compose([fizzbuzz, buzz, fizz, num])

Array(100).fill().map((_, i) => f(i+1)).map(s => console.log(s));
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0