# Set and Range objects and literals

**URL:** https://es.discourse.group/t/set-and-range-objects-and-literals/1750
**Category:** 💡 Ideas
**Created:** [July 7, 2023, 12:19am UTC](https://es.discourse.group/t/set-and-range-objects-and-literals/1750 "2023-07-07T00:19:02Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![dspeyer](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/dspeyer/32/965_2.png) [@dspeyer](https://es.discourse.group/u/dspeyer)
#### Post date: [July 7, 2023, 12:19am UTC](https://es.discourse.group/t/set-and-range-objects-and-literals/1750/1 "2023-07-07T00:19:02Z")

</div>

Expanding my comment from [the chaining comparitor proposal](https://es.discourse.group/t/operators/1577/30)...

Currently we write:

```
if (longVariableName == 'p' || longVariableName == 'div') {...}
if (effectiveMousePos.x <= right && effectiveMousePos.x >= left) {...}

```

and it's annoyingly verbose. Instead, I'd like to write:

```
if (longVariableName in {{p, div}}) {...}
if (effectiveMousePos.x in [<left, right>]) {...}

```

Sets are kind of like maps, so you can write (I have written)

```
if (longVariableName in {p:1, div:1}) {...}

```

but the irrelevant values are distracting, so it's awkward. Doubly so if there's a formatter or style rule that insists on thinking of the set as a map.

Literals and `in` tests are the case I most care about, but as long as we're doing this...

- `set[key] = True/False` adds or removes a key from a set
- sets take `*`, `+`, and `-` operators for intersection, union, and set-difference, with obvious `*=`, `+=`, and `-=`.
- `for (i in set)` does this obvious
- ranges have members `start`, `end`, `min`, and `max`. The first two are ordered based on definition, the latter based on comparison.
- `for (i of range) ` starts at `min`, then adds 1 until `i` exceeds `max`
- `for (i of range.by(step))` does the same except that it adds `step` each time. If `step` is negative, it starts from `max`.

---

<div class="post-metadata">

### Author: ![bakkot](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bakkot/32/22_2.png) [@bakkot](https://es.discourse.group/u/bakkot)
#### Post date: [July 7, 2023, 12:41am UTC](https://es.discourse.group/t/set-and-range-objects-and-literals/1750/2 "2023-07-07T00:41:08Z")

</div>

It's probably not worth adding syntax for this.

For your first use case, `['p', 'div'].includes(longVariableName)` already exists, is sufficient, and is plenty fast (you need a surprisingly amount of data before membership tests start being slower for arrays than for hashmaps, certainly more than it would be sensible to write inline).

---

<div class="post-metadata">

### Author: ![0X-JonMichaelGalindo](https://avatars.discourse-cdn.com/v4/letter/0/4af34b/32.png) [@0X-JonMichaelGalindo](https://es.discourse.group/u/0X-JonMichaelGalindo)
#### Post date: [July 14, 2023, 11:51am UTC](https://es.discourse.group/t/set-and-range-objects-and-literals/1750/3 "2023-07-14T11:51:56Z")

</div>

How would this syntax handle logic?

```javascript
if( x > left && x < right ) ...
//vs.:
if( x > left || x < right ) ...

```

---

<div class="post-metadata">

### Author: ![rdking](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/rdking/32/54_2.png) [@rdking](https://es.discourse.group/u/rdking)
#### Post date: [August 24, 2023, 7:13pm UTC](https://es.discourse.group/t/set-and-range-objects-and-literals/1750/4 "2023-08-24T19:13:17Z")

</div>

Try this:

```js
//Yours
if (effectiveMousePos.x <= right && effectiveMousePos.x >= left) {...}

//Alternative (loose)
if ({with(effectiveMousePos) {x <= right && x>= left}}) {...}

//Alternative (strict)
if ((({x}) => (x <= right && x>= left))(effectiveMousePos)) {...}

```
