News for hackers. Stuff that matters.
Re:Elevator Saga – An elevator programming game by SeoxyS on Thursday January 22, 2015 @09:35PM (#8933124)
My attempt at a basic smart algorithm. Not so easy...

https://gist.github.com/kballenegger/e275a99d50de2ee07f97

Re:Elevator Saga – An elevator programming game by idbehold on Thursday January 22, 2015 @10:49PM (#8933301)
Why do you have conditionals written like this: if (elevator.goingUpIndicator() == true)? Why not just: if (elevator.goingUpIndicator())?
Re:Elevator Saga – An elevator programming game by _lce0 on Thursday January 22, 2015 @11:56PM (#8933472)
I believe some people really need think in term of comparations and, for those, adding a "==" symbol make sense, even if the resultant code doesn't make any sense at all!!

Also, I can never understand why people write ..

    if (foo()) {
        return true;
    } else {
        return false;
    }
.. or things like extra parens on conditionals, or weird styles like:

    return (false);

I strongly believe people write as they talk, and talk as they think; which not clearly translates linearly when what you write is code ;)
Re:Elevator Saga – An elevator programming game by SeoxyS on Friday January 23, 2015 @03:29AM (#8933918)
Responding to GP; I think it's a tiny bit clearer with `== true`, but I don't feel particularly strongly about it. I wrote this code without caring too much about style and cleanliness.

However, I don't agree that the shortest code is always the cleanest. In JS for example, I've found that wrapping every conditional body in braces even if it's a single statement helps avoid bugs and clear out ambiguity.

Also, this code:

    if (foo()) {
        return true;
    } else {
        return false;
    }
is not equivalent to

    return foo();
because foo() could return something that isn't boolean. The correct short version would be:

    return !!foo();
It's far less obvious what is going on here. That said, I'd probably go with:

    return foo() ? true : false;
Re:Elevator Saga – An elevator programming game by JoeAltmaier on Friday January 23, 2015 @12:20AM (#8933516)
Many languages are expressions (as opposed to statements) including C-like languages. I've always thought that 'return' itself was redundant. If a compound statement was defined to return the value of the final (executed) statement, then functional methods would get much simpler. And remove the need for the '?' operator for instance.

e.g.

   int foo(int x) {   bar(x,y); }
or

   x = {if (foo() > 9) true; else false; }
Re:Elevator Saga – An elevator programming game by _lce0 on Friday January 23, 2015 @12:04PM (#8935902)
I think one problem with statements-as-expression is that is not always evident which should be their result.

for instance:

    bool r = if (test()) {
      false;
    } else {
      true;
    }
what r should be set to? true or false?

I don't see the point of adding such complexity.. it's waaay more clear to be explicit

    bool r = test();
    if (r) {
      false;
    } else {
      true;
    }
or, if you intended the other way

    bool r;
    if (test()) {
      r = false;
    } else {
      r = true;
    }
I guess that's why you need to be strictly correct, or highly opinionated, to design a language.
Re:Elevator Saga – An elevator programming game by JoeAltmaier on Friday January 23, 2015 @02:06PM (#8936595)
I'm missing your point I guess. All those are equivalent? In C an assignment has the value of the RHS, so that doesn't change anything in these examples.
Re:Elevator Saga – An elevator programming game by molo on Tuesday January 27, 2015 @01:23PM (#8954695)
That is unnecessary verbosity. Try this instead:

    bool r = !test();
Re:Elevator Saga – An elevator programming game by thedufer on Friday January 23, 2015 @01:21AM (#8933642)
Have you tried coffeescript? Your examples are pretty close to valid code that does what you want.
Re:Elevator Saga – An elevator programming game by eru on Monday January 26, 2015 @11:55PM (#8951301)
In an imperative language, you still might want an optional return to jump out of the middle of some code.
Re:Elevator Saga – An elevator programming game by eru on Friday January 23, 2015 @12:29AM (#8933534)
You might like functional languages.
Re:Elevator Saga – An elevator programming game by _lce0 on Thursday January 22, 2015 @11:52PM (#8933464)
here mine: https://gist.github.com/eridal/7ce55190837801811067

side note: why are you using underscore? I wanted to try your code, but doesn't work out-of-the-box.

Re:Elevator Saga – An elevator programming game by SeoxyS on Friday January 23, 2015 @03:23AM (#8933908)
LoDash actually, not Underscore. Mostly because it's included by the game already. The code should run as-is. That said, it does occasionally get stuck. I think I have a bug with the direction indicators.