75

I recently came upon the following code:

IntPredicate neg = x -> x <- x;

What is this, some sort of reverse double lambda?

fredoverflow
  • 256,549
  • 94
  • 388
  • 662
  • 50
    My guess: Lambda `(x) -> (x < -x)` – tobias_k Jul 24 '14 at 15:47
  • 1
    I am tempted to vote to close for "simple typographical error." ;) – Mark Rotteveel Jul 24 '14 at 15:49
  • 13
    That's an example what parentheses are sometimes good for. – MicSim Jul 24 '14 at 15:50
  • 13
    Maybe it's related to the [--> operator](http://stackoverflow.com/questions/1642028/what-is-the-name-of-this-operator) – Alex Jul 24 '14 at 15:52
  • 5
    @rightfold I disagree. The "duplicate" question is about C++, not Java, and this question was asked years before Java introduced lambda expressions. – Oliver Spryn Jul 24 '14 at 17:17
  • 1
    BTW, `x < -x` is broken for testing if `x` is negative. It will fail in the case where `x == Integer.MIN_VALUE`. – David Conrad Jul 24 '14 at 17:23
  • @spryno724 Uh, I know. Yet it’s pretty much exactly the same problem: obfuscated operators. –  Jul 24 '14 at 18:03
  • 1
    Reverse double lambda, man does that sound scary – Dunno Jul 24 '14 at 20:21
  • 2
    @rightfold They're both about obfuscated operators, but that doesn't mean that reading the answer to the other question would give you the answer to this one. I find it hard to consider it a duplicate for that reason. – Chris Hayes Jul 24 '14 at 21:16
  • 1
    @ChrisHayes Reading the answer to the other question will immediately ring a bell and the reader would conclude that the latter `-` here is in fact the unary negation operator. –  Jul 24 '14 at 21:17
  • 4
    @rightfold That's hardly guaranteed. You or I might, but someone else who's going through a hard day might still have trouble parsing what's going on. In any case, since it's not an *exact* duplicate it becomes somewhat subjective, and the lack of close votes seems to indicate that most people want the question to stay. – Chris Hayes Jul 24 '14 at 21:21
  • 1
    Really? You really came across this code in an actual program written for some practical purpose? – Raedwald Jul 25 '14 at 00:07
  • 2
    @righfold duplicate answer != duplicate question... – assylias Jul 25 '14 at 17:52
  • Oh man... I hate developers who write damn difficult to understand code like `x -> x <- x` or [-->](http://stackoverflow.com/questions/1642028/what-is-the-name-of-this-operator) or something like that that looks _cool ASCII art_. – ST3 Jul 28 '14 at 14:05

1 Answers1

91

There is no -> <- operator. That first -> is just lambda syntax, as introduced in Java 8, and that second <- is a misleading concatenation of 'smaller than' < and 'unary minus' -.

You can read it as IntPredicate neg = (x) -> (x < (-x));, i.e. it tests whether x is smaller than -x, which is the case for all (well, most) negative numbers, hence the name neg.

IntPredicate neg = x -> x <- x;
System.out.println(neg.test(4));   // false
System.out.println(neg.test(0));   // false
System.out.println(neg.test(-4));  // true

Just for completeness: This test is not only (intentionally?) hard to understand, but -- as pointed out in the comments -- it also fails for Integer.MIN_VALUE (which is ==-Integer.MIN_VALUE). Instead, you should probably just use the much simpler IntPredicate neg = x -> (x < 0);.

Community
  • 1
  • 1
tobias_k
  • 81,265
  • 12
  • 120
  • 179