Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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