[Functional Programming] Make a flip and reverse functions

function f(...args) {
  return args;
}


function flip(fn) {
  return function flipped (arg1, arg2, ...args) {
    return fn(arg2, arg1, ...args)
  }
}

let z = flip(f)
console.log(z(1,2,3)) // [2,1,3]

function reverse(fn) {
  return function reversed(...args) {
    return fn(...args.reverse())
  }
}
let r = reverse(f)
console.log(r(1,2,3)) // [3,2,1]
posted @ 2022-07-31 16:45  Zhentiw  阅读(19)  评论(0编辑  收藏  举报