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

Pages: 1-

Flipping variable

Name: Anonymous 2010-11-05 16:50


if(foo == 0)
    foo = 44;
else
    foo = 0;



How do I change that into a single line that does the trick using arithmetic?

Name: Anonymous 2010-11-05 17:03

foo = foo ? 0 : 44;
or
foo = !foo * 44;

Name: Anonymous 2010-11-05 17:19

>>2
ternary is bad and you should feel bad for suggesting it.

Name: Anonymous 2010-11-05 17:23

>>3
Expressions are better than statements, and you should feel bad for not knowing that.

But using ternary/if requires a branch unnecessarily. The second option is neater.

Name: Anonymous 2010-11-05 18:05

foo ^= 0x2C;

Name: Anonymous 2010-11-05 18:50

>>4
Readable code is better than difficult to read ternary expressions.
You should feel bad for suggesting that expressions are somehow better than statements in any concrete and practical way.

Name: Anonymous 2010-11-05 19:24

>>6
Anyone who says that ternary expressions are ``difficult to read'' should feel bad, and I think I have bitch tits.

Name: Anonymous 2010-11-05 19:50

Whatever >>1 is doing is probably bad programming practice in the first place.

Name: Anonymous 2010-11-05 21:35

>>8
In the end, all food causes cancer, and all programming practices are bad programming practices.

Name: Anonymous 2010-11-05 21:38

foo = 44 if foo is 0 else 0

Name: Anonymous 2010-11-05 22:48

v1 ? s1 : v2 ? s2 : v3 ? s3 : v4;

ternary considered harmful

Name: Anonymous 2010-11-05 23:08

>>11
Delicious Code Golf.
ternary considered harmful
Very yes.

Name: Anonymous 2010-11-06 0:34

What you want here is the ternary assignment operator.
foo ?= 0 : 44;

Name: Anonymous 2010-11-06 0:42

>>13
I could do that in Scheme:
(set! foo (if (= foo 0) 44 0))

Name: Anonymous 2010-11-06 1:22

>>13 Because you're too lazy to type what >>10-san did?

Name: Anonymous 2010-11-06 3:01

>>2
foo = !foo * 44;
Not guaranteed to work.

Name: Anonymous 2010-11-06 6:23

>>5
Would not work. consider foo = 1  in op's code and yours.

Name: Anonymous 2010-11-06 6:24

>>16
Explain yourself.

Name: Anonymous 2010-11-06 6:31

>>14
>I could do that in Scheme:

No. You can't, ``retard''
>(set! foo (if (= foo 0) 44 0))

is equivalent to foo = foo == 0 ? 44 : 0 (>>2), not
foo ?= 0 : 44; (>>13). Count how many times variable name "foo" is encountered in your expression, >>2-kun's expression and how many times in >>13-chan's expression.

Name: Anonymous 2010-11-06 6:56

Now extend it to
foo = foo == a ? b : c;
or even any relational operator in place of ==

It's quite easy.

Name: Anonymous 2010-11-06 6:57

>>19
However, that will definitely be optimised out.

Name: Anonymous 2010-11-06 8:38

inb4 Scheme macro that generalizes for any values of foo and 44

Name: Anonymous 2010-11-06 10:57

>>18
>>16-tan forgot to read section 6.5.3.3.5 of the C Standard today and speaks out of her ass. !x * 44 is guaranteed to work.

Name: Anonymous 2010-11-06 17:23

(module toggle-example ()

(import chicken scheme)
(use extras miscmacros srfi-69)

(define (toggle left right)
  (lambda (x)
    (select x
      ((left) right)
      ((right) left)
      (else (error "toggle" left right x)))))

(define s (toggle 44 0))

(define x 44)
(printf "x before: ~a~n" x)
(modify! x s)
(printf "x after: ~a~n" x)

(define v (vector 0 1 2 3))
(printf "v before: ~a~n" v)
(modify! (vector-ref v 0) s)
(printf "v after: ~a~n" v)

(define l (list 0 1))
(printf "l before: ~a~n" l)
(modify! (car l) s)
(printf "l after: ~a~n" l)

(define h (alist->hash-table '((key . 0))))
(printf "h before: ~a~n" (hash-table->alist h))
(modify! (hash-table-ref h 'key) s)
(printf "h after: ~a~n" (hash-table->alist h))

)

Name: !fAVMdKdIRo 2011-04-18 16:54

butthurt

Name: anus !an/us 2013-03-15 19:33

faggots

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