public TextLib() { RegisterMethod("split", 2, args => FObject.NewArray(new FArray( args[0].IsString().Split(args[1].IsString()) .Select(FObject.NewString).ToArray()))); RegisterMethod("replace", 3, GetMethod((x, y, z) => x.Replace(y, z))); RegisterMethod("join", 2, (x) => { var arr = x[0]; if (arr.Type != FObjectType.Array) { throw new Exception(); } var glue = x[1].IsString(); return(FObject.NewString(string.Join(glue, arr.Array().Get().Select(y => y.ToString())))); }); }
private static void LoadArrayFunctions() { functions["insert"] = new FBuiltin { // Arity count is not including caller object (array in this case) Arity = 2, CallObjectTypes = { FObjectType.Array }, Invokable = (fi, ins) => { ins[0].Array().Insert(ins[1].I64, ins[2]); return(ins[0]); }, ArgumentTypes = { FObjectType.Array, FObjectType.Int, FObjectType.Any } }; functions["count"] = new FBuiltin { Arity = 0, CallObjectTypes = { FObjectType.Array, FObjectType.String }, Invokable = (fi, ins) => FObject.NewI64(ins[0].Array().Length), }; functions["remove"] = new FBuiltin { Arity = 1, CallObjectTypes = { FObjectType.Array }, Invokable = (fi, ins) => { ins[0].Array().Remove(ins[1].I64); return(ins[0]); }, ArgumentTypes = { FObjectType.Int } }; functions["mutate"] = new FBuiltin { Arity = 1, CallObjectTypes = { FObjectType.Array }, Invokable = (fi, ins) => { var oArr = ins[0].Array(); var func = ins[1].Function(); var nArr = new FArray(oArr.Length); for (int i = 0; i < oArr.Length; i++) { nArr.Push(fi.Invoke(func, new[] { oArr.Get(i) })); } return(FObject.NewArray(nArr)); }, ArgumentTypes = { FObjectType.Function } }; functions["filter"] = new FBuiltin { Arity = 1, CallObjectTypes = { FObjectType.Array }, Invokable = (fi, ins) => { var oArr = ins[0].Array(); var func = ins[1].Function(); var nArr = new FArray(oArr.Length); for (int i = 0; i < oArr.Length; i++) { if (fi.Invoke(func, new[] { oArr.Get(i) }).True()) { nArr.Push(oArr.Get(i)); } } return(FObject.NewArray(nArr)); }, ArgumentTypes = { FObjectType.Function } }; functions["foreach"] = new FBuiltin { Arity = 1, CallObjectTypes = { FObjectType.Array }, Invokable = (fi, ins) => { var oArr = ins[0].Array(); var func = ins[1].Function(); for (int i = 0; i < oArr.Length; i++) { fi.Invoke(func, new[] { oArr.Get(i) }); } return(FObject.Nil); }, ArgumentTypes = { FObjectType.Function } }; }