Array functions - HiStructClient/femcad-doc GitHub Wiki

List of functions

  1. Select
  2. Select Iterate
  3. SelectMany
  4. SelectManyFn
  5. Max, MaxBy and IndexOfMax
  6. Min, MinBy and IndexOfMin
  7. Order by one column or more columns
  8. Where
  9. Find
  10. Take and Skip
  11. Aggregate
  12. AggregateIterate
  13. Zip
  14. ColectBy
  15. Sum, SumItems and CumulativeSums
  16. All and Any
  17. Array properties
  18. Array operations
  19. Array merging
  20. Array multiplication
  21. Spacings
  22. Array item replacement
  23. Other

Map expression to array items

array.Select( item => exp. : any ) : array of any

Select iterate

Allows using also index of every iteration.

Example:

 [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"]]

Select many

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.

Example:

 [[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"]

SelectManyFn

Sumps up the items changed by lambda function. Every result of lambda expresion must be an array.

Example:

 [[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]

Maximal value

Get maximal value

array.Max( [ item => exp. : number or string ] ) : number or string

Examples

# exp 1
[ 1, 5 ].Max()
| 5

# exp 2
[ "Aa", "aa", "ab" ].Max()
| "aa"

# exp 3
[ { a = 1 }, { a = 5 } ].Max( a => a.a )
| 5

Get array item with maximal value

array.MaxBy( item => exp. : number or string ) : array item

Examples

# exp 1
[ { a = 1 }, { a = 5 } ].MaxBy( a => a.a )
| { a = 5 }

Get index of maximal value

array.IndexOfMax( [ item => exp. : number or string ] ) : number

Examples

# exp 1
[ 1, 5 ].IndexOfMax()
| 1

# exp 2
[ "Aa", "aa", "ab" ].IndexOfMax()
| 1

# exp 3
[ { a = 1 }, { a = 5 } ].IndexOfMax( a => a.a )
| 1

Minimal value

Get minimal value

array.Min( [ item => exp. : number or string ] ) : number or string

Examples

# exp 1
[ 1, 5 ].Min()
| 1

# exp 2
[ { a = 1 }, { a = 5 } ].Min( a => a.a )
| 1

Get array item with minimal value

array.MinBy( item => exp. : number or string ) : array item

Examples

# exp 1
[ { a = 1 }, { a = 5 } ].MinBy( a => a.a )
| { a = 1 }

Get index of minimal value

array.IndexOfMin( [ item => exp. : number or string ] ) : number

Examples

# exp 1
[ 1, 5 ].IndexOfMax()
| 0

# exp 2
[ { a = 1 }, { a = 5 } ].IndexOfMax( a => a.a )
| 0

Order array

By one "column"

array.OrderByAscending( item => exp. : number or string ) : array
array.OrderByDescending( item => exp. : number or string ) : array

Example

# 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"}
| ]

By more columns

array.OrderByAscendingMore( ( item => exp. : number or string )[] ) : array
array.OrderByDescendingMore( ( item => exp. : number or string )[] ) : array

Example

# 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"}
| ]

Filter desired values out

array.Where( item => exp. : bool ) : array

Example

# 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}]

Find first occurrence of desired value

array.Find( item => exp. : bool ) : array item
array.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.

Examples

# 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}

Take and Skip

array.Take( numberOfTakenItems : number ) : array
array.Skip( numberOfSkippedItems : number ) : array

Examples

# 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}
| ]

Reverse order

array.Reverse() : array

Examples

# 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}
| ]

Aggregate value

array.Aggregate( startValueForAggregate : any, aggregatedValue : any, ( item => exp. : any ) : any

Examples

# 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] 

Aggregate Iterate

Adds index of iteration value and returns the last iteration value.

Example:

 [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

Combine arrays (Zip)

array1.Zip( array2: array, ( array1Item, array2Item => exp. : any ) : array

Example

# 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}
| ]

Group by same values (Collect, Buckets)

array1.CollectBy( item => exp. : number or string ) : array of Buckets

Function CollectBy() returns a array of Buckets, see examples.

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}
| ]

Summations

Summation

array.Sum : number
array.SumItems( [ item => exp. : number ] ) : number

Examples

# 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

Cumulative summation

array.CumulativeSums : array

Example

# exp 1
[ 10, 5, 20 ].CumulativeSums
| [10,15,35]

# exp 2
[ 10, 5, 25 ].GetCumulativeSums()
| [10,15,40]

Check array items

All of items meets the condition

array.All( item => exp. : bool ) : bool

Examples

# exp 1
[ 10, 5, 25 ].All(c => c > 15)
| False

# exp 2
[ 10, 5, 25 ].All(c => c > 0)
| True

Any of items meets the condition

array.Any( item => exp. : bool ) : bool

Examples

# exp 1
[ 10, 5, 25 ].Any(c => c > 15)
| True

# exp 3
[ 10, 5, 25 ].Any(c => c < 0)
| False

Array properties

Count of items

array.Count : number

Example

# exp 1
[ 10, 5 ].Count
| 2

Empty array

array.IsEmpty  : bool

Examples

# exp 1
[].IsEmpty
| True

# exp 2
[10,5].IsEmpty
| False

Array operations

Multiply array by number or unit

( array : array of number * value : number or unit ) : array of numbers
[].MultiplyElements( array : array of number, value : number or unit ) : array of numbers

Examples

# 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 divide by unit

( array : array of number / value : unit ) : array of numbers

Examples

# exp 2
[10,5,25] / Unit.mm
| [10000,5000,25000]

Array merging

Basic array merging

( arrayFirst : array of any + arraySecond : array of any ) : array of any

Example

# exp 1
[ 10, 50, 25 ] + [ 5, 2]
| [10,50,25,5,2]

Merge without same values

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]

Array multiplication

( numberOfDuplications : number * array : array of any ) : array of any
[].Mul( numberOfDuplications : number, array : array of any ) : array of any

Examples

# 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]

Spacings

Basic spacings

array.GetSpacings() : array or number

Examples

# exp 1
[ 10, 5, 25 ].GetSpacings()
| [-5,20]

# exp 2
[ 10, 15, 25 ].GetSpacings()
| [5,10]

Note: GetSpacings() function returns double.

Masked spacings summations

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.

Examples

# 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]

Replacement

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]))

Example

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] )

Other

# exp 1
[10,5].Empty  : array
| []

# exp 2
[{a=1},{a=5}].ToString() : string
| "[{a = 1}, {a = 5}]"
⚠️ **GitHub.com Fallback** ⚠️