Is List.partition guaranteed to maintain order?

I noticed that this behaves this way, but I don't want to rely on him unless he is intentional. Here is 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.

+3
source share
1 answer

The specification does not say:

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

but the current implementation in FSharp.Core keeps order (it uses the mutation under the hood to create the resulting lists in order, since it follows the original, this is effective). I will ask you to see if we intend to advance this in the specification, as this seems like a useful guarantee.

+5
source

Source: https://habr.com/ru/post/1733684/


All Articles