Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-8081-

Ternary Operator

Name: Anonymous 2010-01-08 8:07

ALL HAIL!

Name: Anonymous 2010-01-08 8:16

pig disgusting.
only used by amateurs who're trying to be fancy, except on rare occasions.

Name: Anonymous 2010-01-08 8:24

>>2
Butthurt fag, you mad?

Name: Anonymous 2010-01-08 8:28

>>3
back to /b/, please.

Name: Anonymous 2010-01-08 9:21

>>2
Yeah because ?: is sooooo fancy amirite

Name: Anonymous 2010-01-08 9:24

Name: Anonymous 2010-01-08 9:32

It should have been the default behaviour of the if statement. There should have never been a need to make a ternary operator. There should be no real statements, almost everything should be an expression. if (a) b c should itself return the value. A value which you could further use. If the value is unused, there is no harm, the compiler just frees those resources up. Languages which knew how to do it right, just had almost everything returning a value(think Lisp, ML, but there are many others that do it right), and then you could wire the outputs directly. While I do use the ternary operator in C less than if for aesthetic reasons, even if it has the functionality if should have always had.  Its syntax looks uglier than the if statement. Default IF behaviour impedes functional dataflow.

Name: Anonymous 2010-01-08 10:18

>>5
What's "?:"? We are talking about return 1 if n < 3 else fibs(n-1) + fibs(n-2)!

Name: Anonymous 2010-01-08 10:48

FIBS MY FACT

Name: Anonymous 2010-01-08 11:15

>>8
The Tenary operator can follow the structure:
variable = conditional ? A : B

which is virtually the same as:

if(conditional)
{
   variable = A
}
else
{
   variable = B
}

Name: Anonymous 2010-01-08 12:25

>>7
GTFO, this isn't Haskell. Go back to your academic ivory tower and let real programmers get some work done.

Name: Anonymous 2010-01-08 12:48

>>11
Hi, been a long time lurker on this board.. but I feel I have to speak out at this moment.

Never on the 4Chan programming board have I seen someone be so horrible and with no provocation, utterly appalling.

That was uncalled for and just plain rude.

Name: Anonymous 2010-01-08 12:51

>>11
I don't write Haskell, and I do plenty of work for real, sometimes dirty work myself. I'm not going to argue with you, as you have clearly not programmed in a language where almost everything is an expression.

Name: Anonymous 2010-01-08 13:17

>>11
LOL!!!!!!!!!!!

Name: Anonymous 2010-01-08 13:33

The ternary operator has its uses, that's why it was built into C and kept in Java. Only to be used it the equivalent if statement is less elegant,and never, ever nest them.

Name: Anonymous 2010-01-08 14:03

and never, ever nest them.
( ≖‿≖)

Name: Anonymous 2010-01-08 15:06

>>15
Nice dogmatic view, Jedi scum.

Name: Anonymous 2010-01-08 16:41

I think this thread needs a program written entirely in nested ternary operators

Name: Anonymous 2010-01-08 17:23

>>18
Don't say his name three times! FrozenVoid will appear.

Name: Anonymous 2010-01-08 17:25

>>15
Only to be used it the equivalent if statement is less elegant
In other words, use it whenever possible.

Name: Anonymous 2010-01-08 17:52

>>10
>virtually the same
Sometimes it is but sometimes the ternary operator can cost you a temporary variable or causes strange conversions.
http://dis.4chan.org/read/prog/1239221504

Name: Anonymous 2010-01-08 18:51


echo 'an' . ( ! $plural ? 'us' : 'ii');



echo 'an';
if( ! $plural)
{
    echo 'us';
}
else
{
    echo 'ii';
}

Name: Anonymous 2010-01-08 18:54

>>22
The second is way, way clearer. Learn to program.

Name: Anonymous 2010-01-08 19:00


FROZENVOID QUALITY!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
FILE *f;
char s[512];
int x(int n, char *m) {
  !strlen(m) ? exit(n) : fprintf(stderr,"%s\n",m); exit(n);
}
int r(void) {
  fgets(s,sizeof(s),f) ? printf("%s",s) : x(0,""); r();
}
int main(int argc, char **argv) {
  argv[1]?(f=fopen(argv[1],"r"))?r():x(1,"fopen() failed"):x(1,"usage: tcat FILE");
}

Name: Anonymous 2010-01-08 19:05

>>22
( ! $
what the fuck is this shit

Name: Anonymous 2010-01-08 19:08

>>25
i assume its some kind of toy language

Name: Anonymous 2010-01-08 19:10

>>24
Lacks #include "void.h"

Name: Anonymous 2010-01-08 19:29

>>23
How can you seriously make the argument that a program requiring you to decode nine lines of code is clearer than a single short line of code that replaces repeated function calls with a few constants? Either this is some kind of knee-jerk reaction to the use of the conditional operator, or you just need to learn to program. Either way, IHBT.

Name: Anonymous 2010-01-08 21:10

>>27
Lacks //handles all the common functions,#defines,#ifdefs and #includes

Name: Anonymous 2010-01-08 21:15

>>29
Lacks my $anus

Name: Anonymous 2010-01-08 22:01

>>28
One is more abstract and has a higher information density. The other is spread out more reducing the cognition required to interpret it.

Name: Anonymous 2010-01-08 22:13

>>7
I agree.  I actually created a better approach to the if ... else ... block statements in C.  It makes more sense logically, and is much more functional in flow.

flag = cond ? 1 : 0;
for (i = flag; i == 1; i++)
{
   // if condition logic...
}
for (i = flag; i == 0; i++)
{
   // else condition logic...
}

This approach is superior because it doesn't divert the logic flow like if else blocks, and only uses the ternary operator.  The loops make a lot more sense then conditional code, because they basically say "run it for as many times as it is true or false".

Name: Anonymous 2010-01-08 22:16

>>31
That's patently false. The information density is precisely the same. Both blocks of code complete the same task. No matter how you quantify "density," there is another definition that will just as certainly show that pro to be a con; there is no clear winner because the two blocks are functionally equivalent.

As far as "spreading out," the ternary operation could also be written

echo 'an';
!$plural ?
    echo 'us':
    echo 'ii';

This is a matter of taste. Some people prefer

if(!$plural) {
    echo 'us';
} else {
    echo 'ii';
}

To each their own.

The only reason you find the ternary operator as written to require more "cognition" is because you're not used to seeing it. if/else conditionals are encountered much more frequently because it's more flexible. But for matters of simple if then else one liners, it makes sense to use a simpler notation. Just as we use contractions instead of writing do not, could not, should not every time. These are common problems that benefit from simpler solutions.

Name: Anonymous 2010-01-08 23:50

>>31
One has more room to hide mistakes or dirty tricks, making it necessary to carefully read the whole thing, while there's nothing unexpected that could happen with a variable name and two constants. The situation is even worse when the if statements in question set a variable: anything could happen between the if statements and that variable's use. Underusing the ternary operator is bad style for the same reason that overusing goto is bad style.

Name: Anonymous 2010-01-09 0:10

>>34
advocating imperative style
comparing functional style to goto usage


...however it's true that lambda is the ultimate goto.

Name: Anonymous 2010-01-09 1:08

>>35
I'm not advocating imperative style... and I'm not comparing functional style to goto use.

Name: Anonymous 2010-01-09 15:03

Bumping the only sensemaking thread on /prog/

Name: Anonymous 2010-01-09 16:25

DOUBLE BUMP for great justice!!!

Name: Anonymous 2010-01-09 17:52

theres an emplentation of ternatry operation in scheme?

Name: Kao-Moji 2010-01-09 18:01

About two or three days ago someone started a thread about a website called javabat.com where a user could solve small Java exercises in order to learn the language.

I have decided to swing on some of these exercises with nested ternary operations and finals.

public final boolean posNeg(final int a, final int b, final boolean negative) {
 return negative ? a < 0 && b < 0 : a < 0 && b > 0 || a > 0 && b < 0;
}


public final int strCount(final String str, final String sub) {
 return str.length() == 0 ? 0 : str.startsWith(sub) ? 1 + strCount(str.substring(sub.length()),sub) : strCount(str.substring(1),sub);
}


public final int count11(final String str) {
 return str.length() == 0 ? 0 : str.startsWith("11") ? 1 + count11(str.substring(2)) : count11(str.substring(1));
}


public final int countAbc(final String str) {
 return str.length() < 3 ? 0 : str.startsWith("abc") || str.startsWith("aba") ? 1 + countAbc(str.substring(1)) : countAbc(str.substring(1));
}


public final int countX(final String str) {
 return (str.length() == 0) ? 0 : (str.startsWith("x")) ? 1 + countX(str.substring(1)) : countX(str.substring(1));
}


public final int redTicket(final int a, final int b, final int c) {
 return a + b + c == 6 ? 10 : a == b && a == c && b == c ? 5 : a != b && c != a ? 1 : 0;
}


public final int blackjack(int a, int b) {
 if (b > a) { b ^= a; a ^= b; b ^= a; }
 return a > 21 ? b > 21 ? 0 : b : a;
}

Name: Kao-Moji 2010-01-09 18:09

public final int luckySum(final int a,final int b,final int c) {
 return a == 13 ? 0 : b == 13 ? a : c == 13 ? a + b : a + b + c;
}


public final int intMax(final int a, final int b,final int c) {
 return a > b ? a > c ? a : c : b > c ? b : c;
}


public final int max1020(int a,int b) {
 if (b > a) { b ^= a; a ^= b; b ^= a; }
 return inRange(a) ? a : inRange(b) ? b : 0;
}

private final boolean inRange(final int n) {
 return n >= 10 && n <= 20;
}


public final String endUp(final String str) {
  int bounds = str.length() - 3;
  return str.length() < 4 ? str.toUpperCase() : str.substring(0,bounds) + str.substring(bounds).toUpperCase();
}


public final int caughtSpeeding(int speed, final boolean isBirthday) {
 if (isBirthday) speed -= 5;
 return speed <= 60 ? 0 : speed >= 61 && speed <= 80 ? 1 : 2;
}


public final int dateFashion(final int you, final int date) {
 return you <= 2 || date <= 2 ? 0 : you >= 8 || date >= 8 ? 2 : 1;
}

Name: Anonymous 2010-01-09 21:45

>>40,41
If you indented those in the right way, they look almost like Lisp without the parens.

Name: Anonymous 2010-01-10 0:38

>>42
Lisp without the parens.
You mean we've finally fixed Lisp?

Name: Anonymous 2010-01-10 0:57

final final final final final final final final final final final final public final public public final public final final final final final final public final final final final final final final final final final final final final public final public public final public final final final final final final public final final final final final final final final final final final final final public final public public final public  final final public final final final final final final final final final final final final final public final public

Name: Anonymous 2010-01-10 1:08

>>44
final define final fib x public
  final if final < x 1 public
    1
    final + final fib final - x 1 public public final fib final - x 2 public public public public public

Name: Anonymous 2010-01-10 1:24

final is the single worst thing in Java if everything else is rendered "tolerable."

Name: Anonymous 2010-01-10 2:21

http://www.ibm.com/developerworks/java/library/j-jtp1029.html

And why does every buio enterprise /buio compsci website have to look like this? haven't they ever heard of leading? i want to gouge my eyes out

Name: Anonymous 2010-01-10 2:43

>>47
BUIO QUALITY

Name: Anonymous 2010-01-12 6:00

IF THEN ELSE.

Name: Anonymous 2010-01-12 9:51

I strongly prefer "boiu" because it sounds good in my head.

Name: Anonymous 2010-01-12 12:06

>>47
Performance enhancement is almost always a bad reason to compromise good object-oriented design principles
This is what ENTERPRISE programmers believe.

Also,

buio m (f buia, m plural bui, f plural buie)

1. dark (contrary of luminous)

Name: Anonymous 2010-01-12 12:27

Dark like Edward???

Name: Anonymous 2010-01-12 12:42

>>50
I prefer \\ buio// because it sounds like a spell from the Harry Potter series of books and film

Name: Anonymous 2010-01-12 12:58

Everyone knows ``boui'' is the standard.

Name: Anonymous 2010-01-12 13:08

This is ridiculous.  ´´biuo`` is the only sensible scheme.

Name: Anonymous 2010-01-12 15:01

Obviously only 66buoi99 represents BEST PRACTICES.

Name: Anonymous 2010-01-12 15:13

Fags don't know how to use [biou].

Name: Anonymous 2010-01-12 16:11

I am ashamed and appalled, /prog/, that you would be getting your information from anywhere other than the the source. Read your BB&C. The standard is ubio.

Name: Anonymous 2010-01-12 16:14

I prefer ❝uibo

Name: Anonymous 2010-01-12 16:39

>>59
I ❝lol'd❞

Name: Anonymous 2010-01-14 11:13

Sometimes it is but sometimes the ternary operator can cost you a temporary variable or causes strange conversions.

Not JAVA.

Name: Anonymous 2010-03-21 23:28

all the solutions are online.

Name: Anonymous 2010-03-22 16:01

It should technically be obui, as no respectable HTML programmer would put a span inside a formatting tag, also b is always the first for some reason. But buio is fun to say in your head and is easiest to type on a AZERTY keyboard.

Name: Anonymous 2010-03-22 16:19

>>63
Actually, the obui and buio standards are incompatible, as you can see. For a truly enterprise format, even the strictest HTML Programmer has but no choice!

Name: Anonymous 2010-03-22 17:52

>>64
Oh, of course! Silly me. But it is still strange how the u and o parts are not italicised by i, isn't it! Observe the parallelity of the lines in the following:
biuo
buoi
Astounding!!!

Name: Anonymous 2010-03-23 1:43

>>63
ブイオ

Name: Anonymous 2010-03-23 2:21

You are now realizing that ternary sounds like turnkey.

Name: Anonymous 2010-03-23 5:01

>>67
ENTERPRISE TERNARY SOLUTIONS

Name: Anonymous 2010-03-23 5:11

In fact, the level of 1337ness is proportional to amount of ternary operators per 1Kb of code.

Name: Anonymous 2010-03-23 5:13

>>69
Mine is 0.5 ternaries per Kb ;_;

Name: Anonymous 2010-03-23 5:42

>>69
I have two in one 47 line function!

Name: Anonymous 2010-04-20 14:59

predicate && true_expr || false_expr

Name: Anonymous 2010-04-20 15:18

>>72
split-p soup?

Name: Anonymous 2010-04-20 16:56

>>72
Invalid Boolean Expression.

Name: Anonymous 2010-04-20 17:36

>>74
Since when? It works for me.

Name: Anonymous 2010-04-20 17:43

>>75
Logical AND and Logical OR have the same precedence in Boolean Logic, therefore that statement is technically invalid.

Name: Anonymous 2010-04-20 19:41

>>76
Fully depends on what langauge you're using

Name: Anonymous 2010-04-21 3:59

Ternary is great until you reach some one-liner made of v:?b?:(3*d)?:c:((f-0.2)/2)?e?d:(c>a)?a

Name: Anonymous 2010-04-21 4:02

>>78
It's great until it's great?

Name: Anonymous 2010-04-21 4:06

>>79
Until?Great:NotGreat

Name: Anonymous 2010-04-21 8:59

>>78
Invalid!

Name: Anonymous 2010-04-21 9:02

>>72,75
false_expr would also be evaluated if true_expr evaluated as false. But it works for stuff like print (debug && "Couldn't desecrate the mangina on line 45!" || "Something bad happened.")

Name: Anonymous 2010-04-21 9:19

>>82
Perl has a lovely way of solving that. You could also just use nesting.

As much as duplication of facilities is a tiresome path (see Perl), I really think S-expressions should show up in languages that don't strictly need them. The convenience of the ternary op could be unified with regular if/else blocks if done in S-expression form.

Name: Anonymous 2010-04-21 14:46

>>83
(See Perl && Perl or Perl)

Name: Anonymous 2010-04-23 19:07

w32BumpThread(wThisThread, W_TOP|W_SYNCHRONOUS, NULL, NULL, NULL)

Name: Anonymous 2010-04-24 5:43

s/\)/,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,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL\)/

Name: Anonymous 2010-07-09 6:01

I wanted to do something like foo = 4 (bar ? + : -) 2; and it doesn't work. Is there any clever way of making it work without putting the whole addition and subtraction inside the operator?

Name: Anonymous 2010-07-09 6:40

>>87
no

Name: Anonymous 2010-07-09 6:47

>>87
No, learn what expressions are

Name: Anonymous 2010-07-09 6:58

>>87
This isn't Haskell.

Name: Anonymous 2010-07-09 7:05

foo=4+bar?2:-2;

Name: Anonymous 2010-07-09 7:07

>>87
foo = 4 + (bar ? 2 : -2);
Or perhaps (faggot mode)
foo = 2 + (!bar) << 2;

Name: Anonymous 2010-07-09 7:09

            Func<int, int, int> add = (x, y) => x + y;
            Func<int, int, int> substract = (x, y) => x - y;
            var bar = true;
            var foo = (bar ? add : substract).Invoke(4, 2);

Name: Anonymous 2010-07-09 7:31

>>92
foo = 2 + (!bar) << 2;
I like it how you posted a solution that applies only to the particular case of addition/subtraction even though they were an obvious example.

Name: Anonymous 2010-07-09 7:54

>>94
Whatever you meant when you said that, it's easy to simplify whatever similar mathematical expression into that form.

Name: Anonymous 2010-07-09 11:46

opium dreams

Name: Anonymous 2010-07-09 11:51

people who `optimize" their programs into a nests of ternary expressions, don't know 'bout CPU Branch Predictors and penalty for not guessing the correct jump.
In fact most optimized program is one with least branches(jumps).

Name: Anonymous 2010-07-09 14:16

>>97
Solution: use conditionals that reduce to branchless arithmetic.

Name: Anonymous 2010-07-09 14:19

>>98
How do you mean?

Name: Anonymous 2010-07-09 14:21

>>98
Excuse me sir but, what the fuck are you talking about?

Name: Anonymous 2010-07-09 14:21

>>99
goto

Name: Anonymous 2010-07-09 14:36

>>99-101
Here's an example from recent nonsense:
http://dis.4chan.org/read/prog/1203076617

What's the first thing that comes to mind when you see that giant switch?

goto
Technically it fits the description, but no. Just no.

Name: Anonymous 2010-07-09 15:16

>>97
Or maybe our branch predictors don't suck.

Name: Anonymous 2010-07-10 0:09

>>103
There's no such thing as branch prediction that doesn't suck.

Name: Anonymous 2011-02-03 0:09


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