One Point Solution

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Tuesday, 13 March 2012

Language features: Rants and wishes

Posted on 20:06 by Unknown
Every time the google code jam approaches I ponder the idea of making a preprocessor of sorts to be able to "use my own language" in the qualification rounds. But then I notice that there is little time left. Either way, these are things I really wish I could use during contests, without losing the good powerful things such as the STL that c++ has, of course.

Loops in general blow
For loops are such basic construction blocks that you always use yet in c++ and many other languages they are so messed up and limited...

Look at this:
for(int i=0; i<4; i++) {
}


What is wrong with that? You may ask. Well, note how you type the variable i three times. Were the guys at the C council of standards really unable to ponder about "once and only once"? I am just saying... making funny bugs and wasting at least 3 seconds finding out you mistyped one of the three instances of the variable and put j instead of i gets very old.

It is not just the possibility of bugs or having to type things multiple times. I sometimes wish the language didn't expect me to code something as common as a walk from 0 to 6 having to do so many manual things like the comparison function and the increments... I once dreamed of something like this:

for( 0 <= i < 4) {}


Imagine that, it is clearly saying that you will repeat the operations for numbers from 0 to 3. You can control bounds being exclusive or not. And it is very expressive. Of course, it is not perfect and I am not saying it is. But I actually implemented this in a custom language I wrote for Warcraft 3 map making. And it was so cute... Anyway, serious languages have solutions of their own, like python's range() function. A lot of these solutions are better than what C, C++ and Java offer in this regards.

The other day, I pondered about loops that need you to iterate through pairs or tuples of many variables. Imagine you could just do this, in a single loop.

