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-11-30 20:19

>>39

No, you are a fucking idiot. To expand and spoon feed you more on the example I already provided, lets use that function to see which is later, January 2nd 2009 or February 1st 2009.

If I wanted to compare some user inputted string it would be called like this:
Console.WriteLine(Compare<DateTime>.Max("01/02/2009", "02/01/2009"));
And the output is correct: 02/01/2009

A britfag would use the same inputs, and get the same result as is. To a britfag, the result reads that January 2nd is later than February first. This of course is fucking wrong, and you somehow think that makes it more compatible.

Using System.Threading.Thread.CurrentThread.CurrentCulture, will correct the comparison (assuming the thread is set with something like: System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-GB");

Not only will the program behave as expected for peoples all over the world, it is a simple change that will not blow anything fucking up. There is no chance that would introduce a bug. It fixes the bug of the function not accounting for the format of different data types in different cultures.

The fact I initially gave a very example, you didn't understand it, then claimed it would 'blow up' despite contending that the globalization feature makes apps more compatible, ad the best way to use it is to not use it (format provider is required in the call to ToType, and specifying InvariantCulture means don't apply any formatting). As in the simple date example, formatting is important.

In summary, you are an idiot faggot.

Name: Anonymous 2008-11-30 20:35

>>41
Why are you getting so upset over a small programming question? Did mommy and daddy not hug you enough when you were a child?

Name: Anonymous 2008-11-30 21:01

>>37
and if you're not a faggot, you would say it's not a valid date because there aren't 2009 days in january.

Name: Anonymous 2008-11-30 21:03

>>43
or february, since >>37 did say "01/02/2009" and not "01/01/2009".

Name: Anonymous 2008-11-30 21:07

One word, ISO 8601, thread over.

Name: Anonymous 2008-11-30 21:27

>>43

What the nigger fuck do you think you are trying to talk about? 2009 is a year. It is a year in both the commonly used mm/dd/yyyy and dd/mm/yyyy formats.

At no point does the data 01/02/2009 have anything to do with there been 2009 days in January. The date 01/02/2009 is a valid date in either of those common formats.

Only an incredible master faggot would come up with something so stupid.

>>45

Yeah, because the average person knows what that is. Faggot.

Name: Anonymous 2008-11-30 21:38

>>46
commonly used by faggots. it's not a valid ISO 8601 date.

yyyy-mm-dd or GTFO.

Name: 33 2008-11-30 22:12

>>37
what

I know what that does and why it is there. It's just that System.Globalization.CultureInfo.InvariantCulture and System.Threading.Thread.CurrentThread.CurrentCulture are probably even a step below NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, which you don't seem to notice in your RAGE.

Name: Anonymous 2008-11-30 22:54

>>47

As I ALREADY FUCKING SAID, the average person is not going to know what that is if it is up to them to work with dates in an application. Even if your gave an input hint with something like yyyy-mm-dd they would still fuck it up, people are fucking stupid.

Now if you are an expert loonix programer you would require only the ISO date. You would give no input hint. You would not document this. Someone will have to find that fact on some obscure outdated wikipage.

Name: Anonymous 2008-11-30 23:36

>>49
ISO 8601! ISO 8601! ISO 8601 IS THE STANDARD!!!
http://isotc.iso.org/livelink/livelink?func=doc.Fetch&nodeid=4021199

Name: Anonymous 2008-11-30 23:59

>>49
I guess you're enjoying writing programs for idiots (judging by your posts' contents). We'll enjoy our loonix and obscure dates. Have an ice day.

Name: Anonymous 2008-12-01 0:35

Nah, I am just a good programmer. I write apps people can use. Not lazy shit because I am too shit eating reatrded to handle some data formats. Expecially when the solution is so fucking simple that a nigger like you can't even understand it.

Name: Anonymous 2008-12-01 0:39

>>49
Actually they will find in the man page that all dates and times are to be POSIX time. Anyone who wouldn't like that won't have made it far enough into my program or the docs to even know about it. It's called defensive programming, learn it.

Name: Anonymous 2008-12-01 2:46

