Is List.partition guaranteed to preserve order?
开发者_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.
精彩评论