Id functor Typescript -example

by dimitrs_papadimitriou

HTML

<code>
   new Id(2).map(square).Value : <span id="1"  ></span>
 </code>

<br>
  <code> 
  new Id(square(2))  :  <span id="2"  ></span>
 </code>

TypeScript

function log(id:number,x:any):void {
     $("#"+id.toString()).text(x);
  }  
  
class Id<T> {
  Value: T;

  constructor(value: T) {
    this.Value = value;
  }

  map<T1>(f: (y: T) => T1): Id<T1> {
    return new Id<T1>(f(this.Value));
  } 
 
}//
  
  function square(x:number):number {
     return  x * x;
  }  
 
 log( 1,new Id(2).map(square).Value)
  log(2, new Id(2).map(square).Value)
new Id(square(2)).Value