仅查询Dict中的前3个

时间:2018-09-19 04:29:42

标签: elm

我想不出一种简单的方法来仅过滤此Dict中的前3个项目

 fruit =
    fromList
        [ ( ( 0, 0 ), "apple" )
        , ( ( 0, 1 ), "orange" )
        , ( ( 0, 2 ), " " )
        , ( ( 1, 0 ), " "  )
        , ( ( 1, 1 ), " "  )
        ]

我看过docs for Dict,但看不到任何将前三个查询到另一个Dict的简便方法,因此我以后可以只对前三个查询做些事情

1 个答案:

答案 0 :(得分:6)

The problem is that there is not supposed to be a notion of 'first' in a Dict. In Elm, keys are ordered alphabetically, but some other languages (Go) make dictionary order random so that you are not tempted to rely upon it.

So you should probably think about why you want to do this.

That said, if you want to go further, the best I can suggest is

take3Dict dict = 
    dict |> Dict.toList |> List.take 3 |> Dict.fromList