for (1 <= a < b < c < n) {


Sounds fun.

Solution?
- Switch to python , some other language? Not very viable in most contests. And well, I do like C++ better than other languages overall. I really like the STL during contests, so this is not so easy.
- MACROS. This is what a lot of C++ coders in high positions do. They for example have a FOR(v,n) macro that implements for (int v=0; v<n; v++). This fixes the issue with having to type the same variable thrice but it introduces the issue of using macros and making your code look uglier.
- Give up and live with it.

Lack of a for each construct
I covered this before when I explained why I use the for_each #define. It is still quite lame to have to use a #define for this.

Just something, I just found out c++11 kind of implements this. http://en.wikipedia.org/wiki/C%2B%2B11#Range-based_for-loop

Lack of var
Something c++11 (or c++0x) does well is add the auto keyword. It will work like this:
map< pair<int,int>, string > thesupermap 
auto q = thesupermap.begin();
//get it? This is the equivalent of the good oldie
// map<pair<int,int>, string>::iterator q = thesupermap.begin()



This should be of great value to anyone who has attempted to use the STL and has not gone insane in the process.

But there is a problem. Why call it auto? I wish it was named var, like in some other language. It seems they just wanted to recycle a keyword instead of adding a new one.

The lack of this feature really hurts. The good news is that it is probably already possible to use it in the google code jam, shall you get a compiler that supports the new c++ specification. The bad thing is that it is less likely in topcoder. Which uses a C++ compiler from the stone age and is likely to ever update the language versions.

Solution?
- Use c++11, if possible.
- Use a #define. Like in the case of for_each, the typeof() macro can help. The problem is that the syntax of declaring a variable will look like this: var(q, s); instead of auto q = s;
- Live with it.
- Lobby g++ to implement c++11 fully, then lobby topcoder to use the new g++ version.

I miss <?, >?, <?= and >?=
These are operators that do operations so basic that it is hard to believe that after gcc invented them, they decided to deprecate them instead of lobby the c++0x council to make them global standards.

Explanation : (5 <? 3) returns 3. That's right, it gets the minimum of two numbers. >? the maximum and, even nicer: x <?= 5 is the same as x = min(x, 5).

Something good about the old g++ version in TopCoder is that this obscure feature is still supported. The bad thing is that it is not a standard, and later compilers removed the features. So I had to stop using it.

There was so much potential in this. Surely, it causes confusion, but that is only because it is a rare feature. I am sure that if *= was not a standard everybody would get very confused about finding code that says x *= 4.

I would have made them better though. Let them do what std::min and std::max do. So, for example, imagine you could do this:

string s[5] = {"aab", "bbc", "xxx", "h", "lxa" }; 
string lex = s[1];
for (1 <= i < 5) {
lex <?= s[i];
}


To find the lexicographically smallest of 5 strings...

Solution?
- Use old g++ versions. Good luck with that. Codeforces does not support this. And in recent ubuntu versions, for example, gcc 4.0 is not in the repositories.
- Live with it.

Native bignum support
Easy, I wish you could just do (big x = "3483389483893382929284349493839338") and then use x in all sort of operations.

Solution?
- Switch to another language. But not Java because their bignum support does not do this. A lot of languages can do this, some without even asking... (Which is not a good thing, if instead of overflow your program suddenly gets magnitudes slower)
- Really, if you can use non-standard libraries just get gmp and do a typedef.
- If you can't use libraries, well... live with it.

I really wish c++11 implemented this, but it won't. Surely you can code your own big nums, but not so much, and in fact there is such a plethora of optimizations and aspects that it is better to use a well-tested wheel rather than make your own. Plus bignums in contests are the archetypical Fake difficulty stunt.


.begin(), .end() ad nauseum
Speaking of using the STL and going insane in the process. If there is something that is really broken about it, is that you have to do (v.begin(), v.end()) so many times... Once again, this is an explicit attack against "once and only once".

Solution?
- Live with it.
- Coders tend to make an ALL(v) #define that translates to v.begin(), v.end(). Once again, it fixes one problem and introduces another.

Stupid !@#~% long long
I hate having to type long twice. I really do. Specially because there is a pointless "long" type that does the same as "int". c++11 does nothing to fix this.

I have resorted to using a define in my code that fixes this, and once again introduces tons of other issues.
Email ThisBlogThis!Share to XShare to Facebook
Posted in implementation, languages, rant | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • TopCoder SRM 557 - finally
    SRM 557 Explanation for division 1 Easy and match recap. Explanations for div2 easy and div2 medium. It feels like it has been ages since t...
  • SRM 589 Editorial
    I have finished writing the editorial for TopCoder SRM 589: http://apps.topcoder.com/wiki/display/tc/SRM+589 . As you most likely noticed. L...
  • SRM 590 recap and editorial
    Another week another Topcoder match. Not a great day. I had a bad flu and still do. Div1 500: The one with Xor Given a list of cards with nu...
  • SRM 546: relief
    I figured I should post something about this SRM. I've been very busy these weeks because the semester is ending and I tried to win a t-...
  • SRM 526: The killing wait for results
    While I wait for results, here is my perspective on this algorithm contest. It began with issues, it had to be postponed 15 minutes. TC has ...
  • SRM 554 div1 hard: TheBrickTowerHardDivOne
    Link to problem statement We got infinitely many bricks of dimensions 1x1x1 and C different colors. Count the number of towers of size 2x2...
  • SRM 533: Div1 500 MagicBoard explanation
    Finally solved it. It is a nice problem that is worth explaining in a post. You have a grid/board of at most 50x50 cells. Some cells contain...
  • Member SRM 505: Part 1
    So, let me explain a couple of problems from a Topcoder Member SRM that I wrote and never got an editorial. BTW, it was the last member SRM....
  • ListedLinks 2012-02-10
    Saturday Morning Breakfast Cereal comics: Grace Hopper's ghost That Oracle engineer blog post Oracle would really not like anyone to se...
  • Codeforces "Good bye 2013" round
    So it was a special round for coders of both divisions, problems ranged from the super easy problem A to the super difficult problems E,F,G....

Categories

  • acm
  • algorithm
  • answers
  • arenaplugin
  • badday
  • behindthescenes
  • bugs
  • c++
  • censorship
  • codechef
  • codeforces
  • contests
  • crocchamp
  • editorial
  • editorial.srm
  • embarrassing
  • explanation
  • gcj2013
  • gmp
  • goodday
  • google
  • googlecodejam
  • greed
  • groklaw
  • health
  • html
  • httpseverywhere
  • implementation
  • ipsc
  • ispc
  • java
  • kawigiedit
  • kindagoodday
  • lamebook
  • languages
  • lego
  • listedlinks
  • marathon
  • nasa
  • offtopic
  • ouch
  • postmortem
  • postportem
  • practical
  • probably_not_a_good_tip
  • problemsetting
  • programming
  • python
  • quora
  • rant
  • recap
  • slightlygoodday
  • snippet
  • srm
  • stl
  • strategy
  • swerc
  • tco
  • tco12
  • tco13
  • tco2012
  • tco2013
  • ternarysearch
  • topcoder
  • tricks
  • ubuntu
  • uva
  • vjass
  • vkcup
  • wc3
  • zinc

Blog Archive

  • ►  2014 (1)
    • ►  January (1)
  • ►  2013 (141)
    • ►  December (14)
    • ►  November (8)
    • ►  October (13)
    • ►  September (11)
    • ►  August (14)
    • ►  July (15)
    • ►  June (13)
    • ►  May (13)
    • ►  April (12)
    • ►  March (11)
    • ►  February (11)
    • ►  January (6)
  • ▼  2012 (94)
    • ►  December (5)
    • ►  October (6)
    • ►  September (8)
    • ►  August (6)
    • ►  July (3)
    • ►  June (5)
    • ►  May (8)
    • ►  April (10)
    • ▼  March (20)
      • Topcoder Open 2012 round 1A
      • Codeforces round #114. 167C: Wizards and numbers
      • Official TCO 2012 blogger
      • Codeforces round #114 (div1)
      • 5 reasons editorial votes in Topcoder are useless
      • Codeforces VK Cup round 2: (ouch)
      • Codeforces round #113 div2 (unofficial)
      • Writing SRM 538
      • SRM 537: oh my
      • Codeforces round #112
      • Language features: Rants and wishes
      • Google code jam registration - Just note something...
      • Codeforces: VK Cup round 1: Problem C: Abracadabra
      • Codeforces: VK Cup round 1 (update 1)
      • SRM 536: heh
      • SRM 535 editorial: The making of
      • Codeforces round #111 (div2 only)
      • SRM 535 : lame
      • A note about SRM 537 and 540 money prizes
      • The March of Destruction begins!
    • ►  February (16)
    • ►  January (7)
  • ►  2011 (51)
    • ►  December (7)
    • ►  November (12)
    • ►  October (5)
    • ►  September (1)
    • ►  August (3)
    • ►  July (4)
    • ►  June (3)
    • ►  May (7)
    • ►  April (3)
    • ►  March (2)
    • ►  February (1)
    • ►  January (3)
  • ►  2010 (9)
    • ►  December (4)
    • ►  October (1)
    • ►  June (1)
    • ►  May (1)
    • ►  January (2)
  • ►  2009 (1)
    • ►  December (1)
Powered by Blogger.

About Me

Unknown
View my complete profile