Saturday, July 10, 2021

tuples in JSON and JavaScript?

What is tuple? - Definition from WhatIs.com

1) In programming languages, such as LispPython, Linda, and others, a tuple (pronounced TUH-pul) is an ordered set of values. The separator for each value is often a comma (depending on the rules of the particular language). Common uses for the tuple as a data type are (1) for passing a string of parameters from one program to another, and (2) representing a set of value attributes in a relational database. In some languages, tuples can be nested within other tuples within parentheses or brackets or other delimiters. Tuples can contain a mixture of other data types.


 A first look at records and tuples in JavaScript

At the moment, JavaScript only compares primitive values such as strings by value (by looking at their contents):

> 'abc' === 'abc'
true

In contrast, objects are compared by identity (each object has a unique identity and is only strictly equal to itself):

> {x: 1, y: 4} === {x: 1, y: 4}
false
> ['a', 'b'] === ['a', 'b']
false

> const obj = {x: 1, y: 4};
> obj === obj
true

The proposal Record & Tuple (by Robin Ricard and Rick Button) lets us create compound values that are compared by value.

For, example, by prefixing an object literal with a number sign (#), we create a record – a compound value that is compared by value and immutable:

> #{x: 1, y: 4} === #{x: 1, y: 4}
true

If we prefix an Array literal with #, we create a tuple – an Array that is compared by value and immutable:

> #['a', 'b'] === #['a', 'b']
true


Python Tuples

thistuple = ("apple", "banana", "cherry")


Preserve Python tuples with JSON - Stack Overflow

jsonstring =  enc.encode([1, 2, (3, 4), [5, 6, (7, 8)]])
# [1, 2, {"items": [3, 4], "__tuple__": true}, [5, 6, {"items": [7, 8], "__tuple__": true}]]

^ too complex encoding :(


JSON - Wikipedia


No comments: