• Jump To … +
    Either.js List.js Maybe.js Set.js Stream.js Thunk.js Tuple.js boolean.js char.js base.js extra.js fold.js map.js zipWith.js collection.js control.js curried.js eq.js floating.js fractional.js function.js integral.js match.js num.js numeric.js object.js ord.js realfrac.js register.js string.js type.js nodash.js
  • Tuple.js

  • ¶
    /* vim: set et sw=2 ts=2: */
    'use strict';
    
    module.exports = [ 'freeze', function (freeze) {
    
      var Nodash = this;
      
      function Tuple(fst, snd) {
        if (!(this instanceof Tuple)) {
          return new Tuple(fst, snd);
        }
    
        this.fst = Nodash.idf(fst);
        this.snd = Nodash.idf(snd);
    
        freeze(this);
      }
      Tuple.__type = 'tuple';
    
      function tuple(first, second) {
        return new Tuple(first, second);
      }
    
      function tuple3(first, second, third) {
        return tuple(first, tuple(second, third));
      }
    
      function tuple4(first, second, third, fourth) {
        return tuple3(first, second, tuple(third, fourth));
      }
    
      return {
        Tuple: Tuple,
        
        fst: function (t) {
          return t.fst();
        },
    
        snd: function (t) {
          return t.snd();
        },
    
        ', tuple': tuple,
        ',, tuple3': tuple3,
        ',,, tuple4': tuple4,
    
        tuplesToObject: function (xs) {
          var obj = {};
          Nodash.each(function (t) {
            obj[t.fst()] = t.snd();
          }, xs);
          return obj;
        },
    
        objectToArray: function (obj) {
          var arr = [];
          Nodash.each(function (val, key) {
            arr.push(new Tuple(key, val));
          }, obj);
          return arr;
        }
    
      };  
    }];