Class: Array

Inherits:
Object show all
Defined in:
lib/utils.rb

Overview

Array extension (see SyncS for enumeration synchronisation) See this for presentation

Instance Method Summary collapse

Instance Method Details

#choiceObject

return a random item from array



320
321
322
# File 'lib/utils.rb', line 320

def choice
  return self[Kernel::rand(self.size)]
end

#flattenonceObject

flatten an array of arrays

[[1,1], [2,2]].flattenonce => [1,1,2,2]


367
368
369
370
371
372
373
# File 'lib/utils.rb', line 367

def flattenonce
  result = []
  self.each do |subarray|
    result += subarray
  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"


379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/utils.rb', line 379

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)



454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# File 'lib/utils.rb', line 454

def forpattern(pattern, &block)
  cindexes = Array.new( self.size, 0 )
  result = []
  while true
    newitem = []
    pattern.each do |arrayindex|
  newitem.push( self.[]( arrayindex )[ cindexes[ arrayindex] ] )
  cindexes[ arrayindex] += 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]]


427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'lib/utils.rb', line 427

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 |subarray, arity|
    tozip.push( subarray.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]


361
362
363
# File 'lib/utils.rb', line 361

def half( &block )
  return sub(2,&block)
end

#meanObject

returns the mean of the array content

[V2D[0.0,0.0], V2D[1.0,1.0]].mean => V2D[0.5,0.5]


315
316
317
# File 'lib/utils.rb', line 315

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"


410
411
412
# File 'lib/utils.rb', line 410

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)


350
351
352
353
354
355
356
357
# File 'lib/utils.rb', line 350

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

#rotate(sens = :right) ⇒ Object

return an array with same elements as self, but rotated



325
326
327
328
329
330
331
332
333
# File 'lib/utils.rb', line 325

def rotate(sens=:right)
  result = []
  if sens == :right
    result = [self[-1]] + self[0..-2]
  else
    result = self[1..-1] + [self[0]]
  end
  return result
end

#rotations(sens = :right) ⇒ Object

generate every rotation for the array, with as first element self



336
337
338
339
340
341
342
343
344
# File 'lib/utils.rb', line 336

def rotations(sens=:right)
  result = [self]
  current = self
  (self.size-1).times do 
    result << current.rotate(sens)
    current = result[-1]
  end
  return result
end

#shuffleObject

shuffle values in array



476
477
478
# File 'lib/utils.rb', line 476

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]


289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/utils.rb', line 289

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

#sumObject

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


307
308
309
310
311
# File 'lib/utils.rb', line 307

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"


416
417
418
# File 'lib/utils.rb', line 416

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"


397
398
399
400
401
402
403
404
405
406
# File 'lib/utils.rb', line 397

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