11

float リストを Vector3 または Vector2 リストに変換しようとする 2 つのコード スニペットがあります。アイデアは、リストから一度に 2/3 の要素を取得し、それらをベクトルとして結合することです。最終結果は一連のベクトルです。

    let rec vec3Seq floatList =
        seq {
            match floatList with
            | x::y::z::tail -> yield Vector3(x,y,z)
                               yield! vec3Seq tail
            | [] -> ()
            | _ -> failwith "float array not multiple of 3?"
            }

    let rec vec2Seq floatList =
        seq {
            match floatList with
            | x::y::tail -> yield Vector2(x,y)
                            yield! vec2Seq tail
            | [] -> ()
            | _ -> failwith "float array not multiple of 2?"
            }

コードは非常によく似ていますが、共通部分を抽出する方法がないようです。何か案は?

4

4 に答える 4

13

これが1つのアプローチです。これが実際にどれほど単純かはわかりませんが、繰り返されるロジックの一部を抽象化しています。

let rec mkSeq (|P|_|) x =
  seq {
    match x with
    | P(p,tail) -> 
        yield p
        yield! mkSeq (|P|_|) tail
    | [] -> ()
    | _ -> failwith "List length mismatch" }

let vec3Seq =
  mkSeq (function
  | x::y::z::tail -> Some(Vector3(x,y,z), tail)
  | _ -> None)
于 2010-02-14T02:53:22.373 に答える
2

これは kvb のソリューションと似ていますが、部分的なアクティブ パターンを使用していません。

let rec listToSeq convert (list:list<_>) =
    seq {
        if not(List.isEmpty list) then
            let list, vec = convert list
            yield vec
            yield! listToSeq convert list
        }

let vec2Seq = listToSeq (function
    | x::y::tail -> tail, Vector2(x,y)
    | _ -> failwith "float array not multiple of 2?")

let vec3Seq = listToSeq (function
    | x::y::z::tail -> tail, Vector3(x,y,z)
    | _ -> failwith "float array not multiple of 3?")
于 2010-02-14T05:31:31.247 に答える
2

Rex がコメントしたように、これを 2 つのケースだけにしたい場合は、コードをそのままにしておけばおそらく問題はありません。ただし、共通のパターンを抽出したい場合は、リストを指定された長さ (2 または 3 またはその他の数) のサブリストに分割する関数を作成できます。mapこれを行うと、指定された長さの各リストを に変換するためだけに使用しますVector

リストを分割する関数は F# ライブラリでは利用できないので (私が知る限り)、自分で実装する必要があります。おおよそ次のように実行できます。

let divideList n list = 
  // 'acc' - accumulates the resulting sub-lists (reversed order)
  // 'tmp' - stores values of the current sub-list (reversed order)
  // 'c'   - the length of 'tmp' so far
  // 'list' - the remaining elements to process
  let rec divideListAux acc tmp c list = 
    match list with
    | x::xs when c = n - 1 -> 
      // we're adding last element to 'tmp', 
      // so we reverse it and add it to accumulator
      divideListAux ((List.rev (x::tmp))::acc) [] 0 xs
    | x::xs ->
      // add one more value to 'tmp'
      divideListAux acc (x::tmp) (c+1) xs
    | [] when c = 0 ->  List.rev acc // no more elements and empty 'tmp'
    | _ -> failwithf "not multiple of %d" n // non-empty 'tmp'
  divideListAux [] [] 0 list      

これで、この関数を使用して、次のように 2 つの変換を実装できます。

seq { for [x; y] in floatList |> divideList 2 -> Vector2(x,y) }
seq { for [x; y; z] in floatList |> divideList 3 -> Vector3(x,y,z) }

返されるリストの長さがそれぞれ 2 または 3 になることを期待する不完全なパターンを使用しているため、これは警告を発しますが、それは正しい期待であるため、コードは正常に動作します。と同じことを行う簡単なバージョンのシーケンス式も使用していますが、このような単純な場合にのみ使用できます。->do yield

于 2010-02-14T02:51:11.810 に答える
0

正直なところ、これを使用してもう少しコンパクトにすることができるかもしれませんが、あなたが持っているものはできる限り良いものです:

// take 3 [1 .. 5] returns ([1; 2; 3], [4; 5])
let rec take count l =
    match count, l with
    | 0, xs -> [], xs
    | n, x::xs -> let res, xs' = take (count - 1) xs in x::res, xs'
    | n, [] -> failwith "Index out of range"

// split 3 [1 .. 6] returns [[1;2;3]; [4;5;6]]
let rec split count l =
    seq { match take count l with
          | xs, ys -> yield xs; if ys <> [] then yield! split count ys }

let vec3Seq l = split 3 l |> Seq.map (fun [x;y;z] -> Vector3(x, y, z))
let vec2Seq l = split 2 l |> Seq.map (fun [x;y] -> Vector2(x, y))

リストを分割するプロセスは、独自の一般的な "take" および "split" 関数に移動され、目的のタイプにマップするのがはるかに簡単になりました。

于 2010-02-14T03:05:24.467 に答える