LoginSignup
2
1

More than 5 years have passed since last update.

LiveScript で FizzBuzz

Last updated at Posted at 2016-05-03

素直に書いた場合

fizzbuzz = ->
  | it % (3 * 5) is 0 => \FizzBuzz
  | it % 3 is 0 => \Fizz
  | it % 5 is 0 => \Buzz
  | _ => it
[1 to 30] |> each fizzbuzz >> console~log

is_multiple_of関数を作って抽出する

回答1 (バッククォートを想定)

is_a_multiple_of = (a, b)-> a % b is 0
fizzbuzz = ->
  | it `is_a_multiple_of` (3 * 5) => \FizzBuzz
  | it `is_a_multiple_of` 3 => \Fizz
  | it `is_a_multiple_of` 5 => \Buzz
  | _ => it
[1 to 30] |> each fizzbuzz >> console~log

回答2 (パイピングを想定)

is_a_multiple_of = (b, a)--> a % b is 0
fizzbuzz = ->
  | it |> is_a_multiple_of (3 * 5) => \FizzBuzz
  | it |> is_a_multiple_of 3 => \Fizz
  | it |> is_a_multiple_of 5 => \Buzz
  | _ => it
[1 to 30] |> each fizzbuzz >> console~log

課題

関数を作る際、バッククォートを想定して作るか、パイピングでの利用を想定して作るかで引数の宣言順番が異なります。
そもそもコンセプトが相反するものなので両対応のものは出来なさそうです。

どっちでも作れそうな時はどっちが美しいんでしょうかね?
ぶっちゃけ好みの問題な気もしますが…

カリー化してある関数のほうがLiveScriptではパイピングや関数合成でポンポン値を変化させられますし、prelude-lsのほぼ全ての関数がパイピングでの利用を想定しています。
そう考えるとパイピング想定の方が素直な気もしますね。

でも、is_multiple_of関数なんて発展性のない2引数しか使わないので、やはりバッククォートを使うべきか?
意外と悩ましい問題です。

2
1
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
1