Category: Uncategorized
Weird Snippet to Divide an Array [ruby]
Dividing an array of n elements into pieces subarrays is a little bit trickier than it seems. Each subarray will contain chunk_size = n / pieces elements and n % pieces will remain unassigned, and need to be distributed to the subarrays. You can do this in many ways, but in this case we will assign the first remaining element to the first subarray, and so on. So the first n%pieces subarrays will contain n/pieces+1 elements.
def chunk_array(a,pieces)
cs = a.length / pieces #chunk size
r = -1*(a.length%pieces) #remaining elements to be asigned to a chunk
(0..pieces-1).map{|i| r+=1; a[i*cs..(i+1)*cs-1] + (r<1 ? a[r-1..r-1] : [])}
end
C# & Linq Functional Style – Simple XML Parsing
With things like this, C# is regaining my attention. Parse a simple XML to a string array containing only the child elements defined in an other string array:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace PruebaLinqFuncional
{
class Program
{
public static List ParseXML(string xml, string objectName, string[] dataToParse)
{
//Programacion funcional en C# :O
/*
return XDocument.Parse(xml).Descendants(objectName).Select((element) =>
{
var lst = new List();
foreach (string field in dataToParse)
lst.Add(element.Element(field).Value);
return lst.ToArray();
}).ToList();
*/
return XDocument.Parse(xml).Descendants(objectName).Select( (element) =>
{
return dataToParse.Select( (field) =>
{
return element.Element(field).Value;
}).ToArray();
}).ToList();
}
static void Main(string[] args)
{
var xml = @"
string-value
string-value
int-value
string-value
Mi_URL_Uno
Mi_Blob_Uno
date/time-value
etag
size-in-bytes
blob-content-type
Mi_URL_Dos
Mi_Blob_Dos
date/time-value
etag
size-in-bytes
blob-content-type
blob-prefix
";
foreach (var stra in ParseXML(xml, "Blob", new string[] { "Url", "Name" }))
Console.WriteLine(string.Join(",", stra));
Console.ReadLine();
}
}
}
GUIDs in Ruby
sudo gem install guid require 'guid' Guid.new
http://rubyforge.org/forum/forum.php?forum_id=4743 go to the bottom)
On Windows this might be useful:
http://www.agileprogrammer.com/dotnetguy/archive/2005/10/27/8991.aspx
StackOverflow Architecture
http://highscalability.com/stack-overflow-architecture|It’s good to see that we are doing similar things…