Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the javascript `_|_`?

I was looking through a solution to a problem in javascript, namely parsing a string into its constituent names, operators, and brackets, when I saw this expression:

return accept(")") ? _|_ : e;

What is that _|_? Is that using node's _ feature? I've looked for documentation but not found any.

When I try using it myself, this happens:

> 5
5
> true ? _|_ : 0
ReferenceError: _ is not defined
    at eval:1:1
    at eval
    at n.<anonymous>

As a clarification, the variable _ was not defined anywhere in the code.

This was run on Node v8.1.3, but also works fine on chrome native browser JS.

Here is the code:

function tokenise(string) {
  const tokens = string.match( /[a-z]+|\(|\)|[!#$%&*+\-\/<=>@^_.,;]+/gi ) || [];
  const accept = s => s===tokens[0] && tokens.shift() ;
  const unaccept = s => s!==tokens[0] && tokens.shift() ;
  const singles = (e) => ( e = unaccept(")") ) ? [ e, ...brackets() ] : [] ;
  const brackets = (e) => accept("(") ? ( e = brackets(), accept(")") || _|_ , [ e, ...brackets() ] ) : singles() ;
  try      { let e = brackets(); return accept(")") ? _|_ : e ; }
  catch(_) { return null; }
}
like image 755
AJF Avatar asked Sep 09 '17 12:09

AJF


2 Answers

_|_ has no special meaning in JavaScript, it's just a | (bitwise OR) expression with the identifier _ on both sides of it.

That code checks the result of calling accept and, if it's truthy, returns the result of the | operation; if not it returns the value of e.

Is that using node's _ feature?

_ isn't special in Node code. In the REPL (only), it's a variable that receives the result of the most recently evaluated expression; details here.

As a clarification, the variable _ was not defined anywhere in the code.

That means that if accept(")") returns a truthy value, the code will throw a ReferenceError because _ is an undeclared identifier. If that's what the author intended, there's no need for the | operator at all, but in a now-deleted answer, georg pointed out that they may have used _|_ as an attempt to express the mathematical concept of bottom ("a computation which never completes successfully"), the symbol for which is . In any case, the author seems to use it to force an exception in an expression context (since throw is a statement; there's talk of possibly allowing it in expression contexts at some point, though).

like image 165
T.J. Crowder Avatar answered Sep 25 '22 14:09

T.J. Crowder


It take a variable _ and uses a bitwise OR | with itself.

As result, you get a 32 bit integer number.

like image 31
Nina Scholz Avatar answered Sep 26 '22 14:09

Nina Scholz