문제

이 작업을 수행하는 가장 좋은 방법을 알고 있습니까 :

let toTableau2D (seqinit:seq<'a*'b*'c>) =
   let myfst = fun (a,b,c) -> a
   let myscd = fun (a,b,c) -> b
   let mytrd = fun (a,b,c) -> c

   let inputd = seqinit |> groupBy2 myfst myscd
.

FST를 다시 쓰는 것보다 더 좋은 방법이 있어야합니다.

업데이트 패드 조언 후 이전 'A *'B를 단일 구조로 포장하는 것으로 다시 작성합니다. 내 코드는 이제 처럼 보입니다

let toTableau (seqinit:seq<'a*'b>) =
  let inputd = seqinit |> Seq.groupBy fst |> toMap
  let keys =  seqinit |> Seq.map fst |> Set.ofSeq |> List.ofSeq
  ...
.

도움이 되었습니까?

해결책

Why don't you just write it explicitly:

let toTableau2D (a, b, c) =
   let toto = a
   // ...

If you want to refer to seqinit later on, you always can reconstruct the triple or use the named pattern:

let toTableau2D ((a, b, c) as seqinit) =
   let toto = a
   // Do something with seqinit
   // ...

EDIT:

Unless you use reflection, you cannot have fst function for any kind of tuples. In your example, writing some utility functions and reusing them doesn't hurt:

let fst3 (a, _, _) = a
let snd3 (_, b, _) = b
let thd3 (_, _, c) = c

let toTableau2D (seqinit: seq<'a*'b*'c>) =
   let inputd = seqinit |> groupBy2 fst3 snd3
   // ...

If you want to make this work for arbitrary number of tuple elements, consider changing tuples to lists and employing pattern matching on lists.

다른 팁

+1 to what @pad said. Otherwise (if you just simplified what you're trying to do and are stuck with seqinit defined that way) I guess you can always do:

let toTableau2D (seqinit:'a*'b*'c) =
   let toto, _, _ = seqinit          
   //...
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top