Skip to main content
replaced http://codegolf.stackexchange.com/ with https://codegolf.stackexchange.com/
Source Link

Use fold/reduce as infix foreach

We have :x as a shorthand for {x}% and or {x}* (depending on whether x is unary or binary). Unfortunately, there is no equivalent infix operator to shorten {x}/. However, very often when we do {x}/, x is actually a binary operator which repeatedly modifies the item lying underneath on the stack. If that's the case, and said item is not an array, we can save a byte by abusing fold/reduce as foreach:

5 [1 2 3 4]{-}/  e# Gives -5
5 [1 2 3 4]+:-

This works because fold always leaves the first element untouched. Unfortunately, it doesn't save a byte, when the modified element is an array, because adding it would unwrap it. However, sometimes you're lucky enoughsometimes you're lucky enough that your array already contains that element at the front, in which case reduce should be kept in mind (instead of manually removing the element before using {}/ on the remainder).

Use fold/reduce as infix foreach

We have :x as a shorthand for {x}% and or {x}* (depending on whether x is unary or binary). Unfortunately, there is no equivalent infix operator to shorten {x}/. However, very often when we do {x}/, x is actually a binary operator which repeatedly modifies the item lying underneath on the stack. If that's the case, and said item is not an array, we can save a byte by abusing fold/reduce as foreach:

5 [1 2 3 4]{-}/  e# Gives -5
5 [1 2 3 4]+:-

This works because fold always leaves the first element untouched. Unfortunately, it doesn't save a byte, when the modified element is an array, because adding it would unwrap it. However, sometimes you're lucky enough that your array already contains that element at the front, in which case reduce should be kept in mind (instead of manually removing the element before using {}/ on the remainder).

Use fold/reduce as infix foreach

We have :x as a shorthand for {x}% and or {x}* (depending on whether x is unary or binary). Unfortunately, there is no equivalent infix operator to shorten {x}/. However, very often when we do {x}/, x is actually a binary operator which repeatedly modifies the item lying underneath on the stack. If that's the case, and said item is not an array, we can save a byte by abusing fold/reduce as foreach:

5 [1 2 3 4]{-}/  e# Gives -5
5 [1 2 3 4]+:-

This works because fold always leaves the first element untouched. Unfortunately, it doesn't save a byte, when the modified element is an array, because adding it would unwrap it. However, sometimes you're lucky enough that your array already contains that element at the front, in which case reduce should be kept in mind (instead of manually removing the element before using {}/ on the remainder).

Source Link
Martin Ender
  • 197.2k
  • 67
  • 447
  • 975

Use fold/reduce as infix foreach

We have :x as a shorthand for {x}% and or {x}* (depending on whether x is unary or binary). Unfortunately, there is no equivalent infix operator to shorten {x}/. However, very often when we do {x}/, x is actually a binary operator which repeatedly modifies the item lying underneath on the stack. If that's the case, and said item is not an array, we can save a byte by abusing fold/reduce as foreach:

5 [1 2 3 4]{-}/  e# Gives -5
5 [1 2 3 4]+:-

This works because fold always leaves the first element untouched. Unfortunately, it doesn't save a byte, when the modified element is an array, because adding it would unwrap it. However, sometimes you're lucky enough that your array already contains that element at the front, in which case reduce should be kept in mind (instead of manually removing the element before using {}/ on the remainder).