tuple-w

A tiny tuple implementation

Tuple

A tiny JavaScript tuple implementation. This came to fruition after a blog post I wrote got a bit of attention. I felt like refining the idea and turning it into a proper project with package management, tests and documentation.

This version is quite different to my original implementation (which received quite a bit of critique on Hacker News). I have dropped a few things that meant it was not a real tuple, so now it's closer to the mark. I have tried to follow a KISS (Keep It Simple Stupid) mentality whilst solidifying my ideas within this repository. As a result the API and source are absolutely tiny.

It can be installed through bower with bower install tuple and npm with npm install tuple-w. I had to add a "w" (for Wolfy87) because the name "tuple" was taken.

Creating a Tuple instance containing some values.

// No values. 
var empty = new Tuple();
 
// A single value. 
var one = new Tuple('hello');
 
// Loads of values! 
var loads = new Tuple('foo', 'bar', 'baz');

Extracting those values.

var point = new Tuple(50, 75);
 
// You can unpack them into any names you want. 
// They're just function arguments! 
point.unpack(function (xy) {
    doStuff(* y);
});

Getting a value from unpack.

var guy = new Tuple('Oliver', 'Caldwell');
 
// Unpack returns the value that was returned by the function you passed to it. 
var name = guy.unpack(function (firstNamelastName) {
    return [
        firstName,
        lastName
    ].join(' ');
});
 
name; // "Oliver Caldwell" 

You can make this unpacking cleaner by passing predefined functions.

function add(ab) {
    return a + b;
}
 
var numbers = new Tuple(10, 20);
var result = numbers.unpack(add);
 
result; // 30 

Getting a string representation of a tuple.

var t = new Tuple('foo', 'bar');
 
// Manual call to toString. 
t.toString(); // "(foo, bar)" 
 
// Automatic coercion too! 
'Hello! ' + t; // "Hello! (foo, bar)" 

Simple tuple implementation. This constructor will create new instances and store immutable values within them.

  • Class Tuple A tiny tuple implementation.
  • Param {...*} List of values to store within the tuple.

Passes the values as arguments, in the same order they were set, to the provided unpacker function. It will return the value that the unpacker returns.

  • Param {Function} unpacker Is passed all of the tuples values in order, it's return value will be returned.
  • Return {*} The value that the unpacker function returns.

Flattens the tuples values into a string.

  • Return {String} A textual representation of the tuples contents.

You can execute the tests by running the local Python HTTP server (./tools/server.sh) and navigating to http://localhost:8000/tests/.

Copyright (c) 2013 Oliver Caldwell

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.