Add a Utils.Where function to lua for filtering collections

This commit is contained in:
abcdefg30
2016-04-08 11:00:19 +02:00
parent 1c6253c3fb
commit b02a9a188d

View File

@@ -9,6 +9,7 @@
*/
#endregion
using System.Collections;
using System.Linq;
using Eluant;
using OpenRA.Scripting;
@@ -54,6 +55,20 @@ namespace OpenRA.Mods.Common.Scripting
return true;
}
[Desc("Returns the original collection filtered with the func.")]
public LuaTable Where(LuaValue[] collection, LuaFunction func)
{
var t = Context.CreateTable();
foreach (var c in collection)
using (var ret = func.Call(c))
using (var result = ret.FirstOrDefault())
if (result != null && result.ToBoolean())
t.Add(t.Count + 1, c);
return t;
}
[Desc("Returns the first n values from a collection.")]
public LuaValue[] Take(int n, LuaValue[] source)
{