Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Generic algorithms

Name: Anonymous 2008-11-28 11:31

Implement a single function that takes two arguments and returns the bigger of the two. Assume you don't know the type of the  arguments and you don't know if the types can be compared. Assume that if a type can be compared, it will always be implemented by following a single standard. Use the latest standard of your language. Write the simplest program
that will pass an integer 1(one) and a float 1.1(one point one) into the function and write the result to standard output.

Post the compiler you used, the code and the result.
Write what will happen if the max function is called with broken syntax (I'm looking at you C macros).
Write what will happen if the objects of a type can't be compared.

I'll start with C and C++.
Compiler: gcc 4.3.2 20081105 (Red Hat 4.3.2-7)
#include <iostream>
template<class F>
F& max(F& a, F& b) { return (a < b) ? b : a; }
int main() { std::cout << max(1, 1.1) << '\n'; }

Results: Compile time error:
max.cpp: In function ‘int main()’:
max.cpp:6: error: no matching function for call to ‘max(int, double)’

Bad syntax: Standard compile-time error
No comparison: Standard compile-time error about undefined operator<

#define max(a, b) (less(a, b) ? b : a)
#include <stdio.h>
int less(int a, int b) { return a < b; }
int main() { printf("%f\n", max(1, 1.1)); }

Result: Bad output
1.000000
Bad syntax: Depends on the error in the syntax. Can either compile and cause undefined behaviour or fail at compile time with strange syntax errors.
No comparison: Standard compile-time error about an undefined function.

Name: Anonymous 2008-12-01 18:56

>>59

Jesus christ, more idiot programmers.

>You shouldn't store and manipulate a date in it's original format. That's retarded. You should always store it as an integer and compare it as that.

Speaking of retarded, a retard would not understand that datetime types from any decent library take care of storing date times as an integer internally. If you feel compelled to work with datetimes as an integer and not a datetype type, you are a fucking retard. Did you think there might be a reason that datetime types are fairly ubiquitous?

>are fucking retarded ways to implement a max function.

And to further prove to you that you are indeed, a retard; the original posed asked to "you don't know if the types can be compared. Assume that if a type can be compared, it will always be implemented by following a single standard." Then the very example used in the OP was for 2 numbers of different types.

So already, the challenge is to return the maximum value of 2 things that might not be the same type, but could be compared. So type conversion is at the very heart of the challenge, retard.

The you go on to retardedly say:
I've given my examples a single type parameter for a reason.

A retard like yourself does not understand that >>31 and >>36 do take a single fucking type parameter.

So now that you understand the challenge was a generic max function that can handle different types that can be compared, you should understand that those 2 actually did it fucking right, and you are too much of a retard to understand that.

Which brings me back to the whole why the fuck did I bring up dates and strings. See, you were too retarded to follow, so more spoon feeding of a retard.

If you need to gather a date from a user, it is not uncommon or unreasonable to ask for that date as a string that they input in to the app. Any decent datetime type can create the datetype from a string input. So given the perfectly valid generic function provided for .net, one can certainly compare 2 strings of formatted date information. The only drawback was it expected dates than an American (and some other cultures) would put in. The fix to allow for a user to easily enter date information according to the standards of their culture was quick and simple, so it is unreasonable to force the user to conform to your ignorant and inflexible single standard just because you are lazy and stupid. Also, datetime type is a simple example for this, there are other cases where this would be used also.

Now you could be butt-hurt and say that the data should be corrected before passed to the function and blah blah lazy ignorant bullshit. But then once more, you would be missing the point like the little faggy retard you are.

If the function is going to be generic and useful and do type conversion, it needs to handle the format of the data it is converting. I could care less if you are too stupid to understand the example is just an example and an apt one. The example is a simple way to demonstrate further concerns, but the attempt was to condence it to something as simple as a date, which we thought retards like yourself could understand.

Now you stupid little faggy retard, if you still do not understand, DO NOT FUCKING POST. You will make yourself look even stupider.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List