Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Identity Monad">
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.20.1/ramda.min.js"></script>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Identity Monad</title>
</head>
<body>
</body>
</html>
 
class Identity {
  constructor(x) {
    this.run = () => x
    
    // this :: Identity x
    // (x -> y) -> Identity y
    this.map = f => new Identity(f(this.run()))
    
    // this :: Identity x
    // (x -> Identity y) -> Identity y
    this.bind = f => f(this.run())
    
    // this :: Identity x
    // (x -> (y || Identity y)) -> Identity y
    this.then = f => {
      const y = f(this.run())
      return y instanceof Identity ? y : new Identity(y)
    }
  }
}
Identity.pure = x => new Identity(x)
const result = Identity.pure(10)
  .then(x => x + 1) // map
  .then(x => new Identity(x * 2)) // bind
.run()
console.log(result)
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
homampro
0viewers