F #에서 사전에 대한 생성자가 있습니까 (Seq/List를 입력으로 사용하고 사전을 출력합니까)? 나는 내가 원하는 것을 다음 코드를 작성했습니다,하지만 난 어쩌면 이미 구현되어 단지 호기심 (그래서 내가 직접 구현할 필요가 없습니다) 내가 아는F # 사전 작성자 목록/Seq
let DictionaryBuilder (keyFunc:'a->'b) (valueFunc:'a->'c) aList =
let dict = new Dictionary<'b,'c>()
aList
|> Seq.iter (fun a -> dict.Add(keyFunc a, valueFunc a))
dict // return
C#으로, 당신이 할 수있는 그 .ToDictionary (System.Linq 사용)
// using System.Collections.Generic;
// using System.Linq;
List<string> example = new List<string> {"a","b","c"};
Dictionary<string,string> output = example.ToDictionary(x => x+"Key", x => x+"Value");
// Output: {"aKey": "aValue", "bKey": "bValue", "cKey": "cValue"}
고맙습니다.
'dict'은 읽기 전용 사전을 생성합니다 (항목을 추가하거나 제거하려고하면 예외가 발생합니다). 사전을 다시 Dictionary 생성자에 전달하여이 문제를 해결할 수 있습니다 :'... |> dict |> System.Collections.Generic.Dictionary' – TheQuickBrownFox
감사합니다. –