LoginSignup
8
7

More than 5 years have passed since last update.

F#でofなんたらとtoなんたらの使い分け

Posted at

F#には、T seq から T list に変換する関数として、

  • Seq.toList
  • List.ofSeq

の2つの関数があります。
この2つの関数はまったく同じシグネチャを持つため、どちらかしか使っていない、という人もいると思います。

toなんたらと、ofなんたらは、次のように使い分けるといいでしょう。

  • パイプライン演算子でつなぐ場合は toなんたら
  • そうでない場合は ofなんたら
let xs = seq { 1..100 }
let lst =
  xs
  |> Seq.filter (fun x -> x % 2 = 0)
  |> Seq.map string
  |> (* ここまではseq *) Seq.toList (* ここからlist *)
     //                  seq -> list
let xs =
  seq { 1..100 }
  |> Seq.filter (fun x -> x % 2 = 0)
  |> Seq.map string
let lst (* この型はlist *) = List.ofSeq xs (* こっちの型はseq *)
        //                   list <- seq
8
7
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
8
7