Skip to main content
Commonmark migration
Source Link

#Golfing variable assignments in control flow structures using tuples

Golfing variable assignments in control flow structures using tuples

Consider you want to use a while loop, and you want to use the same thing in both the condition and the block to follow. Then, an inline assignment in a tuple would most likely help. The longer your attribute, the better! Consider this (3 bytes shorter):

func f(s:[Int]){var k=s,i=0;while(i=k.count,i>0).1{print(i,i+k[i-1]);k.removeLast();}}

over this:

func g(s:[Int]){var k=s,i=0;while k.count>0{i=k.count;print(i,i+k[i-1]);k.removeLast();}}

Notice the (i=k.count,i>0).1 part, which is quite interesting.


Inspired by one of Herman Lauenstein's answers.

#Golfing variable assignments in control flow structures using tuples

Consider you want to use a while loop, and you want to use the same thing in both the condition and the block to follow. Then, an inline assignment in a tuple would most likely help. The longer your attribute, the better! Consider this (3 bytes shorter):

func f(s:[Int]){var k=s,i=0;while(i=k.count,i>0).1{print(i,i+k[i-1]);k.removeLast();}}

over this:

func g(s:[Int]){var k=s,i=0;while k.count>0{i=k.count;print(i,i+k[i-1]);k.removeLast();}}

Notice the (i=k.count,i>0).1 part, which is quite interesting.


Inspired by one of Herman Lauenstein's answers.

Golfing variable assignments in control flow structures using tuples

Consider you want to use a while loop, and you want to use the same thing in both the condition and the block to follow. Then, an inline assignment in a tuple would most likely help. The longer your attribute, the better! Consider this (3 bytes shorter):

func f(s:[Int]){var k=s,i=0;while(i=k.count,i>0).1{print(i,i+k[i-1]);k.removeLast();}}

over this:

func g(s:[Int]){var k=s,i=0;while k.count>0{i=k.count;print(i,i+k[i-1]);k.removeLast();}}

Notice the (i=k.count,i>0).1 part, which is quite interesting.


Inspired by one of Herman Lauenstein's answers.

Source Link
Mr. Xcoder
  • 42.5k
  • 9
  • 81
  • 214

#Golfing variable assignments in control flow structures using tuples

Consider you want to use a while loop, and you want to use the same thing in both the condition and the block to follow. Then, an inline assignment in a tuple would most likely help. The longer your attribute, the better! Consider this (3 bytes shorter):

func f(s:[Int]){var k=s,i=0;while(i=k.count,i>0).1{print(i,i+k[i-1]);k.removeLast();}}

over this:

func g(s:[Int]){var k=s,i=0;while k.count>0{i=k.count;print(i,i+k[i-1]);k.removeLast();}}

Notice the (i=k.count,i>0).1 part, which is quite interesting.


Inspired by one of Herman Lauenstein's answers.