Class: Array
- Inherits:
-
Object
- Object
- Array
- Includes:
- XRVG::Samplable, XRVG::Splittable
- Defined in:
- lib/utils.rb
Overview
Array extension to synchronize enumerations, and also provide other recurrent services See this for presentation
Instance Method Summary collapse
- #addfilter(newfilter) ⇒ Object
-
#choice ⇒ Object
return a random item from array.
-
#compute(inputs, type, &block) ⇒ Object
FloatFunctor overloading to synchronize content sampling and splitting.
-
#flattenonce ⇒ Object
flatten an array of arrays [[1,1], [2,2]].flattenonce => [1,1,2,2].
-
#foreach(arity = nil, &block) ⇒ Object
same as Enumerator.each_slice with implicit size given by block.arity, or explicit if no blocks (in that case, return array of array) same enumeration model as for Tcl foreach command (see Array.zip method for further compatibility) [1,2,3,4].foreach {|v1,v2| puts “#{v1} #v2”} => “1 2” “3 4”.
-
#forpattern(pattern, &block) ⇒ Object
in same idea as forzip, but with explicit array index if pattern is nil, is equivalent to [0,1,…, self.size-1] [ [1,2,3,4], [a,b] ].forpattern( [0,0,1] ) => [1,2,a,3,4,b] [ [1,2,3,4], [a,b] ].forpattern( [0,1,0] ) => [1,a,2,3,b,4].
-
#forzip(aarity = nil, &block) ⇒ Object
aarity = array of arity if nil, default value is Array.new( self.size, 1 ) size of aarity must be inferior or equal to self.size.
-
#half(&block) ⇒ Object
alias for sub(2) [1,2,3,4].half => [1,3].
- #mean ⇒ Object
-
#pairs(&block) ⇒ Object
alias for uplets(2, &block) [1,2,3,4].pairs {|v| puts “#{v} #1”} => “1 2” “2 3” “3 4”.
-
#range(proc = nil) ⇒ Object
compute range of an array by returning (min..max) [1.0, 3.0, 2.0].range => (1.0..3.0) if proc supplied, use it to return range of subvalues [V2D::O, V2D::X].range( :x ) => (0.0..1.0).
-
#shuffle ⇒ Object
shuffle values in array.
-
#sub(period, &block) ⇒ Object
take only the nieme elements.
- #sum ⇒ Object
-
#triplets(&block) ⇒ Object
alias for uplets(3, &block) [1,2,3,4].pairs {|v| puts “#{v} #1 #2”} => “1 2 3” “2 3 4”.
-
#uplets(arity = nil, &block) ⇒ Object
same as Enumerator.each_cons with implicit size given by block.arity, or explicit if no blocks (in that case, return array of array) [1,2,3,4].uplets {|v1,v2| puts “#{v1} #v2”} => “1 2” “2 3” “3 4”.
Methods included from XRVG::Splittable
#apply_split, #apply_splits, #split, #splits
Methods included from XRVG::FloatFunctor
#alternate, #apply, #applyhash, #filter, #generate, #geo, #geofull, #modify, #process, #random, #sin, #ssort, #transform, #transforms, #trigger
Methods included from XRVG::Samplable
#apply_sample, #apply_samples, build, #sample, #samples
Instance Method Details
#addfilter(newfilter) ⇒ Object
468 469 470 |
# File 'lib/utils.rb', line 468 def addfilter( newfilter ) self.each {|v| v.addfilter( newfilter )} end |
#choice ⇒ Object
return a random item from array
319 320 321 |
# File 'lib/utils.rb', line 319 def choice return self[Kernel::rand(self.size)] end |
#compute(inputs, type, &block) ⇒ Object
FloatFunctor overloading to synchronize content sampling and splitting
464 465 466 |
# File 'lib/utils.rb', line 464 def compute( inputs, type, &block ) return self.map {|v| v.compute( inputs, type )}.forzip(nil,&block) end |
#flattenonce ⇒ Object
flatten an array of arrays
[[1,1], [2,2]].flattenonce => [1,1,2,2]
344 345 346 347 348 349 350 |
# File 'lib/utils.rb', line 344 def flattenonce result = [] self.each do || result += end return result end |
#foreach(arity = nil, &block) ⇒ Object
same as Enumerator.each_slice with implicit size given by block.arity, or explicit if no blocks (in that case, return array of array) same enumeration model as for Tcl foreach command (see Array.zip method for further compatibility)
[1,2,3,4].foreach {|v1,v2| puts "#{v1} #{v2}"} => "1 2" "3 4"
356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'lib/utils.rb', line 356 def foreach( arity=nil, &block ) if not arity arity = block.arity end if block if arity == 1 return self.each( &block ) else return self.each_slice(arity, &block) end else return self.enum_slice(arity).to_a end end |
#forpattern(pattern, &block) ⇒ Object
in same idea as forzip, but with explicit array index if pattern is nil, is equivalent to [0,1,…, self.size-1]
[ [1,2,3,4], [a,b] ].forpattern( [0,0,1] ) => [1,2,a,3,4,b]
[ [1,2,3,4], [a,b] ].forpattern( [0,1,0] ) => [1,a,2,3,b,4]
Rke : an interesting application is to use this method to filter some item periodically
for example [[array]].forpattern( [0,0] ) {|i,j| result.push( i )} take only first item on a pair (to be tested)
Rke2 : not so usefull for the moment (since compared with forzip, the only added value is to allow permutations of values between different subarrays)
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 |
# File 'lib/utils.rb', line 431 def forpattern(pattern, &block) cindexes = Array.new( self.size, 0 ) result = [] while true newitem = [] pattern.each do || newitem.push( self.[]( )[ cindexes[ ] ] ) cindexes[ ] += 1 end if newitem.compact.size == 0 break end result += newitem end # result = result.flatten if block return result.foreach( nil, &block ) end return result end |
#forzip(aarity = nil, &block) ⇒ Object
aarity = array of arity if nil, default value is Array.new( self.size, 1 ) size of aarity must be inferior or equal to self.size. If inferior, is completed with 1 Rke : with array size 1, is equivalent to foreach
[ [1,2,3,4], [a,b] ].forzip( [2,1] ) => [1,2,a,3,4,b]
[ [1,2,3,4], [a,b] ].forzip => [[1,a], [2,b], [3,nil], [4,nil]]
[ [a,b], [1,2,3,4] ].forzip => [[a,1], [b,2], [nil,3], [nil,4]]
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 |
# File 'lib/utils.rb', line 404 def forzip(aarity=nil, &block) if not aarity aarity = Array.new( self.size, 1 ) end if aarity.size < self.size aarity = aarity.concat( Array.new( self.size - aarity.size, 1 ) ) end tozip = Array.new self.zip( aarity ) do |, arity| tozip.push( .foreach(arity) ) end result = tozip[0].zip( *tozip[1..-1] ) result = result.flattenonce.flattenonce if block return result.foreach( nil, &block ) end return result end |
#half(&block) ⇒ Object
alias for sub(2)
[1,2,3,4].half => [1,3]
338 339 340 |
# File 'lib/utils.rb', line 338 def half( &block ) return sub(2,&block) end |
#mean ⇒ Object
returns the mean of the array content
[V2D[0.0,0.0], V2D[1.0,1.0]].mean => V2D[0.5,0.5]
314 315 316 |
# File 'lib/utils.rb', line 314 def mean return self.sum / self.size end |
#pairs(&block) ⇒ Object
alias for uplets(2, &block)
[1,2,3,4].pairs {|v| puts "#{v[0]} #{v[1]}"} => "1 2" "2 3" "3 4"
387 388 389 |
# File 'lib/utils.rb', line 387 def pairs( &block ) return self.uplets(2, &block) end |
#range(proc = nil) ⇒ Object
compute range of an array by returning (min..max)
[1.0, 3.0, 2.0].range => (1.0..3.0)
if proc supplied, use it to return range of subvalues
[V2D::O, V2D::X].range( :x ) => (0.0..1.0)
327 328 329 330 331 332 333 334 |
# File 'lib/utils.rb', line 327 def range( proc=nil ) if not proc return (self.min..self.max) else arraytmp = self.map {|item| item.send( proc )} return arraytmp.range end end |
#shuffle ⇒ Object
shuffle values in array
453 454 455 |
# File 'lib/utils.rb', line 453 def shuffle return self.sort_by{ rand } end |
#sub(period, &block) ⇒ Object
take only the nieme elements
Experimental
[1, 2, 3, 4].sub(2) => [1, 3]
288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/utils.rb', line 288 def sub(period, &block) result = [] self.foreach(period) do |slice| item = slice[0] if block yield item else result.push( item ) end end return result end |
#sum ⇒ Object
return the sum of the elements of the Array works for array whose content defines the + operator
[1.0, 2.0].sum => 3.0
[V2D[-1.0,-1.0], V2D[1.0,1.0]].sum => V2D[0.0,0.0]
[curve1, curve2].sum => concatenation of curves
306 307 308 309 310 |
# File 'lib/utils.rb', line 306 def sum sum = self[0] self[1..-1].each {|v| sum += v} return sum end |
#triplets(&block) ⇒ Object
alias for uplets(3, &block)
[1,2,3,4].pairs {|v| puts "#{v[0]} #{v[1]} #{v[2]}"} => "1 2 3" "2 3 4"
393 394 395 |
# File 'lib/utils.rb', line 393 def triplets( &block ) return self.uplets(3, &block) end |
#uplets(arity = nil, &block) ⇒ Object
same as Enumerator.each_cons with implicit size given by block.arity, or explicit if no blocks (in that case, return array of array)
[1,2,3,4].uplets {|v1,v2| puts "#{v1} #{v2}"} => "1 2" "2 3" "3 4"
374 375 376 377 378 379 380 381 382 383 |
# File 'lib/utils.rb', line 374 def uplets(arity=nil, &block ) if not arity arity = block.arity end if block return self.each_cons(arity, &block) else return self.enum_cons(arity).to_a end end |