【激突】関数型言語 VS オブジェクト指向言語2at TECH
【激突】関数型言語 VS オブジェクト指向言語2 - 暇つぶし2ch341:デフォルトの名無しさん
12/04/14 05:12:11.99
F#で演算子オーバーロードなし。

let threadLocal = new System.Threading.ThreadLocal<_>()

let makeFizzBuzz name condition (x : obj) =
    match x with
    | :? int as number ->
        threadLocal.Value <- number
        if condition number then box name else box number
    | :? string as names ->
        box (if condition threadLocal.Value then names + name else names)
    | _ -> failwith "ロジックエラー"

let fizz = makeFizzBuzz "Fizz" (fun x -> x % 3 = 0)
let buzz = makeFizzBuzz "Buzz" (fun x -> x % 5 = 0)
let gizz = makeFizzBuzz "Gizz" (fun x -> x % 7 = 0)

> [1; 3; 5; 7; 15; 21; 35; 105] |> List.map (fun x -> gizz(buzz(fizz x)));;
val it : obj list =
  [1; "Fizz"; "Buzz"; "Gizz"; "FizzBuzz"; "FizzGizz"; "BuzzGizz"; "FizzBuzzGizz"]


次ページ
続きを表示
1を表示
最新レス表示
レスジャンプ
類似スレ一覧
スレッドの検索
話題のニュース
おまかせリスト
オプション
しおりを挟む
スレッドに書込
スレッドの一覧
暇つぶし2ch