开发者

Is List.partition guaranteed to preserve order?

开发者 https://www.devze.com 2022-12-20 15:28 出处:网络
开发者_Python百科I\'ve noticed it seems to behave this way, but I don\'t want to rely on it if it\'s not intentional. Here\'s the code in question:

开发者_Python百科I've noticed it seems to behave this way, but I don't want to rely on it if it's not intentional. Here's the code in question:

let bestValuesUnder max =
    allValues
    >> List.partition (fun value -> value < max)
    >> function
        | ([], bad) -> [List.min bad]
        | (good, _) -> good // |> List.sortBy (fun value -> -value)

allValues is a function that returns an int list.


The spec does not say:

http://msdn.microsoft.com/en-us/library/ee353782(VS.100).aspx

but the current implementation in FSharp.Core does preserve order (it uses mutation under the hood to create the resulting lists in order, as it walks the original; this is efficient). I'll ask to see if we intend to promote this to the spec, as it seems like a useful guarantee.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号