Nice programing

옵션 유형 목록을 하나도없는 요소로 압축하는 가장 좋은 방법은 무엇입니까?

nicepro 2020. 11. 30. 19:53
반응형

옵션 유형 목록을 하나도없는 요소로 압축하는 가장 좋은 방법은 무엇입니까?


예기치 않게 '옵션 목록에서 Some 요소 만 포함하는 목록으로 이동하는 데 약간의 문제가 있습니다.

나의 초기 시도는 :

    let ga = List.filter (fun xx ->
        match xx with
        | Some(g) -> true
        | None -> false) gao 

그러나 물론이 결과 유형은 여전히 ​​'옵션 목록'입니다. List.map을 사용하여이를 압축하는 방법을 모르겠습니다. match 문에서 모든 경우를 처리해야하기 때문입니다. 추악한 해결책이 있지만 더 나은 것이 있는지 궁금합니다.

추한:

    let rec gOptRemove gdec gacc = 
        match gdec with 
        | head :: tail -> 
            match head with 
            | Some(a) -> gOptRemove tail (a :: gacc)
            | None -> gOptRemove tail gacc
        | [] -> gacc

비 재귀 솔루션을 찾거나 이런 종류의 표준 방법이 무엇인지 알아내는 것을 선호합니다.


간단히

List.choose id

에서와 같이

> [Some 4; None; Some 2; None] |> List.choose id;;
val it : int list = [4; 2]

List.choose

신분증

참고 URL : https://stackoverflow.com/questions/3548532/best-way-to-condense-a-list-of-option-type-down-to-only-elements-that-are-not-no

반응형