如何为monad实现通用提升功能?

时间:2019-03-05 20:16:34

标签: javascript functional-programming monads lifting

我有很多了解Arity的电梯功能:

const chain = f => xs =>
  xs.reduce((acc, x) => acc.concat(f(x)), []);

const of = x => [x];

const add = (x, y) => x + y;

const liftM2 = (chain, of) => f => m1 => m2 =>
  chain(x => chain(y => of(f(x, y))) (m2)) (m1);

console.log(liftM2(chain, of) (add) ([2]) ([3])); // [5]

是否可以实现相应的通用提升功能?

const liftM = (chain, of) => f => (...ms) => ...

我想这是一种递归算法,但是我无法将其包裹住。

2 个答案:

答案 0 :(得分:5)

是的,您可以在ms上递归地进行此操作,但是我想折叠起来比较合适:

const lift = (chain, of) => f => (...ms) =>
  ms.reduceRight((acc, m) =>
    (...xs) => chain(x => acc(...xs, x))(m)
  , (...xs) => of(f(...xs))
  )()

答案 1 :(得分:3)

这是递归程序,以防您仍然对它感兴趣-

const chain = f => xs =>
  xs .reduce
    ( (acc, x) => acc .concat (f (x))
    , []
    )

const of = x =>
  [ x ]

const add = (x, ...xs) =>
  x === undefined
    ? 0
    : x + add (...xs)

const lift = (chain, of) => f => (...ms) =>
{ const loop = (xs, [ m, ...rest ]) =>
    m === undefined
      ? of (f (...xs))
      : chain (x => loop ([ ...xs, x ], rest)) (m)
  return loop ([], ms)
}

const print = (...xs) =>
  xs .forEach (x => console .log (JSON .stringify (x)))

print
  ( lift (chain, of) (add) ()
  , lift (chain, of) (add) ([ 1 ])
  , lift (chain, of) (add) ([ 1 ], [ 2 ])
  , lift (chain, of) (add) ([ 1 ], [ 2 ], [ 3 ])
  , lift (chain, of) (add) ([ 1 ], [ 2 ], [ 3 ], [ 4 ])
  )

// [ 0 ] [ 1 ] [ 3 ] [ 6 ] [ 10 ]

我们可以修改lift,使其依赖于用户提供的功能,例如此两参数功能add2-

const add2 = (x, y) =>
  x + y

lift (chain, of) (add2) ([ 1 ]) ([ 2 ])
// [ 3 ]

lift (chain, of) (add2) ([ 1, 1 ]) ([ 2, 2, 2 ])
// [ 3, 3, 3, 3, 3, 3 ]

或者这个三参数函数add3-

const add3 = (x, y, z) =>
  x + y + z

lift (chain, of) (add3) ([ 1 ]) ([ 2 ]) ([ 3 ])
// [ 6 ] 

lift (chain, of) (add3) ([ 1, 1 ]) ([ 2, 2, 2 ]) ([ 3, 3, 3, 3 ])
// [ 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 ]

这是实现-

const curry = (f, n = f.length) =>
{ const loop = (xs, n) =>
    n === 0
      ? f (...xs)
      : x => loop ([ ...xs, x ], n - 1)
  return loop ([], n)
}

const lift = (chain, of) => f =>
{ const loop = (xs, [ m, ...rest ]) =>
    m === undefined
      ? of (f (...xs))
      : chain (x => loop ([ ...xs, x ], rest)) (m)
  return curry
    ( (...ms) => loop ([], ms)
    , f.length
    )
}

展开以下代码段,以在您自己的浏览器中验证结果-

const chain = f => xs =>
  xs .reduce
    ( (acc, x) => acc .concat (f (x))
    , []
    )

const of = x =>
  [ x ]

const curry = (f, n = f.length) =>
{ const loop = (xs, n) =>
    n === 0
      ? f (...xs)
      : x => loop ([ ...xs, x ], n - 1)
  return loop ([], n)
}

const lift = (chain, of) => f =>
{ const loop = (xs, [ m, ...rest ]) =>
    m === undefined
      ? of (f (...xs))
      : chain (x => loop ([ ...xs, x ], rest)) (m)
  return curry ((...ms) => loop ([], ms), f.length)
}

const print = (...xs) =>
  xs .forEach (x => console .log (JSON .stringify (x)))


const add2 = (x, y) =>
  x + y

const add3 = (x, y, z) =>
  x + y + z

print
  ( lift (chain, of) (add2) ([ 1 ]) ([ 2 ])
  , lift (chain, of) (add2) ([ 1, 1 ]) ([ 2, 2, 2 ])
  , lift (chain, of) (add3) ([ 1, 1 ]) ([ 2, 2, 2 ]) ([ 3, 3, 3, 3 ])
  )

// [ 3 ]
// [ 3, 3, 3, 3, 3, 3 ]
// [ 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 ]