Skip to content

Commit

Permalink
Merge pull request #470 from joelverhagen/minby-maxby
Browse files Browse the repository at this point in the history
Add Linq.MinBy and Linq.MaxBy support
  • Loading branch information
yanghuan authored Jan 4, 2024
2 parents c6ea5a3 + 78a42de commit 8177ef6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
40 changes: 40 additions & 0 deletions CSharp.lua/CoreSystem.Lua/CoreSystem/Collections/Linq.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1272,6 +1272,46 @@ function Enumerable.Max(source, ...)
return minOrMax(maxFn, source, ...)
end

local function minByOrMaxBy(compareFn, source, keySelector, comparer, TSource, TKey)
if source == nil then throw(ArgumentNullException("source")) end
if keySelector == nil then throw(ArgumentNullException("keySelector")) end
if comparer == nil then
comparer = Comparer_1(TKey).getDefault()
end
local compare = comparer.Compare
local key = TKey:default()
local item = TSource:default()
local hasItem = false
for _, x in each(source) do
local xKey = keySelector(x)
if hasItem then
if compareFn(compare, comparer, xKey, key) then
key = xKey
item = x
end
else
key = xKey
item = x
hasItem = true
end
end
if hasItem then
return item
elseif item == nil then
return nil
else
throw(InvalidOperationException("NoElements"))
end
end

function Enumerable.MinBy(source, keySelector, comparer, TSource, TKey)
return minByOrMaxBy(minFn, source, keySelector, comparer, TSource, TKey)
end

function Enumerable.MaxBy(source, keySelector, comparer, TSource, TKey)
return minByOrMaxBy(maxFn, source, keySelector, comparer, TSource, TKey)
end

function Enumerable.Average(source, ...)
if source == nil then throw(ArgumentNullException("source")) end
local sum, count = 0, 0
Expand Down
4 changes: 4 additions & 0 deletions CSharp.lua/System.xml
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,10 @@ limitations under the License.
<method name="Max" ArgCount="2" RetType="System.Double" Template="Linq.Max({0}, {1}, System.Double)" />
<method name="Max" ArgCount="2" RetType="System.Decimal" Template="Linq.Max({0}, {1}, System.Double)" />
<method name="Max" ArgCount="2" GenericArgCount="2" Template="Linq.Max({0}, {1}, {`1})" />
<method name="MinBy" ArgCount="2" GenericArgCount="2" Template="Linq.MinBy({0}, {1}, nil, {`0}, {`1})" />
<method name="MinBy" ArgCount="3" GenericArgCount="2" Template="Linq.MinBy({0}, {1}, {2}, {`0}, {`1})" />
<method name="MaxBy" ArgCount="2" GenericArgCount="2" Template="Linq.MaxBy({0}, {1}, nil, {`0}, {`1})" />
<method name="MaxBy" ArgCount="3" GenericArgCount="2" Template="Linq.MaxBy({0}, {1}, {2}, {`0}, {`1})" />
<method name="OfType" Template="Linq.OfType({0}, {`0})" />
<method name="OrderBy" ArgCount="2" Template="Linq.OrderBy({0}, {1}, nil, {`1})" />
<method name="OrderBy" ArgCount="3" Template="Linq.OrderBy({0}, {1}, {2}, {`1})" />
Expand Down

0 comments on commit 8177ef6

Please sign in to comment.