I'm interested in your favorite code snippet. It can be in any language. Please share it in the comments below. Mine is a way to swap to variables without using a temporary variable: var a = 5; var b = 3; var a = b + a; var b = a - b; var a = a - b; After that a is equal to 3 and b is equal to 5. We have swapped the variables without using a third variable.
author | grider123 |
---|---|
permlink | share-your-favorite-code-snippet |
category | programming |
json_metadata | {"tags":["programming","code","snippet","share"],"app":"steemit/0.1","format":"markdown"} |
created | 2017-06-10 11:18:06 |
last_update | 2017-06-10 11:18:06 |
depth | 0 |
children | 4 |
last_payout | 2017-06-17 11:18:06 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 363 |
author_reputation | 107,965,352,035,530 |
root_title | "Share your favorite code snippet" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 4,525,544 |
net_rshares | 1,002,495,818 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
bedroomshaman | 0 | 195,301,393 | 10% | ||
grider123 | 0 | 50,764,697 | 100% | ||
javifo | 0 | 756,429,728 | 100% |
```var port = process.env.PORT || 8000``` It's just so fancy using the pipes to set a default value when the initial value is undefined.
author | adnanrahic |
---|---|
permlink | re-grider123-share-your-favorite-code-snippet-20170610t221941713z |
category | programming |
json_metadata | {"tags":["programming"],"app":"steemit/0.1"} |
created | 2017-06-10 22:19:42 |
last_update | 2017-06-10 22:19:42 |
depth | 1 |
children | 1 |
last_payout | 2017-06-17 22:19:42 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 136 |
author_reputation | 1,605,311,705,685 |
root_title | "Share your favorite code snippet" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 4,557,725 |
net_rshares | 75,557,058 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
grider123 | 0 | 75,557,058 | 100% |
Good example! Ty
author | grider123 |
---|---|
permlink | re-adnanrahic-re-grider123-share-your-favorite-code-snippet-20170611t105642208z |
category | programming |
json_metadata | {"tags":["programming"],"app":"steemit/0.1"} |
created | 2017-06-11 10:56:45 |
last_update | 2017-06-11 10:56:45 |
depth | 2 |
children | 0 |
last_payout | 2017-06-18 10:56:45 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 16 |
author_reputation | 107,965,352,035,530 |
root_title | "Share your favorite code snippet" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 4,585,806 |
net_rshares | 0 |
Alternate way of swapping two vars without using a third: ``` a ^= b; b ^= a; a ^= b; ``` I'm also a fan of Java 8 streams: ``` public class MyObj { int someValue; // ... } List<MyObj> someList; // already defined someList.stream() .map(myObj -> myObj.someValue) .filter(value -> value > 0) .findFirst().orElse(null); ```
author | xerdo |
---|---|
permlink | re-grider123-share-your-favorite-code-snippet-20170612t085328298z |
category | programming |
json_metadata | {"tags":["programming"],"app":"steemit/0.1"} |
created | 2017-06-12 08:53:27 |
last_update | 2017-06-12 08:53:27 |
depth | 1 |
children | 1 |
last_payout | 2017-06-19 08:53:27 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 340 |
author_reputation | 82,257,647,130 |
root_title | "Share your favorite code snippet" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 4,646,338 |
net_rshares | 152,228,300 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
grider123 | 0 | 152,228,300 | 100% |
Cool XOR swap. Didn't know this trick! Really neat.
author | grider123 |
---|---|
permlink | re-xerdo-re-grider123-share-your-favorite-code-snippet-20170612t172623211z |
category | programming |
json_metadata | {"tags":["programming"],"app":"steemit/0.1"} |
created | 2017-06-12 17:26:24 |
last_update | 2017-06-12 17:26:24 |
depth | 2 |
children | 0 |
last_payout | 2017-06-19 17:26:24 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 51 |
author_reputation | 107,965,352,035,530 |
root_title | "Share your favorite code snippet" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 4,670,420 |
net_rshares | 0 |