Skip to content

Instantly share code, notes, and snippets.

@barneycarroll
Created March 2, 2018 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barneycarroll/c4d38ce718e2c375244cd935031b9960 to your computer and use it in GitHub Desktop.
Save barneycarroll/c4d38ce718e2c375244cd935031b9960 to your computer and use it in GitHub Desktop.
When a nested expression is the desired return value

Written to avoid the noisome pattern of

input => {
  const reference = expression with input

  sideEffects(reference)

  return reference 
}

…which can instead be expressed as

give(me =>
  input => {
    sideEffects(
      me(expression with input)
    )
  }
)

For functional scenarios where a non-yielding side effect expressions comprises the desired return value.

The value lies in avoiding 2 keywords and temporary assignment with 2 repetitions of 'reference' in preference for 2 generic references with one repetition.

The give(me => /**/) pattern can be translated as, 'the return value for the following function is a nested expression (this one)'.

Straw man:

const doubles = values.map(
  give(me =>
    value => {
      alert(
        me(2 * value)
      )
    }
  )
)
const give = fn => {
let output
return function(){
fn(
input => output = input
).apply(
this,
arguments,
)
return output
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment