Skip to content

fix(js/ts/python/dart/rust/beam): break min and max ties like FSharp.Core - #5198

Merged
MangelMaxime merged 5 commits into
fable-compiler:mainfrom
leonitousconforti:fix-min-tie-breaking
Nov 24, 2026
Merged

MangelMaxime merged 5 commits into
fable-compiler:mainfrom
leonitousconforti:fix-min-tie-breaking

Conversation

@leonitousconforti

@leonitousconforti leonitousconforti commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

The min/max collection functions and the max and min operators now pick the same element as FSharp.Core when values tie. One commit per target.

  • List/Array/Seq.minBy returned the last of tied minima and now return the first: [ "a", 1; "b", 0; "c", 0 ] |> List.minBy snd gave ("c", 0), now ("b", 0). minBy and maxBy also call the projection once per element, as FSharp.Core does.
  • List/Array/Seq.min returned the last of equal elements: List.min [ 1.0m; 1.00m ] gave 1.00, now 1.0.
  • The max operator returned its second argument on a tie (e.g. a type with a custom CompareTo). FSharp.Core returns the first. min already matched.
  • Python: the Rust-backed Array.max/min kept the last of equal elements too.
  • Rust: minBy/min and maxBy/max all kept the last of tied elements. The float max/min operators used f64::max/min, which pick either zero and drop NaN: max 0.0 -0.0 gave -0.0 and max nan 1.0 gave 1.0. They now give 0.0 and nan, like Math.Max/Math.Min.
  • Dart: comparison used double.compareTo, which orders -0.0 below 0.0, so the two were never a tie: List.max [ -0.0; 0.0 ] gave 0.0, now -0.0. compare 0.0 -0.0 is now 0, also inside tuples, records and unions (compare { X = 0.0 } { X = -0.0 } gave 1), as Set/Map keys, and through a boxed IComparable.CompareTo.
  • Beam: minBy/maxBy broke key ties by Erlang term order of the elements. The float max/min operators returned their first argument for 0.0 and -0.0 (max -0.0 0.0 gave -0.0, now 0.0). min on tuples, records and unions returned its first argument on a tie, now the second.

Notes:

  • Python is fixed in place, like fix(js/ts/python/dart/beam): add FSharp.Core's argument checks to collection functions #5154. I can switch to new names as AGENTS.md asks if you prefer.
  • NaN handling is unchanged on JS, Python, Dart and Beam, apart from which of several NaN keys minBy returns. On Rust some NaN results changed to match .NET: the float operators above, max on tuples with a NaN, List.max with a NaN, and max in generic code. The remaining NaN differences come from compare using a total order, which seems like a separate issue.

leonitousconforti added a commit to leonitousconforti/flip7 that referenced this pull request Nov 6, 2026
Only the min tie, fable-compiler/Fable#5198, is still listed. ParamName and
Convert.ToHexString come from Fable now, and Assert.Throws no longer singles
out a plain Exception.
leonitousconforti added a commit to leonitousconforti/flip7 that referenced this pull request Nov 6, 2026
Only the min tie, fable-compiler/Fable#5198, is still listed. ParamName and
Convert.ToHexString come from Fable now, and Assert.Throws no longer singles
out a plain Exception.
leonitousconforti added a commit to leonitousconforti/flip7 that referenced this pull request Nov 6, 2026
Fable's List.minBy keeps the last of a tie until fable-compiler/Fable#5198
ships, so until then the fable job fails on this game.
@leonitousconforti

Copy link
Copy Markdown
Contributor Author

👋

- List, Array and Seq minBy returned the last of the tied minima; they
  now return the first, and minBy and maxBy call the projection once per
  element instead of twice per step.
- List, Array and Seq min returned the last of equal minima, visible
  with 1.0m and 1.00m, -0.0 and 0.0 or a custom comparison. They now
  return the first, like max already did.
- The max operator returned its second argument when the values compare
  equal; it now returns the first like FSharp.Core (min already returned
  the second, as FSharp.Core does).
- List and Seq minBy returned the last of the tied minima; they now
  return the first. List, Array and Seq minBy and maxBy call the
  projection once per element instead of twice per step.
- List and Seq min, and Array min and max, returned the last of equal
  elements (e.g. -0.0 and 0.0); they now return the first.
- The max operator returned its second argument when the values compare
  equal; it now returns the first like FSharp.Core.
- List, Array and Seq minBy returned the last of the tied minima; they
  now return the first, and minBy and maxBy call the projection once per
  element instead of twice per step.
- List, Array and Seq min returned the last of equal elements (e.g. with
  a custom comparison); they now return the first, like max already did.
- The max operator returned its second argument when the values compare
  equal; it now returns the first like FSharp.Core.
- Float comparison used double.compareTo, which orders -0.0 before 0.0.
  It now goes through compareDouble, which treats them as equal, in
  compare, generated record and union comparison, tuples and Set/Map
  keys.
- IComparable.CompareTo goes through compareDynamic, so boxed floats
  compare 0.0 and -0.0 as equal too.
- List, Array and Seq minBy, maxBy, min and max returned the last of
  tied elements; they now return the first, comparing each element with
  the current one like FSharp.Core. minBy and maxBy call the projection
  once per element instead of twice per step.
- The max operator returned its second argument when the values compare
  equal; it now returns the first like FSharp.Core.
- The float max and min operators used f64::max and f64::min, which
  pick either zero and drop NaN. They now go through maxFloat and
  minFloat, where max prefers 0.0 over -0.0, min prefers -0.0, and NaN
  propagates, like Math.Max and Math.Min.
- List and Array minBy and maxBy broke key ties by comparing the
  elements themselves in Erlang term order, so they returned the
  smallest or largest of the tied elements. They now return the first,
  as they already did for union keys.
- Seq minBy returned the last of the tied minima, and Seq min the last
  of equal elements; they now return the first. Seq minBy and maxBy call
  the projection once per element instead of twice per step.
- The float max and min operators returned their first argument for 0.0
  and -0.0. They now go through max_float and min_float, where max
  returns 0.0 and min returns -0.0, like Math.Max and Math.Min.
- The min operator on tuples, records and other structural values
  returned its first argument on a tie. It now goes through
  min_structural, which returns the second like FSharp.Core. min on
  unions did the same and now returns the second too.
@MangelMaxime
MangelMaxime merged commit bbba1dd into fable-compiler:main Nov 24, 2026
44 checks passed
@MangelMaxime

Copy link
Copy Markdown
Member

Thank you for the PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants