0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

LiveScript版FizzBuzz

Last updated at Posted at 2017-12-21

ES2015 が登場して以降どうしても出番が減ってきている…という以前からそもそもマイナー感がある
LiveScript です。
自分も最近あまり書いていませんでしたのでなんだかちょっと新鮮です。

まずはシンプルに書いてみます。

is-multiple-of = (n, m) -> n % m is 0
fizzbuzz = ->
  | it `is-multiple-of` 15 => \FizzBuzz
  | it `is-multiple-of` 3  => \Fizz
  | it `is-multiple-of` 5  => \Buzz
  | _                      => it + ''

[1 to 100]
  |> map fizzbuzz
  |> each console~log

次は ES6でFizzBuzz のやり方を参考してやってみます。

bind = (f, g) -> (n) -> f(n) ? g(n)
is-multiple-of = (n, m) -> n % m is 0

fizzbuzz = -> if it `is-multiple-of` 15 => \FizzBuzz
fizz     = -> if it `is-multiple-of` 3  => \Fizz
buzz     = -> if it `is-multiple-of` 5  => \Buzz
num      = (+ '')

[1 to 100]
  |> map (fold1 bind, [fizzbuzz, fizz, buzz, num])
  |> each console~log

最後はちょっと長いけど今の自分に思いつく範囲でワンライナー。

[1 to 100]|>map((n)->(find (->it.0 n),[[(%15)>>(is 0),\Fizzbuzz] [(%3)>>(is 0),\Fizz] [(%5)>>(is 0),\Buzz]])?1 or n+'')|>each console~log

以上です。

FizzBuzz もたまに書くと楽しいですね。

0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?