Skip to content

Commit 6dbb0c1

Browse files
committed
New array shorthands.
1 parent 909df74 commit 6dbb0c1

1 file changed

Lines changed: 61 additions & 28 deletions

File tree

Deltinteger/Deltinteger/Parse/Types/ArrayType.cs

Lines changed: 61 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,47 +29,42 @@ public ArrayType(CodeType arrayOfType) : base((arrayOfType?.Name ?? "define") +
2929
_scope.AddNativeVariable(_first);
3030

3131
// Filtered Array
32-
Func(new FuncMethodBuilder() {
32+
new GenericSortFunction() {
3333
Name = "FilteredArray",
3434
Documentation = "A copy of the specified array with any values that do not match the specified condition removed.",
35-
DoesReturnValue = true,
3635
ReturnType = this,
37-
Parameters = new CodeParameter[] {
38-
new CodeParameter("conditionLambda", "The condition that is evaluated for each element of the copied array. If the condition is true, the element is kept in the copied array.", new MacroLambda(null, ArrayOfType))
39-
},
40-
Action = (actionSet, methodCall) => GenericSort<V_FilteredArray>(actionSet, methodCall)
41-
});
36+
ArrayOfType = ArrayOfType,
37+
ParameterDocumentation = "The condition that is evaluated for each element of the copied array. If the condition is true, the element is kept in the copied array."
38+
}.Add<V_FilteredArray>(_scope);
4239
// Sorted Array
43-
Func(new FuncMethodBuilder() {
40+
new GenericSortFunction() {
4441
Name = "SortedArray",
4542
Documentation = "A copy of the specified array with the values sorted according to the value rank that is evaluated for each element.",
46-
DoesReturnValue = true,
4743
ReturnType = this,
48-
Parameters = new CodeParameter[] {
49-
new CodeParameter("conditionLambda", "The value that is evaluated for each element of the copied array. The array is sorted by this rank in ascending order.", new MacroLambda(null, ArrayOfType))
50-
},
51-
Action = (actionSet, methodCall) => GenericSort<V_SortedArray>(actionSet, methodCall)
52-
});
44+
ArrayOfType = ArrayOfType,
45+
ParameterDocumentation = "The value that is evaluated for each element of the copied array. The array is sorted by this rank in ascending order."
46+
}.Add<V_SortedArray>(_scope);
5347
// Is True For Any
54-
Func(new FuncMethodBuilder() {
48+
new GenericSortFunction() {
5549
Name = "IsTrueForAny",
5650
Documentation = "Whether the specified condition evaluates to true for any value in the specified array.",
57-
DoesReturnValue = true,
58-
Parameters = new CodeParameter[] {
59-
new CodeParameter("conditionLambda", "The condition that is evaluated for each element of the specified array.", new MacroLambda(null, ArrayOfType))
60-
},
61-
Action = (actionSet, methodCall) => GenericSort<V_IsTrueForAny>(actionSet, methodCall)
62-
});
51+
ArrayOfType = ArrayOfType,
52+
ParameterDocumentation = "The condition that is evaluated for each element of the specified array."
53+
}.Add<V_IsTrueForAny>(_scope);
6354
// Is True For All
64-
Func(new FuncMethodBuilder() {
55+
new GenericSortFunction() {
6556
Name = "IsTrueForAll",
6657
Documentation = "Whether the specified condition evaluates to true for every value in the specified array.",
67-
DoesReturnValue = true,
68-
Parameters = new CodeParameter[] {
69-
new CodeParameter("conditionLambda", "The condition that is evaluated for each element of the specified array.", new MacroLambda(null, ArrayOfType))
70-
},
71-
Action = (actionSet, methodCall) => GenericSort<V_IsTrueForAll>(actionSet, methodCall)
72-
});
58+
ArrayOfType = ArrayOfType,
59+
ParameterDocumentation = "The condition that is evaluated for each element of the specified array."
60+
}.Add<V_IsTrueForAll>(_scope);
61+
// Mapped
62+
new GenericSortFunction() {
63+
Name = "Map",
64+
Documentation = "Whether the specified condition evaluates to true for every value in the specified array.",
65+
ArrayOfType = ArrayOfType,
66+
ParameterDocumentation = "The condition that is evaluated for each element of the specified array."
67+
}.Add<V_MappedArray>(_scope);
7368
// Contains
7469
Func(new FuncMethodBuilder() {
7570
Name = "Contains",
@@ -161,4 +156,42 @@ public override void AddObjectVariablesToAssigner(IWorkshopTree reference, VarIn
161156
public override Scope ReturningScope() => null;
162157
public override CompletionItem GetCompletion() => throw new NotImplementedException();
163158
}
159+
160+
class GenericSortFunction
161+
{
162+
public string Name;
163+
public string Documentation;
164+
public string ParameterDocumentation;
165+
public CodeType ReturnType;
166+
public CodeType ArrayOfType;
167+
168+
public void Add<T>(Scope addToScope) where T: Element, new()
169+
{
170+
// value => ...
171+
var noIndex = GetFuncMethod();
172+
noIndex.Parameters = new CodeParameter[] {
173+
new CodeParameter("conditionLambda", ParameterDocumentation, new MacroLambda(null, ArrayOfType))
174+
};
175+
noIndex.Action = (actionSet, methodCall) =>
176+
Element.Part<T>(actionSet.CurrentObject, ((LambdaAction)methodCall.ParameterValues[0]).Invoke(actionSet, new V_ArrayElement()));
177+
178+
// (value, index) => ...
179+
var withIndex = GetFuncMethod();
180+
withIndex.Parameters = new CodeParameter[] {
181+
new CodeParameter("conditionLambda", ParameterDocumentation, new MacroLambda(null, ArrayOfType, null))
182+
};
183+
withIndex.Action = (actionSet, methodCall) =>
184+
Element.Part<T>(actionSet.CurrentObject, ((LambdaAction)methodCall.ParameterValues[0]).Invoke(actionSet, new V_ArrayElement(), new V_CurrentArrayIndex()));
185+
186+
addToScope.AddNativeMethod(new FuncMethod(noIndex));
187+
addToScope.AddNativeMethod(new FuncMethod(withIndex));
188+
}
189+
190+
private FuncMethodBuilder GetFuncMethod() => new FuncMethodBuilder() {
191+
Name = Name,
192+
Documentation = Documentation,
193+
ReturnType = ReturnType,
194+
DoesReturnValue = true
195+
};
196+
}
164197
}

0 commit comments

Comments
 (0)