Skip to content

Instantly share code, notes, and snippets.

@WaldoJeffers
Last active October 11, 2018 10:17
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 WaldoJeffers/ff46fe25f3970e3ecc9c9401e5ce4f18 to your computer and use it in GitHub Desktop.
Save WaldoJeffers/ff46fe25f3970e3ecc9c9401e5ce4f18 to your computer and use it in GitHub Desktop.
// SUBJECT
// Write a function withParamTransform which,
// for any function f,
// for any parameter transformation such as (a, b, c) => (a, c, b) (inverting 2 parameters)
// satisfies the following condition:
withParamTransform(f)[(a, b, c) => (a, c, b)](1, 2, 3) === f(1, 3, 2)
// A more realistic example
const minus = (a, b) => a - b
minus(5, 2) === 3 // true
withParamTransform(minus)[(a, b) => (b, a)](2, 5) === 3 // true
// Example use case
import { append } from 'ramda'
// append(3, [1, 2]) == [1, 2, 3]
const transformedAppend = withParamTransform(append)
const arr = ['you', 'we', 'they']
arr.reduce(transformedAppend[(acc, val) => (val + ' are', acc)], [])
// ['you are', 'we are', ' they are']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment