Flipping variable
1
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?
2
Name:
Anonymous
2010-11-05 17:03
foo = foo ? 0 : 44;
or
foo = !foo * 44;
3
Name:
Anonymous
2010-11-05 17:19
>>2
ternary is bad and you should feel bad for suggesting it.
4
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.
5
Name:
Anonymous
2010-11-05 18:05
foo ^= 0x2C;
6
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.
7
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.
8
Name:
Anonymous
2010-11-05 19:50
Whatever
>>1 is doing is probably bad programming practice in the first place.
9
Name:
Anonymous
2010-11-05 21:35
>>8
In the end, all food causes cancer, and all programming practices are bad programming practices.
10
Name:
Anonymous
2010-11-05 21:38
foo = 44 if foo is 0 else 0
11
Name:
Anonymous
2010-11-05 22:48
v1 ? s1 : v2 ? s2 : v3 ? s3 : v4;
ternary considered harmful
12
Name:
Anonymous
2010-11-05 23:08
>>11
Delicious Code Golf.
ternary considered harmful
Very yes.
13
Name:
Anonymous
2010-11-06 0:34
What you want here is the ternary assignment operator.
foo ?= 0 : 44;
14
Name:
Anonymous
2010-11-06 0:42
>>13
I could do that in Scheme:
(set! foo (if (= foo 0) 44 0))
15
Name:
Anonymous
2010-11-06 1:22
>>13 Because you're too lazy to type what
>>10- san did?
16
Name:
Anonymous
2010-11-06 3:01
>>2
foo = !foo * 44;
Not guaranteed to work.
17
Name:
Anonymous
2010-11-06 6:23
>>5
Would not work. consider foo = 1 in op's code and yours.
18
Name:
Anonymous
2010-11-06 6:24
19
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.
20
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.
21
Name:
Anonymous
2010-11-06 6:57
>>19
However, that will definitely be optimised out.
22
Name:
Anonymous
2010-11-06 8:38
inb4 Scheme macro that generalizes for any values of foo and 44
23
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.
24
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))
)
26
Name:
!fAVMdKdIRo
2011-04-18 16:54
butthurt
27
Name:
anus !an/us
2013-03-15 19:33
faggots