Array functions - HiStructClient/femcad-doc GitHub Wiki
- Select
- Select Iterate
- SelectMany
- SelectManyFn
- Max, MaxBy and IndexOfMax
- Min, MinBy and IndexOfMin
- Order by one column or more columns
- Where
- Find
- Take and Skip
- Aggregate
- AggregateIterate
- Zip
- ColectBy
- Sum, SumItems and CumulativeSums
- All and Any
- Array properties
- Array operations
- Array merging
- Array multiplication
- Spacings
- Array item replacement
- Other
array.Select( item => exp. : any ) : array of any
Allows using also index of every iteration.
[1,1,2,1].SelectIterate(index,x => [index, x].ToString())
["[0, 1]", "[1, 1]", "[2, 2]", "[3, 1]"]
["a", "b", "c", "d"].SelectIterate(index,x => [index, x])
[[0,"a"],[1,"b"],[2,"c"],[3,"d"]]
Takes every sequence item and adds them together. All the items must be an array. Note: the same result is obtained by using function .Sum. However, SelectMany is recomended because it is much faster. SelectMany is based on extensioning memory slot size instead of creating new memory slot for every new item. This way there is only one memory usage instead of one for every expansion of array.
[[1,1],[1,1,2],[2,2],[1,1,2,1]].SelectMany()
[1,1,1,1,2,2,2,1,1,2,1]
[[1,1],[1,1,[2]],[2,2],[1,1,2,1]].SelectMany()
[1,1,1,1,[2],2,2,1,1,2,1]
[["a"], ["b"], ["c"], ["d", "e"],["z"]].SelectMany()
["a","b","c","d","e","z"]
Sumps up the items changed by lambda function. Every result of lambda expresion must be an array.
[[1,1],[1,1,2],[2,2],[1,1,2,1]].SelectManyFn(a=>a.Select(x => x + 1))
[2,2,2,2,3,3,3,2,2,3,2]
[["a"], ["b"], ["c"], ["d", "e"],["z"]].SelectManyFn(a=>a.Select( x => x + "1"))
["a1","b1","c1","d1","e1","z1"]
[ { a := [1] }, { a := [5] }, { a := [10] }].SelectManyFn(i => i.a*100 )
[100,500,1000]
array.Max( [ item => exp. : number or string ] ) : number or string
# exp 1
[ 1, 5 ].Max()
| 5
# exp 2
[ "Aa", "aa", "ab" ].Max()
| "aa"
# exp 3
[ { a = 1 }, { a = 5 } ].Max( a => a.a )
| 5
array.MaxBy( item => exp. : number or string ) : array item
# exp 1
[ { a = 1 }, { a = 5 } ].MaxBy( a => a.a )
| { a = 5 }
array.IndexOfMax( [ item => exp. : number or string ] ) : number
# exp 1
[ 1, 5 ].IndexOfMax()
| 1
# exp 2
[ "Aa", "aa", "ab" ].IndexOfMax()
| 1
# exp 3
[ { a = 1 }, { a = 5 } ].IndexOfMax( a => a.a )
| 1
array.Min( [ item => exp. : number or string ] ) : number or string
# exp 1
[ 1, 5 ].Min()
| 1
# exp 2
[ { a = 1 }, { a = 5 } ].Min( a => a.a )
| 1
array.MinBy( item => exp. : number or string ) : array item
# exp 1
[ { a = 1 }, { a = 5 } ].MinBy( a => a.a )
| { a = 1 }
array.IndexOfMin( [ item => exp. : number or string ] ) : number
# exp 1
[ 1, 5 ].IndexOfMax()
| 0
# exp 2
[ { a = 1 }, { a = 5 } ].IndexOfMax( a => a.a )
| 0
array.OrderByAscending( item => exp. : number or string ) : arrayarray.OrderByDescending( item => exp. : number or string ) : array
# exp 1
[ 10, 5 ].OrderByAscending( a => a )
| [5,10]
# exp 2
[ {a = 1, b = 50 }, { a = 5, b = 15 }, { a = 1, b = 20 } ].OrderByAscending( c => c.b )
| [{a = 5, b = 15},{a = 1, b = 20},{a = 1, b = 50}]
# exp 3
[ { FirstName = "Michel", Surname = "Gibbon" },
{ FirstName = "Kenton", Surname = "Hunnicutt" },
{ FirstName = "Michel", Surname = "Cripe" },
{ FirstName = "Damion", Surname = "Damiani" },
{ FirstName = "Daniel", Surname = "Baylis" }].OrderByAscending( user => user.FirstName )
| [
| {FirstName = "Damion", Surname = "Damiani"},
| {FirstName = "Daniel", Surname = "Baylis"},
| {FirstName = "Kenton", Surname = "Hunnicutt"},
| {FirstName = "Michel", Surname = "Gibbon"},
| {FirstName = "Michel", Surname = "Cripe"}
| ]
# exp 4
[ 10, 5 ].OrderByDescending(a=>a)
| [10,5]
# exp 5
[ {a = 1, b = 50 }, { a = 5, b = 15 }, { a = 1, b = 20 } ].OrderByDescending( c => c.b )
| [{a = 1, b = 50},{a = 1, b = 20},{a = 5, b = 15}]
# exp 6
[ { FirstName = "Michel", Surname = "Gibbon" },
{ FirstName = "Kenton", Surname = "Hunnicutt" },
{ FirstName = "Michel", Surname = "Cripe" },
{ FirstName = "Damion", Surname = "Damiani" },
{ FirstName = "Daniel", Surname = "Baylis" }].OrderByDescending( user => user.FirstName )
| [
| {FirstName = "Michel", Surname = "Gibbon"},
| {FirstName = "Michel", Surname = "Cripe"},
| {FirstName = "Kenton", Surname = "Hunnicutt"},
| {FirstName = "Daniel", Surname = "Baylis"},
| {FirstName = "Damion", Surname = "Damiani"}
| ]
array.OrderByAscendingMore( ( item => exp. : number or string )[] ) : arrayarray.OrderByDescendingMore( ( item => exp. : number or string )[] ) : array
# exp 1
[ {a = 1, b = 50 }, { a = 5, b = 15 }, { a = 1, b = 20 } ].OrderByAscendingMore( [ c => c.a, c => c.b ] )
| [{a = 1, b = 20},{a = 1, b = 50},{a = 5, b = 15}]
# exp 2
[ { FirstName = "Michel", Surname = "Gibbon" },
{ FirstName = "Kenton", Surname = "Hunnicutt" },
{ FirstName = "Michel", Surname = "Cripe" },
{ FirstName = "Damion", Surname = "Damiani" },
{ FirstName = "Daniel", Surname = "Baylis" }].OrderByAscendingMore( [ user => user.FirstName, user => user.Surname ] )
| [
| {FirstName = "Damion", Surname = "Damiani"},
| {FirstName = "Daniel", Surname = "Baylis"},
| {FirstName = "Kenton", Surname = "Hunnicutt"},
| {FirstName = "Michel", Surname = "Cripe"},
| {FirstName = "Michel", Surname = "Gibbon"}
| ]
# exp 3
[ {a = 1, b = 50 }, { a = 5, b = 15 }, { a = 1, b = 20 } ].OrderByDescendingMore( [ c => c.a, c => c.b ] )
| [{a = 5, b = 15},{a = 1, b = 50},{a = 1, b = 20}]
# exp 4
[ { FirstName = "Michel", Surname = "Gibbon" },
{ FirstName = "Kenton", Surname = "Hunnicutt" },
{ FirstName = "Michel", Surname = "Cripe" },
{ FirstName = "Damion", Surname = "Damiani" },
{ FirstName = "Daniel", Surname = "Baylis" }].OrderByDescendingMore( [ user => user.FirstName, user => user.Surname ] )
| [
| {FirstName = "Michel", Surname = "Gibbon"},
| {FirstName = "Michel", Surname = "Cripe"},
| {FirstName = "Kenton", Surname = "Hunnicutt"},
| {FirstName = "Daniel", Surname = "Baylis"},
| {FirstName = "Damion", Surname = "Damiani"}
| ]
array.Where( item => exp. : bool ) : array
# exp 1
[ 10, 5, 2, 5, 8, 6, 3, 5 ].Where( a => a > 5 )
| [10,8,6]
# exp 2
[ { a = 1, b = 50 },
{ a = 5, b = 15 },
{ a = 1, b = 20 },
{ a = 10, b = 15 } ].Where( c => c.b >= 20 )
| [{a = 1, b = 50},{a = 1, b = 20}]
# exp 3
[ { a = 1, b = 50 },
{ a = 5, b = 15 },
{ a = 1, b = 20 },
{ a = 10, b = 15 } ].Where( c => c.b == 15 )
| [{a = 5, b = 15},{a = 10, b = 15}]
array.Find( item => exp. : bool ) : array itemarray.FindOrDefault( ( item => exp. : bool, defaultValue ) : array item or defaultValue
Find()
function throw an error if the array does not contain the desired item. Instead, FindOrDefault()
function uses the defaultValue
.
# exp 1
[ { a = 1, b = 50 },
{ a = 5, b = 15 },
{ a = 1, b = 20 },
{ a = 10, b = 15 } ].Find( c => c.b == 15 )
| {a = 5, b = 15}
# exp 2
[ { a = 1, b = 50 },
{ a = 5, b = 15 },
{ a = 1, b = 20 },
{ a = 10, b = 15 } ].FindOrDefault( c => c.b == 15, { a = 0, b = 0 } )
| {a = 5, b = 15}
# exp 3
[ { a = 1, b = 50 },
{ a = 5, b = 15 },
{ a = 1, b = 20 },
{ a = 10, b = 15 } ].FindOrDefault( c => c.b == 12, { a = 0, b = 0 } )
| {a = 0, b = 0}
array.Take( numberOfTakenItems : number ) : arrayarray.Skip( numberOfSkippedItems : number ) : array
# exp 1 - Take
[ { a = 1, b = 50 },
{ a = 5, b = 15 },
{ a = 1, b = 20 },
{ a = 10, b = 15 } ].Take( 1 )
| [{a = 1, b = 50}]
# exp 2 - Skip
[ { a = 1, b = 50 },
{ a = 5, b = 15 },
{ a = 1, b = 20 },
{ a = 10, b = 15 } ].Skip( 1 )
| [
| {a = 5, b = 15},
| {a = 1, b = 20},
| {a = 10, b = 15}
| ]
array.Reverse() : array
# exp 1
[ { a = 1, b = 50 },
{ a = 5, b = 15 },
{ a = 1, b = 20 },
{ a = 10, b = 15 } ].Reverse()
| [
| {a = 10, b = 15},
| {a = 1, b = 20},
| {a = 5, b = 15},
| {a = 1, b = 50}
| ]
array.Aggregate( startValueForAggregate : any, aggregatedValue : any, ( item => exp. : any ) : any
# exp 1
[ { a = 1, b = 50 },
{ a = 5, b = 15 },
{ a = 1, b = 20 },
{ a = 10, b = 15 } ].Aggregate( 15, c, d => c + d.b )
| 115
# exp 2
indices := [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]
joinList := indices.Aggregate( [], a, b => a + b )
print joinList
| [1,2,3,4,5,6,7,8,9]
Adds index of iteration value and returns the last iteration value.
[10, 10, 10].AggregateIterate( 0,(index,accumulator,value => accumulator+value))
30
[1.0, 4.0, 4.1, 2.1].AggregateIterate(-3.0,(index,accumulator,value => accumulator-value+index))
1# -3.0 - 1.0 + 0 = -4.0
2# -4.0 - 4.0 + 1 = -7.0
3# -7.0 - 4.1 + 2 = -9.1
4# -9.1 - 2.1 + 3 = -8.2
-8.2
[100, 100, 100, 100].AggregateIterate( 0,(index,accumulator,value => (accumulator+value)*index))
0# - (000+100)*0 = 0
1# - (000+100)*1 = 100
2# - (100+100)*2 = 400
3# - (400+100)*3 = 1500
1500
array1.Zip( array2: array, ( array1Item, array2Item => exp. : any ) : array
# exp 1
[ { a = 1, b = 50 },
{ a = 5, b = 15 },
{ a = 1, b = 20 },
{ a = 10, b = 15 } ].Zip( [ 1, 2, 3, 4], ( c, d => c{ b = d } ) )
| [
| {a = 1, b = 1},
| {a = 5, b = 2},
| {a = 1, b = 3},
| {a = 10, b = 4}
| ]
array1.CollectBy( item => exp. : number or string ) : array of Buckets
Function CollectBy()
returns a array of Buckets
, see examples.
# exp 1
[ { a = 1, b = 50 }, { a = 5, b = 15 }, { a = 1, b = 20 } ].CollectBy( c => c.a ).Buckets.Select( bucket => bucket.Items[0] )
| [{a = 1, b = 50},{a = 5, b = 15}]
# exp 2
buckets := [ { a = "A", b = 50 }, { a = "B", b = 15 }, { a = "A", b = 20 } ].CollectBy( c => c.a ).Buckets
buckets.Select( bucket => bucket.Items[0] )
| [{a = "A", b = 50},{a = "B", b = 15}]
# exp 3
buckets.Select( bucket => { name := bucket.Items[0].a sum := bucket.Items.SumItems( i => i.b ) } )
| [
| {name = "A", sum = 70},
| {name = "B", sum = 15}
| ]
array.Sum : numberarray.SumItems( [ item => exp. : number ] ) : number
# exp 1
[ 10, 5 ].Sum
| 15
# exp 2
[ 10, 5 ].SumItems()
| 15
# exp 3
[ { a = 1, b = 50 }, { a = 5, b = 15 }, { a = 1, b = 20 } ].SumItems( c => c.b )
| 85
array.CumulativeSums : array
# exp 1
[ 10, 5, 20 ].CumulativeSums
| [10,15,35]
# exp 2
[ 10, 5, 25 ].GetCumulativeSums()
| [10,15,40]
array.All( item => exp. : bool ) : bool
# exp 1
[ 10, 5, 25 ].All(c => c > 15)
| False
# exp 2
[ 10, 5, 25 ].All(c => c > 0)
| True
array.Any( item => exp. : bool ) : bool
# exp 1
[ 10, 5, 25 ].Any(c => c > 15)
| True
# exp 3
[ 10, 5, 25 ].Any(c => c < 0)
| False
array.Count : number
# exp 1
[ 10, 5 ].Count
| 2
array.IsEmpty : bool
# exp 1
[].IsEmpty
| True
# exp 2
[10,5].IsEmpty
| False
( array : array of number * value : number or unit ) : array of numbers[].MultiplyElements( array : array of number, value : number or unit ) : array of numbers
# exp 1
[10,5,25] * 2.5
| [25,12.5,62.5]
# exp 2
[10,5,25] * Unit.mm
| [0.01,0.005,0.025]
# exp 3
[].MultiplyElements([10,5,25], 2.5)
| [25,12.5,62.5]
( array : array of number / value : unit ) : array of numbers
# exp 2
[10,5,25] / Unit.mm
| [10000,5000,25000]
( arrayFirst : array of any + arraySecond : array of any ) : array of any
# exp 1
[ 10, 50, 25 ] + [ 5, 2]
| [10,50,25,5,2]
arrayFirst.MergeDoubles( arraySecond : array of number, precision : number ) : array of numbers
Function merge same value from both array into one array. The values are compared according to the specified precision.
# exp 1
[0,2,2,3,4,4,5].MergeDoubles([1,3,4.00005,5.5], 0.005)
| [0,1,2,2,3,4,4,5,5.5]
( numberOfDuplications : number * array : array of any ) : array of any[].Mul( numberOfDuplications : number, array : array of any ) : array of any
# exp 1
5 * [ 10, 5, 25 ]
| [10,5,25,10,5,25,10,5,25,10,5,25,10,5,25]
# exp 2
[].Mul( 5, [ 10, 5, 25 ] )
| [10,5,25,10,5,25,10,5,25,10,5,25,10,5,25]
# exp 3
[ 1, 2, 3 ] + [ 10, 5, 25 ] * 2
| [1,2,3,20,10,50]
# exp 4
[ 1, 2, 3 ] + 2 * [ 10, 5, 25 ]
| [1,2,3,10,5,25,10,5,25]
array.GetSpacings() : array or number
# exp 1
[ 10, 5, 25 ].GetSpacings()
| [-5,20]
# exp 2
[ 10, 15, 25 ].GetSpacings()
| [5,10]
Note: GetSpacings() function returns double.
spanArray.MaskedSpanSums( maskArray array of 0 and 1 ) : array of number
The maskArray
must be one item longer then spanArray
. And the maskArray
must contain only digits 1 or 0.
# exp 1
[ 3, 3, 3, 3, 3, 3, 3, 3 ].MaskedSpanSums( [ 1, 1, 1, 0, 1, 0, 0, 1, 1 ] )
| [3,3,6,9,3]
Particular item of an array can be replaced by another one by the following sequence:
ReplaceOne := array, index, value => (Fcs.Converters.EnumerableRange(array.Count).Select( n => (n==index) ? value : array[n]))
c = ReplaceOne([1,2,3,4,5,6], 2, 3.3) gives [1,2,3.3,4,5,6]
If more items need to be replaced, the following transcription is suitable:
aa = [[2,3.1],[4,1.5]]
bb = [1,2,3,4,5,6]
cc = Fcs.Converters.EnumerableRange(bb.Count).Select( n => aa.FindOrDefault(p => p[0] == n, [0, bb[n]])[1] )
# exp 1
[10,5].Empty : array
| []
# exp 2
[{a=1},{a=5}].ToString() : string
| "[{a = 1}, {a = 5}]"