A tiny tuple implementation
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.
Tuple instance containing some values.
// No values.var empty = ;// A single value.var one = 'hello';// Loads of values!var loads = 'foo' 'bar' 'baz';
Extracting those values.
var point = 50 75;// You can unpack them into any names you want.// They're just function arguments!pointunpackdoStuffx * y;;
Getting a value from
var guy = 'Oliver' 'Caldwell';// Unpack returns the value that was returned by the function you passed to it.var name = guyunpackreturnfirstNamelastNamejoin' ';;name; // "Oliver Caldwell"
You can make this unpacking cleaner by passing predefined functions.
return a + b;var numbers = 10 20;var result = numbersunpackadd;result; // 30
Getting a string representation of a tuple.
var t = 'foo' 'bar';// Manual call to toString.ttoString; // "(foo, bar)"// Automatic coercion too!'Hello! ' + t; // "Hello! (foo, bar)"
Tuples are array-like objects, that means you can treat them like arrays and it will work just fine. You can access each value with the numerical attribute and measure it with the
var t = 'foo' 'bar';var i;for i = 0; i < tlength; t += 1console.logti;// log: 'foo'// log: 'bar'console.logttoArrayjoin'-'; // 'foo-bar'
Because it's array-like, you can even do crazy stuff like this.
var t = 'Hello, World!';console.logapplynull t;// log: 'Hello, World!'
The browser has automatically converted the tuple to an array and applied it to the
You can also iterate over the tuple using the
forEach method if you don't want to touch the array-like properties.
var t = 10 20 30;tforEachconsole.logvalue;;// log: 10// log: 20// log: 30
You need not iterate over the tuple to compare values though, you can use the
equals method for that.
var t1 = 'foo' 'bar';var t2 = 'foo' 'bar';var t3 = 'foo' 'bar' 'baz';t1equalst2; // truet2equalst1; // truet1equalst3; // falset3equalst2; // false// Or compare to any other array-like object!t1equals'foo' 'bar'; // truet1equals1 2; // false
You can perform greater or less than comparisons on tuples thanks to their
valueOf method. This method returns the product of all values contained within the tuple added together.
Simple tuple implementation. This constructor will create new instances and store immutable values within them.
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.
Flattens the tuples values into a string.
Coerces the tuple into an array. This runs through
Array.prototype.slice.call because tuples are array-like objects.
Iterates over every value within the tuple and pass the said values to the provided callback individually.
The callback is also passed the current index and tuple instance in that order. This matches the normal
Compares each value in both tuples, one value at a time in order. Both tuples have to be of the same length and need to contain the exact same values for this to return true. This can be used to compare against any array-like object.
Returns the product of adding all contained values together. If you only have numbers within your tuple you will get back all of those numbers added together. If you have strings too then you will get a string containing all of the values.
This function is called automatically when using greater or less than comparisons on tuples. So
tuple1 > tuple2 will add all of the containing values together and then compare them.
Fetches the value from the tuple at a specific index in the exact same was as an array. A tuple is an array-like object, so you can use the length and
Contains the number of elements held within the tuple.
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.