create account

Share your favorite code snippet by grider123

View this thread on: hive.blogpeakd.comecency.com
· @grider123 ·
Share your favorite code snippet
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.
👍  , ,
properties (23)
authorgrider123
permlinkshare-your-favorite-code-snippet
categoryprogramming
json_metadata{"tags":["programming","code","snippet","share"],"app":"steemit/0.1","format":"markdown"}
created2017-06-10 11:18:06
last_update2017-06-10 11:18:06
depth0
children4
last_payout2017-06-17 11:18:06
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length363
author_reputation107,965,352,035,530
root_title"Share your favorite code snippet"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id4,525,544
net_rshares1,002,495,818
author_curate_reward""
vote details (3)
@adnanrahic ·
```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.
👍  
properties (23)
authoradnanrahic
permlinkre-grider123-share-your-favorite-code-snippet-20170610t221941713z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-06-10 22:19:42
last_update2017-06-10 22:19:42
depth1
children1
last_payout2017-06-17 22:19:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length136
author_reputation1,605,311,705,685
root_title"Share your favorite code snippet"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id4,557,725
net_rshares75,557,058
author_curate_reward""
vote details (1)
@grider123 ·
Good example! Ty
properties (22)
authorgrider123
permlinkre-adnanrahic-re-grider123-share-your-favorite-code-snippet-20170611t105642208z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-06-11 10:56:45
last_update2017-06-11 10:56:45
depth2
children0
last_payout2017-06-18 10:56:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length16
author_reputation107,965,352,035,530
root_title"Share your favorite code snippet"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id4,585,806
net_rshares0
@xerdo ·
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);
```
👍  
properties (23)
authorxerdo
permlinkre-grider123-share-your-favorite-code-snippet-20170612t085328298z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-06-12 08:53:27
last_update2017-06-12 08:53:27
depth1
children1
last_payout2017-06-19 08:53:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length340
author_reputation82,257,647,130
root_title"Share your favorite code snippet"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id4,646,338
net_rshares152,228,300
author_curate_reward""
vote details (1)
@grider123 ·
Cool XOR swap. Didn't know this trick! Really neat.
properties (22)
authorgrider123
permlinkre-xerdo-re-grider123-share-your-favorite-code-snippet-20170612t172623211z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-06-12 17:26:24
last_update2017-06-12 17:26:24
depth2
children0
last_payout2017-06-19 17:26:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length51
author_reputation107,965,352,035,530
root_title"Share your favorite code snippet"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id4,670,420
net_rshares0