• Luk (unregistered)

    one of my quite usefull mathematic functions:

        public static int Max(params int[] values)
        {
            int max = int.MinValue;
            foreach (int value in values)
            {
                max = Math.Max(max, value);
            }
            return max;
        }
    

    so maybe, not all of them are useless?

    Don't ask me what I'm writing right now... It's not buissnes project

  • Ilya Ehrenburg (unregistered)

    Really, why are you all so negative? These functions have meaningful names and do what their names say. Isn't that perfect coding practice? Now if only they weren't so darned superfluous...

  • Rooser (unregistered)

    A pity this guy didn't enter the WTF Calculator contest.

  • (cs)

    Does nobody here code in functional style? Assuming C# doesn't already have something like add, it would be useful for passing to higher order functions (like map or reduce).

  • Matt (unregistered) in reply to Luk

    You could always try that great function

    public static int Max(params int[] values){ return values.Max(); }

    too. In fact you could generalize it to

    public static int Max(IEnumerable<int> values){ return values.Max(); }

    or even go for a generic solution

    public static T Max(IEnumerable<T> values){ return values.Max(); }

    or then again you could realize that re-programming things that come with the language is stupid.

  • Matt (unregistered) in reply to gekko

    They call it Lambda expressions and it comes with C#. For instance, I could code a foldl expression:

    ///

    /// Wraps a function that takes two parameters of type T,V -> V, folds over an array of type T from left-to-right. /// V foldl(IEnumerable<T> array, V initR, Func<V, V, T> func){ V curValue = initR; foreach(T t in array){ curValue = func(initR, t); } return curValue; } /// /// Special case /// T foldl(IEnumerable<T> array, Func<T,T,T> func){ bool isFirst; T curVal = default(T); foreach(T t in array){ if(isFirst) { curVal = t; continue; } curVal = func(curVal, t); } return curVal; }

    and now you can do the functional things you were talking about:

    double Sum(IEnumerable<double> listOfDoubles){ return foldl(listOfDoubles, (a,b) => a + b); } double Product(IEnumerable<double> listOfDoubles){ return foldl(listOfDouble, (a,b) => a * b); } double Max(IEnumerable<double> listOfDoubles){ return foldl(listOfDouble, (a,b) => Math.Max(a,b)); }

    etc.

  • Fraggle My Rock (unregistered) in reply to J. Walter Weatherman
    J. Walter Weatherman:
    Jesus Christ my ass itches.

    There's a good chance you have enterobius vermicularis (AKA Pinworms). You should talk to your GP and/or visit your local pharmacy for over-the-counter medication.

  • Easter Bunny (unregistered) in reply to incognito
    incognito:
    wow, i bet he was an A+ student back in university

    And I bet he did a bang-up job with the interviewer's "brain-teasers".

  • goat (unregistered) in reply to Matt

    Which came with the latest version of c#.

  • code pioneer (unregistered)

    TRWTF is that he thinks debugging your coworker's code is all there is to life.

  • Austin Smith (unregistered)

    Perhaps his background is in Common Lisp?

    (+ 2 4 (* 5 10))

    -> 56

    The real WTF is that he neglected to implement a complete and compliant Common Lisp interpreter in C#.

  • Shadesofgrey (unregistered) in reply to JonC

    Who says that's C++? Might be a language that doesn't support operator overloading.

  • Porpus (unregistered) in reply to Pyro
    Pyro:
    Meh, including same file everywhere is so lame. Real pro would compile it separately and link it as a library. Bonus points for naming library utils.dll.

    Yeah, static linking of library code is so unprofessional. Thank God Microsoft didn't keep #include in C#... really good C / C++ programmers never used it anyway. </sarcasm>

    I will even go farther than you and say that Utils.dll practically begs for an out-of-process COM implementation (Utils.EXE) preferably running on a dedicated MTS server.

  • cpp (unregistered) in reply to SenTree
    SenTree:
    belgariontheking:
    Well you know Communism was a good idea 'in general'
    Don't you mean 'for the Generals' ?

    Not for the ones Stalin talk care of in the 30s.

  • foo (unregistered) in reply to JonC

    I'm sorry, but this is the situation where it pays to have a "senior" programmer who can say. Don't do that kid... ya bother me. Do that again and I'll put you on the help desk for a week.

  • nick chan (unregistered)

    why is the Multiplication not using recursive operation? so not OOP

  • Mr. Bean (unregistered) in reply to fraserofthenight
    fraserofthenight:
    Why write something passe like "(a + b) * c)" when you could write the much more descriptive "Utils.Multiply(Utils.Add(a, b), c)". Plus, with the Utils class there's no longer any need to worry about that pesky order of operations. Truly only a brillant mind could have come up with that.

    The same truly brilliant mind that came up with COBOL?

  • Marco Mariani (unregistered)

    Heh. The real WTF is that it's statically typed.

    import operator, functools

    def do(what, *towhom): return reduce(getattr(operator, what), towhom)

    add = functools.partial(do, 'add') multiply = functools.partial(do, 'mul') subtract = functools.partial(do, 'sub')

    assert add(4,3) == 7 assert multiply(add(2,2),subtract(6,3)) == 12

  • SunTzuWarmaster (unregistered)

    I'm surprised that no one mentioned templates. I mean come on. If you are going to rewrite basic addition, at least make i so that you can do it to long doubles, floats, long floats, unsigned doubles, etc.

    I mean really, all that code does is add two doubles by using the + operator. At the VERY LEAST we could make it add more than one datatype.

    Also, for the record, there are many types when an array isn't given to you in an ordered list, or the array that you have was recently changed due to some earlier operation.

  • (cs)

    Where are: public static int returnInt(int input) { return input; }

    public static double returnDouble(double input) { return input; }

    public static string returnString(string input) { return input; }

    public static enum returnBool(bool input) { if (input == true) return true;

    if (input == false) return false;

    if (input != true && input != false) return FILE_NOT_FOUND; }

    ??

  • Walleye (unregistered) in reply to Mr. Bean

    [quote user="Mr. Bean The same truly brilliant mind that came up with COBOL? [/quote]

    Admiral Grace Hopper did come up with a pretty good language, when the only two real alternatives were Assembler and AutoCoder.

    Where were you when the page was blank?

  • SlyEcho (unregistered) in reply to Matt
    Matt:
    They call it Lambda expressions and it comes with C#. For instance, I could code a foldl expression:

    ///

    /// Wraps a function that takes two parameters of type T,V -> V, folds over an array of type T from left-to-right. /// V foldl(IEnumerable<T> array, V initR, Func<V, V, T> func)

    ///

    /// Special case /// T foldl(IEnumerable<T> array, Func<T,T,T> func)

    etc.

    Isn't this what System.Linq.Enumerable.Aggregate() does?

    http://msdn2.microsoft.com/en-us/library/system.linq.enumerable_methods.aspx

  • Mark (unregistered)

    What and idiot. What's he going to do if he wants to add firstNo to secondNo?

  • Kuba (unregistered) in reply to warmachine
    warmachine:
    I would say the sum of an array of doubles can be useful as a shorthand. But only in odd applications that happen to use it.

    C++ has std::valarray which does just that ;)

  • (cs) in reply to Fraggle My Rock
    Fraggle My Rock:
    J. Walter Weatherman:
    Jesus Christ my ass itches.

    There's a good chance you have enterobius vermicularis (AKA Pinworms). You should talk to your GP and/or visit your local pharmacy for over-the-counter medication.

    You are of course assuming that Mr Weatherman is not a dog. I'm told that many useful and popular frameworks are designed to make it simple for those of the canine persuasion to create their own web-sites. And I'm pretty sure I've seen some of the results.

    Express those glands, now!

  • (cs) in reply to Mr. Bean
    Mr. Bean:
    fraserofthenight:
    Why write something passe like "(a + b) * c)" when you could write the much more descriptive "Utils.Multiply(Utils.Add(a, b), c)". Plus, with the Utils class there's no longer any need to worry about that pesky order of operations. Truly only a brillant mind could have come up with that.

    The same truly brilliant mind that came up with COBOL?

    Register or Die. Now.

    I'm no fan of COBOL, but I'm a huge fan of Grace Hopper - http://en.wikipedia.org/wiki/Grace_Hopper.

    Not the least of her (many) achievements was to admit that COBOL was, in fact, an inferior language to Fortran (and let nobody quote Dr Johnson at me here), but it beat the opposition out because the compiler was easier and quicker to write.

    Want to take a little trip back in time to the early 1950s?

  • Ringo (unregistered)

    The real wtf is that you're not fixing it.

    Is there really no peer review of any kind where you could point out the stupidity?

  • Luk (unregistered) in reply to Matt
    Matt:
    You could always try that great function

    public static int Max(params int[] values){ return values.Max(); }

    too. In fact you could generalize it to

    public static int Max(IEnumerable<int> values){ return values.Max(); }

    or even go for a generic solution

    public static T Max(IEnumerable<T> values){ return values.Max(); }

    or then again you could realize that re-programming things that come with the language is stupid.

    It's 2.0 framework and Generic exaple is'nt goood, not all types have to have bigger and lower operators. I'm aware of 3.5 goddies and use them well in 3.5 projects, but these have to be 2.0

  • raluth (unregistered) in reply to Luk
    Luk:
    It's 2.0 framework and Generic exaple is'nt goood, not all types have to have bigger and lower operators. I'm aware of 3.5 goddies and use them well in 3.5 projects, but these have to be 2.0

    Min/Max are trivial to implement, even in 2.0 - thanks to Comparer<T>.Default which uses IComparable<T> or IComparable; Sum, Avg are trickier, but can be done - but it needs some grungy code.

    Gotta love how Matt has added a step, though ;-p

  • Cessor (unregistered) in reply to JonC

    Thank you, captain obvious!

  • no (unregistered) in reply to SeaDrive

    tbh such routines are useful for function composition. Which is anything but useless.

  • Greg van Paassen (unregistered) in reply to SeaDrive

    LoL!! The world of systems integration is yours for the taking, SeaDrive!

  • (cs)

    Do not repeat yourself - reuse existing code! Thus:

    public static double Multiply(double num1, double num2)
    {
        double[] toBeAddedUp = new double[num1];
        for (int i=0; i<num1; i++) {
            toBedAddedUp[i] = num2;
        }
        return Add(toBeAddedUp);
    }
    </pre>
    
  • Viliam Búr (unregistered) in reply to fraserofthenight
    fraserofthenight:
    Why write something passe like "(a + b) * c)" when you could write the much more descriptive "Utils.Multiply(Utils.Add(a, b), c)".
    Writing "Utils.Multiply(Utils.Add(a, b), c)" shows that they are good at object-oriented programming. Or something like this. ;-)
  • Khorne (unregistered)

    Actually... http://www.sgi.com/tech/stl/plus.html

  • btx40 (unregistered) in reply to SeaDrive
    SeaDrive:
    If you combine this re-write from Trondster:

    public static double Add(double firstNo, double secondNo) { double total = Add(new double[] {firstNo,secondNo}); return total; }

    with this rewrite from Grovsey:

    public static double Add(double[] valueArray) { double total = 0.00; foreach(double val in valueArray) { total = Add(total, val) } return total; }

    you would really be getting somewhere!

    Addition functions guaranteed to throw a

    System.StackOverflowException{/code] and fail miserably. Genius.[/sarcasm]

    BTW somebody needs to teach this guy about the [code]decimal

    type in C#.

  • Achim (unregistered)

    This makes up for very flexible, easy to maintain code.

    Suppose - for instance, that the definition of what 'add' means changes in the future. Or you find a CPU bug about adding ints. You can simple fix it in this method in one place!!!

    It is thus just as useful as these #define TEN 10 defs.

    (Why didn't I learn something useful, such as carpenter...)

Leave a comment on “They're Useful... In General”

Log In or post as a guest

Replying to comment #:

« Return to Article