Every time I see this thread I read it as Genetic Algorithms. Instead it is a shitty troll war. I feel kind of bad about it :(

Name: OP 2008-12-01 4:52

Oh. my. God. What have I done. I am a faggort.

Name: Anonymous 2008-12-01 7:59

>>55
It's OK. We are all faggots here, my friend.

Name: Anonymous 2008-12-01 12:38

>>53

Enter the number of seconds elapsed since midnight Coordinated Universal Time (UTC) of January 1, 1970, not counting leap seconds:

Defensive loonix programming ftw!

Name: Anonymous 2008-12-01 12:52

>>57
What exactly is your point? No program prompts user for that. Stop spreading around your stupidity and just go back to /g/ already.

Name: OP 2008-12-01 16:15

>>58
He has a point.
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. Conversion should be left to the UI.

>>31
and
>>36
are fucking retarded ways to implement a max function.
Why the fuck should the max function care about type conversion, it's not it's fucking job. I've given my examples a single type parameter for a reason.

Name: Anonymous 2008-12-01 16:44

>>59
What's retarded is not knowing the difference between its and it's.

Name: Anonymous 2008-12-01 17:31

>>59
I've given my examples a single type parameter for a reason.
So that means I can't compare objects of different types, even if operator< is defined for them?

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.

Name: Anonymous 2008-12-01 21:42

>>62,59
Same person.

Name: Anonymous 2008-12-01 23:05

>>1-
Same person.

Name: Anonymous 2008-12-02 2:12

>>63

You are talking that fail meme a little too far with that.

Name: Anonymous 2008-12-02 2:17

>>65
talking

Name: Anonymous 2008-12-02 7:35

class Cast a b where
   cast :: a -> b

instance Cast a a where
   cast = id

instance Cast Int Float where
   cast = fromIntegral

pmax x y = cast x `max` cast y

main = print $ (pmax (1 :: Int) (1.1 :: Float) :: Float)


Result: 1.1
Bad syntax: Syntax error
No cast: Missing instance error

Name: Anonymous 2008-12-02 9:34

>>62

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.
Yes, you demonstrated that perfectly with your examples. You compare two objects of that internal, library type. Not a fucking number and string and convert them in a max function!

Then the very example used in the OP was for 2 numbers of different types.
Because the exercise was meant to show how a given language handles a case when the function is called with different types. Does it do a implicit conversion? Does it just gobble it up and doesn't care? Does it rape you with 5 pages of template errors?

A retard like yourself does not understand that >>31 and >>36 do take a single fucking type parameter.
>>36 in fact, does not. But you've already shown you're an angered EXPERT PROGRAMMER and don't need to think through what you've read.

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.
ihbt

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
It should not. The point of a generic algorithm is that it will work on all types which implement an interface that that algorithm requires. It doesn't care if it's apples, oranges or a bunch of retarded motherfuckers like you, because it's GENERIC.

You have entirely missed the point of the exercise you homosexual cumdupster.

Name: Anonymous 2008-12-02 9:50

Too many Reditors in one thread :(

>>67
I liek yuo :3

Name: Anonymous 2008-12-02 15:09

>Because the exercise was meant to show how a given language handles a case when the function is called with different types. Does it do a implicit conversion? Does it just gobble it up and doesn't care? Does it rape you with 5 pages of template errors?

If you need the details of a language/compilers abilities of coercion or lack thereof, you do not need to write fucking code to find out. RTFM.

>Not a fucking number and string and convert them in a max function!

A simple mind like yours is not able to understand the implications of the simple examples given. It is simple to compare an int and a string that contains number data for the purposes of demonstration. They are 2 dissimilar types that can be compared, the whole fucking point of this exercise. You have limited yourself to primitive types that are easily coerced because you can't seem to understand further implications of that for other types.

>ihbt

No, you were going down the path that you would NEVER store dates as strings. The point isn't to show that dates as string is a good thing, it is a simple demonstration of type conversion. So, for the purposes of example, taking user input and creating a datetime and then comparing those is fucking pointless for this exercise.

>It should not. The point of a generic algorithm is that it will work on all types which implement an interface that that algorithm requires. It doesn't care if it's apples, oranges or a bunch of retarded motherfuckers like you, because it's GENERIC.

Wow, this is obviously way over your head. If an apple can't be an orange, then we need to compare them with something they have in common, like that they are fruit. So apple and orange need to be converted to fruit. Not all fruits will need to. Granny Smith Apple might have the ability to be compared to McIntosh Apple, and a comparison of fruit might not be accurate enough in that scenario.

But as you said, generic functions do care about type because the type needs to implement something in common. Then you say it doesn't care about type. Oh lordy.

Name: Anonymous 2008-12-02 16:31

>I am unable or unwilling to quote properly.
7/10, IHBT.

Name: Anonymous 2008-12-02 17:06

No, because these fuckstick boards need to pick a standard and stick with it.

Name: Anonymous 2008-12-02 17:40

template<typename T1, typename T2>
T1 max(const T1& a, const T2& b)
{
    b = reinterpret_cast<T1> b;
    if(a < b)
        return b;
    return a;
}

Name: Anonymous 2008-12-02 17:59

>>73
Vomit.

Name: Anonymous 2008-12-02 19:12

>>73
reinterpret_cast doesn't do what you think it does, bucko.

Name: Anonymous 2008-12-02 20:33

>>75
From the C++ standard:

* The reinterpret_cast built-in keyword safely type-casts the type of the r-value to the type specified in the template specifier. reinterpret_cast can be used to perform the following conversions:
 + std::string to numerical primitive
 + IEEE 754 binary floating point to integer
 + integer to IEEE 754 binary floating point

Name: Anonymous 2008-12-02 20:52

>>73
C++ is probably the ugliest programming language I've ever seen

Name: Anonymous 2008-12-02 21:13

>>77
You're mom is probably the ugliest programming language I've seen.

Name: Anonymous 2008-12-02 21:20

>>78
You're dad is probably the sexiest programming language I've cummed inside of.

Name: Anonymous 2008-12-02 23:10

>>76
> cat test.cpp
#include <iostream>
#include <string>

int main( int argc, char *argv[] ) {
        std::cout << reinterpret_cast<int>( "1" ) << std::endl;
        return 0;
}

g++ test.cpp
./a.out
134515133


IHBT.

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