Subtraction of two arrays with incremental indexes of the other array to a maximum limit

I have lots of math to do on lots of data but it's all based on a few base templates. So instead of say, when doing math between 2 arrays I do this:

results = [a[0]-b[1],a[1]-b[2],a[2]-b[3]]

I want to instead just put the base template: a[0]-b[1] and make it automatically fill say 50 places in the results array. So I don't always have to manually type it.

What would be the ways to do that? And would a good way be to create 1 method that does this automatically. And I just tell it the math and it fills out an array?

I have no clue, I'm really new to programming.

Answers


Custom

  a = [2,3,4..............,1000]
  b = [1,2,3,4,.............900]

  class Array
   def self.calculate_difference(arr1,arr2,limit)
    begin
     result ||= Array.new
     limit.send(:times) {|index| result << arr1[index]-arr2[index+=1]} 
     result
   rescue
    raise "Index/Limit Error"
   end
  end
 end

Call by:

Array.calculate_difference(a,b,50)

a = [2,3,4]
b = [1,2,3,4]

results = a.zip(b.drop(1)).take(50).map { |v,w| v - w }

Need Your Help

Can I inject proxies in front of Blueprint services published by other bundles?

java osgi blueprint-osgi

We are using Karaf and a number of OSGI Blueprint services to implement a system.

Get session after log in with JavaScript SDK on serverside and get the username also

facebook facebook-graph-api facebook-javascript-sdk facebook-php-sdk

I am trying to do my first Facebook log in. So I am using the Facebook SDK for JavaScript for implementing the pop up log in and then i want to use the SDK for PHP to store the username in the data...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.