Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Fable.Transforms/Rust/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,7 @@ let operators (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr o
| "Clamp", _ -> math r t args i.SignatureArgTypes i.CompiledName |> Some
| ("Min" | "Max") as meth, _ ->
match args.Head.Type with
| Number(Decimal, _) -> libCall com r t "Decimal" (Naming.lowerFirst meth) args |> Some
| Boolean
| Char
| String
Expand Down
60 changes: 39 additions & 21 deletions src/fable-library-beam/Seq.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1166,15 +1166,24 @@ let sum (xs: seq<'T>) ([<Inject>] adder: IGenericAdder<'T>) : 'T =
let sumBy (f: 'T -> 'U) (xs: seq<'T>) ([<Inject>] adder: IGenericAdder<'U>) : 'U =
fold (fun acc x -> adder.Add(acc, f x)) (adder.GetZero()) xs

let maxBy (projection: 'T -> 'U) xs ([<Inject>] comparer: System.Collections.Generic.IComparer<'U>) : 'T =
reduce
(fun x y ->
if comparer.Compare(projection y, projection x) > 0 then
y
else
x
)
xs
let maxBy (projection: 'T -> 'U) (xs: seq<'T>) ([<Inject>] comparer: System.Collections.Generic.IComparer<'U>) : 'T =
use e = ofSeq xs

if not (e.MoveNext()) then
invalidArg "source" SR.inputSequenceEmpty

let mutable acc = e.Current
let mutable accv = projection acc

while e.MoveNext() do
let x = e.Current
let v = projection x

if comparer.Compare(v, accv) > 0 then
acc <- x
accv <- v

acc

let max xs ([<Inject>] comparer: System.Collections.Generic.IComparer<'T>) : 'T =
reduce
Expand All @@ -1186,23 +1195,32 @@ let max xs ([<Inject>] comparer: System.Collections.Generic.IComparer<'T>) : 'T
)
xs

let minBy (projection: 'T -> 'U) xs ([<Inject>] comparer: System.Collections.Generic.IComparer<'U>) : 'T =
reduce
(fun x y ->
if comparer.Compare(projection y, projection x) > 0 then
x
else
y
)
xs
let minBy (projection: 'T -> 'U) (xs: seq<'T>) ([<Inject>] comparer: System.Collections.Generic.IComparer<'U>) : 'T =
use e = ofSeq xs

if not (e.MoveNext()) then
invalidArg "source" SR.inputSequenceEmpty

let mutable acc = e.Current
let mutable accv = projection acc

while e.MoveNext() do
let x = e.Current
let v = projection x

if comparer.Compare(v, accv) < 0 then
acc <- x
accv <- v

acc

let min (xs: seq<'T>) ([<Inject>] comparer: System.Collections.Generic.IComparer<'T>) : 'T =
reduce
(fun x y ->
if comparer.Compare(y, x) > 0 then
x
else
if comparer.Compare(y, x) < 0 then
y
else
x
)
xs

Expand Down
30 changes: 26 additions & 4 deletions src/fable-library-beam/fable_list.erl
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,19 @@ collect(Fn, List) ->
sum_by(Fn, List) ->
lists:sum(lists:map(Fn, List)).

min_by(Fn, List) ->
element(2, lists:min(lists:map(fun(E) -> {Fn(E), E} end, List))).
min_by(Fn, [Head | Tail]) ->
{_, Result} = lists:foldl(
fun(Item, {BestKey, Best}) ->
ItemKey = Fn(Item),
case ItemKey < BestKey of
true -> {ItemKey, Item};
false -> {BestKey, Best}
end
end,
{Fn(Head), Head},
Tail
),
Result.

min_by(Fn, Comparer, [Head | Tail]) ->
{_, Result} = lists:foldl(
Expand All @@ -303,8 +314,19 @@ min_by(Fn, Comparer, [Head | Tail]) ->
),
Result.

max_by(Fn, List) ->
element(2, lists:max(lists:map(fun(E) -> {Fn(E), E} end, List))).
max_by(Fn, [Head | Tail]) ->
{_, Result} = lists:foldl(
fun(Item, {BestKey, Best}) ->
ItemKey = Fn(Item),
case ItemKey > BestKey of
true -> {ItemKey, Item};
false -> {BestKey, Best}
end
end,
{Fn(Head), Head},
Tail
),
Result.

max_by(Fn, Comparer, [Head | Tail]) ->
{_, Result} = lists:foldl(
Expand Down
52 changes: 33 additions & 19 deletions src/fable-library-dart/Array.fs
Original file line number Diff line number Diff line change
Expand Up @@ -958,14 +958,21 @@ let sumBy (projection: 'T -> 'T2) (array: 'T[]) ([<Inject>] adder: IGenericAdder
acc

let maxBy (projection: 'a -> 'b) (xs: 'a[]) ([<Inject>] comparer: IComparer<'b>) : 'a =
reduce
(fun x y ->
if comparer.Compare(projection y, projection x) > 0 then
y
else
x
)
xs
if Array.isEmpty xs then
invalidArg "array" LanguagePrimitives.ErrorStrings.InputArrayEmptyString

let mutable acc = xs[0]
let mutable accv = projection acc

for i = 1 to xs.Length - 1 do
let x = xs[i]
let v = projection x

if comparer.Compare(v, accv) > 0 then
acc <- x
accv <- v

acc

let max (xs: 'a[]) ([<Inject>] comparer: IComparer<'a>) : 'a =
reduce
Expand All @@ -978,22 +985,29 @@ let max (xs: 'a[]) ([<Inject>] comparer: IComparer<'a>) : 'a =
xs

let minBy (projection: 'a -> 'b) (xs: 'a[]) ([<Inject>] comparer: IComparer<'b>) : 'a =
reduce
(fun x y ->
if comparer.Compare(projection y, projection x) > 0 then
x
else
y
)
xs
if Array.isEmpty xs then
invalidArg "array" LanguagePrimitives.ErrorStrings.InputArrayEmptyString

let mutable acc = xs[0]
let mutable accv = projection acc

for i = 1 to xs.Length - 1 do
let x = xs[i]
let v = projection x

if comparer.Compare(v, accv) < 0 then
acc <- x
accv <- v

acc

let min (xs: 'a[]) ([<Inject>] comparer: IComparer<'a>) : 'a =
reduce
(fun x y ->
if comparer.Compare(y, x) > 0 then
x
else
if comparer.Compare(y, x) < 0 then
y
else
x
)
xs

Expand Down
56 changes: 35 additions & 21 deletions src/fable-library-dart/List.fs
Original file line number Diff line number Diff line change
Expand Up @@ -640,15 +640,22 @@ let sum (xs: 'T list) ([<Inject>] adder: IGenericAdder<'T>) : 'T =
let sumBy (f: 'T -> 'U) (xs: 'T list) ([<Inject>] adder: IGenericAdder<'U>) : 'U =
fold (fun acc x -> adder.Add(acc, f x)) (adder.GetZero()) xs

let maxBy (projection: 'T -> 'U) xs ([<Inject>] comparer: IComparer<'U>) : 'T =
reduce
(fun x y ->
if comparer.Compare(projection y, projection x) > 0 then
y
else
x
)
xs
let maxBy (projection: 'T -> 'U) (xs: 'T list) ([<Inject>] comparer: IComparer<'U>) : 'T =
let mutable acc = head xs
let mutable accv = projection acc
let mutable xs = tail xs

while not xs.IsEmpty do
let x = xs.Head
let v = projection x

if comparer.Compare(v, accv) > 0 then
acc <- x
accv <- v

xs <- xs.Tail

acc

let max xs ([<Inject>] comparer: IComparer<'T>) : 'T =
reduce
Expand All @@ -660,23 +667,30 @@ let max xs ([<Inject>] comparer: IComparer<'T>) : 'T =
)
xs

let minBy (projection: 'T -> 'U) xs ([<Inject>] comparer: IComparer<'U>) : 'T =
reduce
(fun x y ->
if comparer.Compare(projection y, projection x) > 0 then
x
else
y
)
xs
let minBy (projection: 'T -> 'U) (xs: 'T list) ([<Inject>] comparer: IComparer<'U>) : 'T =
let mutable acc = head xs
let mutable accv = projection acc
let mutable xs = tail xs

while not xs.IsEmpty do
let x = xs.Head
let v = projection x

if comparer.Compare(v, accv) < 0 then
acc <- x
accv <- v

xs <- xs.Tail

acc

let min (xs: 'T list) ([<Inject>] comparer: IComparer<'T>) : 'T =
reduce
(fun x y ->
if comparer.Compare(y, x) > 0 then
x
else
if comparer.Compare(y, x) < 0 then
y
else
x
)
xs

Expand Down
60 changes: 39 additions & 21 deletions src/fable-library-dart/Seq.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1114,15 +1114,24 @@ let sum (xs: seq<'T>) ([<Inject>] adder: IGenericAdder<'T>) : 'T =
let sumBy (f: 'T -> 'U) (xs: seq<'T>) ([<Inject>] adder: IGenericAdder<'U>) : 'U =
fold (fun acc x -> adder.Add(acc, f x)) (adder.GetZero()) xs

let maxBy (projection: 'T -> 'U) xs ([<Inject>] comparer: IComparer<'U>) : 'T =
reduce
(fun x y ->
if comparer.Compare(projection y, projection x) > 0 then
y
else
x
)
xs
let maxBy (projection: 'T -> 'U) (xs: seq<'T>) ([<Inject>] comparer: IComparer<'U>) : 'T =
use e = ofSeq xs

if not (e.MoveNext()) then
invalidArg "source" SR.inputSequenceEmpty

let mutable acc = e.Current
let mutable accv = projection acc

while e.MoveNext() do
let x = e.Current
let v = projection x

if comparer.Compare(v, accv) > 0 then
acc <- x
accv <- v

acc

let max xs ([<Inject>] comparer: IComparer<'T>) : 'T =
reduce
Expand All @@ -1134,23 +1143,32 @@ let max xs ([<Inject>] comparer: IComparer<'T>) : 'T =
)
xs

let minBy (projection: 'T -> 'U) xs ([<Inject>] comparer: IComparer<'U>) : 'T =
reduce
(fun x y ->
if comparer.Compare(projection y, projection x) > 0 then
x
else
y
)
xs
let minBy (projection: 'T -> 'U) (xs: seq<'T>) ([<Inject>] comparer: IComparer<'U>) : 'T =
use e = ofSeq xs

if not (e.MoveNext()) then
invalidArg "source" SR.inputSequenceEmpty

let mutable acc = e.Current
let mutable accv = projection acc

while e.MoveNext() do
let x = e.Current
let v = projection x

if comparer.Compare(v, accv) < 0 then
acc <- x
accv <- v

acc

let min (xs: seq<'T>) ([<Inject>] comparer: IComparer<'T>) : 'T =
reduce
(fun x y ->
if comparer.Compare(y, x) > 0 then
x
else
if comparer.Compare(y, x) < 0 then
y
else
x
)
xs

Expand Down
2 changes: 1 addition & 1 deletion src/fable-library-dart/Util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,7 @@ T min<T>(int Function(T, T) comparer, T x, T y) {
}

T max<T>(int Function(T, T) comparer, T x, T y) {
return comparer(x, y) > 0 ? x : y;
return comparer(x, y) < 0 ? y : x;
}

T clamp<T>(int Function(T, T) comparer, T value, T min, T max) {
Expand Down
Loading
Loading