Friday, January 18, 2013

Generic array slice

Not sure if anyone created(in delphi) something similar yet, which should indicate that it's usage is very limited, but here it goes...

Ever played with huge arrays and you always have to copy chunks into smaller arrays which then you want to feed to some algorithm(or pass a bunch of indexes)? if yes, then you should know by now that a "slice of TYPE" functionality kinda', shoulda' be available by now in Delphi's compiler... anyhu' with the introduction of generics, we got the options to shoot ourselves in the foot or come up with good ideas, depending on your POV.

So, what is an array slice? Well, wiki says that it grabs a slice of an array and puts it into another array, cool, but that's a bit expensive because I have to copy a block of memory into another, we can surely do better than that, right? well, with generics we can:

procedure TestSlice;
var
  LIntList: TArray<Integer>;
  LSlice: TArraySlice<Integer>;
  Index: Integer;
begin
   ///
   /// fill LIntList
   ///
   SetLength(LIntList, 10);
   for Index := Low(LIntList) to High(LIntList) do
     LIntList[Index] := Index;
   ///
   /// "grab" a slice of LIntList
   ///
   LSlice.SliceArray(LIntList, 0, 5);
   for Index := LSlice.Low to LSlice.High do
     WriteLn(LSlice[Index]);
end;

So, what are we doing in the code above? well, we're creating an array of 10 elements and we set the value of each element to be it's index in the array(I am lazy), next, we grab the first 5 elements from array as a slice and then we walk over the slice and print the elements, the output should be:
0
1
2
3
4

Pretty cool, huh? in case you're wondering, TArraySlice is a generic record, therefore initialization and finalization is done by compiler and it holds very little information(reference to "mother|main array", it's offset, count and original array size).

Here's the whole code of TArraySlice, very simple and yet powerful and incomplete (:

Not feeling like writing too much about it, after all it's a very simple thing so I'll let you have fun with it and if you find something useful to add and would like to share, feel free to comment.

Wednesday, September 12, 2012

Facebook Timeline Cleaner

Yesterday was assigned to make a tool(dirty and fast) to delete posts on facebook, it wasn't important how it accomplished it, but how much one has to interact with it before it's job is finished.
I have permission to share this code with you, without any warranty or restrictions.
NOTE: your posts don't actually get deleted, they use it for ad-analysis and probably other stuff.

What you need:
- Chrome browser
- facebook account
- a ton of stuff you want to delete fast

Step by step instructions:
  1. log into your facebook account
  2. click on your name on the top right corner of your page
  3. scroll down to a "year" that you want to wipe
  4. look at the top of the page(just bellow the "web address field in Chrome") for a few buttons, select "activity log"
  5. in activity log select "your posts" or whatever you want to delete
  6. click F12, this should bring the "javascript console" at the bottom of your Chrome browser
  7. copy-past the following code into the console and hit Enter key:
  8. now copy-past "grab_stories()" without quotes in the console, hit Enter and enjoy
NOTE: there are still some hick-ups, but for about 90%-ish of the time, you don't have to do anything, you can use the browser to do other things in other tabs BUT the tab in which the above javascript runs.
There's no way of selecting a period in the code, it won't detect that it has "nothing else to do", however, it will click on "More Activity" to grab more "ministories".

Blogroll(General programming and Delphi feeds)