基本上我希望利用Seq.Windowed的輸出,返回數組的序列,並把它變成元組從數組的seq中創建元組。
的順序,所以我想利用這個
[[|1;2;3|];[|4;5;6|]]
,並把它變成
[(1,2,3);(4,5,6)]
在此先感謝。
基本上我希望利用Seq.Windowed的輸出,返回數組的序列,並把它變成元組從數組的seq中創建元組。
的順序,所以我想利用這個
[[|1;2;3|];[|4;5;6|]]
,並把它變成
[(1,2,3);(4,5,6)]
在此先感謝。
> let x = [[|1;2;3|];[|4;5;6|]];;
val x : int [] list = [[|1; 2; 3|]; [|4; 5; 6|]]
> let y = [for [|a; b; c|] in x do yield (a, b, c)];;
let y = [for [|a; b; c|] in x do yield (a, b, c)];;
----------------------------^
stdin(6,29): warning FS0025: Incomplete pattern matches on this expression.
For example, the value '[|_; _; _; _|]' may indicate a case not covered by
the pattern(s).
val y : (int * int * int) list = [(1, 2, 3); (4, 5, 6)]
如果您可以保證所有陣列具有相同的形狀,則可以忽略上述警告。如果警告真的困擾你,你可以寫:
> x |> List.map (function [|a;b;c|] -> a, b, c | _ -> failwith "Invalid array length");;
val it : (int * int * int) list = [(1, 2, 3); (4, 5, 6)]
感謝。這適用於我現在正在執行的操作 – AvatarOfChronos 2010-02-04 05:15:54
這不會起作用嗎?讓y2 [| a; b; c |] =(a,b,c) – 2010-11-12 23:34:14
我不確定它是否是一個typeo或不是你的數據與窗口不匹配。
let firstThreeToTuple (a : _[]) = (a.[0], a.[1], a.[2])
seq {1 .. 6}
|> Seq.windowed 3
|> Seq.map firstThreeToTuple
|> Seq.iter (printfn "%A")
(1, 2, 3)
(2, 3, 4)
(3, 4, 5)
(4, 5, 6)
如果你想有一個函數,它接受一個序列,並扒其成陣列序列您可以使用此代碼從另一個question。
let chunks n (sequence: seq<_>) =
let fold_fce (i, s) value =
if i < n then (i+1, Seq.append s (Seq.singleton value))
else ( 1, Seq.singleton value)
in sequence
|> Seq.scan (fold_fce) (0, Seq.empty)
|> Seq.filter (fun (i,_) -> i = n)
|> Seq.map (Seq.to_array << snd)
然後你可以通過firstThreeToTuple運行結果。
seq {1 .. 6}
|> chunks 3
|> Seq.map firstThreeToTuple
|> Seq.iter (printfn "%A")
(1, 2, 3)
(4, 5, 6)
作爲@gradbot指出,您的樣本輸出不符合Seq.Windowed(其中「窗口」,而不是「呼叫」) – Benjol 2010-02-04 07:21:08