Standard Library: Obj

Work with objects (key-value maps)

For more information on objects see the language reference.

  1. focus
  2. get
  3. getDefault
  4. has
  5. isEmpty
  6. keys
  7. len
  8. matches
  9. merge
  10. of
  11. product
  12. put
  13. remove
  14. removeAll
  15. rename
  16. select
  17. set
  18. setDefault
  19. values

Obj.focus(object, keys)

Reorder the entries in object so that the given keys appear first, in the given order.

Obj.focus({ name: "Lamar", yards: 4172, tds: 41, ints: 4 }, ["name", "tds"])
{ name: "Lamar", tds: 41, yards: 4172, ints: 4 }

Obj.get(object, key)

Get the value corresponding to key in object.

Obj.get({ q: 9, r: 5, b: 3, n: 3, p: 1 }, "q")
9

Note that this can also be accomplished using the index [] operator:

{ q: 9, r: 5, b: 3, n: 3, p: 1 }["q"]
9

Obj.getDefault(object, key, default)

Get the value corresponding to key in object, or default if object does not have key.

capitals = { Maine: "Augusta", Maryland: "Annapolis", Massachusetts: "Boston" }
Obj.getDefault(capitals, "Massachusetts", "idk")
Obj.getDefault(capitals, "Massocheichei", "idk")
"Boston"
"idk"

Obj.has(object, key)

Check whether object contains key.

Obj.has({ q: 9, r: 5, b: 3, n: 3, p: 1 }, "q")
Obj.has({ q: 9, r: 5, b: 3, n: 3, p: 1 }, "k")
true
false

Obj.isEmpty(object)

Check whether object is empty.

len({ q: 9, r: 5, b: 3, n: 3, p: 1 })
len({})
5
0

(Accessible as a global through Overloads.isEmpty)


Obj.keys(object)

Get the keys in object as a list.

Obj.keys({ q: 9, r: 5, b: 3, n: 3, p: 1 })
["q", "r", "b", "n", "p"]

Obj.len(object)

Return the number of entries in object.

len({ q: 9, r: 5, b: 3, n: 3, p: 1 })
5

(Accessible as a global through Overloads.len)


Obj.matches(object, matcher)

Check whether object matches the object matcher.

ducky = { name: "Ducky", type: "dog", age: 9 }
Obj.matches(ducky, { name: "Ducky", age: 9 })
Obj.matches(ducky, { name: "Ducky", type: "cat" })

route = { from: { city: "philadelphia" }, to: { city: "baltimore" } }
Obj.matches(route, { from: { city: "philadelphia" } })
Obj.matches(route, { from: { city: "pittsburg" } })
true
false
true
false

Obj.merge(objects)

Merge (flatten) a list of objects into a single object.

Obj.merge([
  { "Hello": "Ola", "Good afternoon": "Boa tarde" },
  { "Hello": "E aí?", "Good night": "Boa noite" },
])
{
  "Hello": "E aí?",
  "Good afternoon": "Boa tarde",
  "Good night": "Boa noite",
}

Obj.of(value)

Convert value to an object, where value is either a table or an object. If value is a table, return an object whose keys are column names and values are lists of the column values in table. If value is an object then return it.

cities = Table.of([
  { city: "New York", state: "NY" },
  { city: "Los Angeles", state: "CA" },
  { city: "Chicago", state: "IL" },
  { city: "Houston", state: "TX" },
])

Obj.of(cities)
{
  city: ["New York", "Los Angeles", "Chicago", "Houston"],
  state: ["NY", "CA", "IL", "TX"],
}

Obj.product(object)

Get the cartesian product of the entries in object.

Given an object, convert each non-list value to a list of length 1. Then create a table containing a row for each possible combination of items from the lists of values. The keys of objects must be strings.

Obj.product({
  rank: ["A", "2", "3", "4" ],
  symbol: ["", "", "", ""],
  count: 1,
})
┌──────┬────────┬───────┐
│ rank │ symbol │ count │ x 16
├──────┼────────┼───────┤
│ "A"  │ ♣      │     1 │
│ "A"  │ ♦      │     1 │
│ "A"  │ ♥      │     1 │
│ "A"  │ ♠      │     1 │
│ "2"  │ ♣      │     1 │
│ "2"  │ ♦      │     1 │
│ "2"  │ ♥      │     1 │
│ "2"  │ ♠      │     1 │
│ "3"  │ ♣      │     1 │
│ "3"  │ ♦      │     1 │
│ "3"  │ ♥      │     1 │
│ "3"  │ ♠      │     1 │
│ "4"  │ ♣      │     1 │
│ "4"  │ ♦      │     1 │
│ "4"  │ ♥      │     1 │
│ "4"  │ ♠      │     1 │
└──────┴────────┴───────┘

Note that this is equivalent to:

Table.product([
  Table.of({ rank: ["A", "2", "3", "4" ] }),
  Table.of({ symbol: ["", "", "", ""] }),
  Table.of({ count: 1 }),
])

Obj.put(value, object, key)

Assign key to value in object.

phrases = { "Hello": "Ola", "Good afternoon": "Boa tarde" }
Obj.put("Good night", phrases, "Boa noite")
{ "Hello": "Ola", "Good afternoon": "Boa tarde", "Boa noite": "Good night" }

Obj.remove(object, key)

Remove the entry with key from object.

remove(
  { name: "Lamar", yards: 4172, tds: 41, ints: 4, fumbles: 10 },
  "fumbles"
)
{ name: "Lamar", yards: 4172, tds: 41, ints: 4 }

(Accessible as a global through Overloads.remove)


Obj.removeAll(object, keys)

Remove the entries with the given keys from object.

Obj.removeAll(
  { name: "Lamar", yards: 4172, tds: 41, ints: 4, fumbles: 10 },
  ["ints", "fumbles"]
)
{ name: "Lamar", yards: 4172, tds: 41 }

Obj.rename(object, old, new)

Update the entry in object with key old to have key new.

Obj.rename(
  { name: "Lamar", yards: 4172, tds: 41, ints: 4 },
  "tds",
  "touchdowns"
)
{ name: "Lamar", yards: 4172, touchdowns: 41, ints: 4 }

Obj.select(object, keys)

Get a subset of the entries in object with the given keys. Entries will appear in the order given in keys.

select({ name: "Lamar", yards: 4172, tds: 41, ints: 4 }, ["name", "tds"])
{ name: "Lamar", tds: 41 }

(Accessible as a global through Overloads.select)


Obj.set(object, key, value)

Assign key to value in object.

phrases = { "Hello": "Ola", "Good afternoon": "Boa tarde" }
Obj.set(phrases, "Good night", "Boa noite")
{ "Hello": "Ola", "Good afternoon": "Boa tarde", "Good night": "Boa noite" }

Note that if you want to update an existing variable, you could also use variable assignment:

phrases = { "Hello": "Ola", "Good afternoon": "Boa tarde" }
phrases["Good night"] = "Boa noite"
phrases =
{ "Hello": "Ola", "Good afternoon": "Boa tarde", "Good night": "Boa noite" }

Obj.setDefault(object, key, value)

Assign key to value in object only if key is not already present.

phrases = { "Hello": "Ola", "Good afternoon": "Boa tarde" }
Obj.setDefault(phrases, "Hello", "E aí")
Obj.setDefault(phrases, "Good night", "Boa noite")
{ "Hello": "Ola", "Good afternoon": "Boa tarde" }
{ "Hello": "Ola", "Good afternoon": "Boa tarde", "Good night": "Boa noite" }

Obj.values(object)

Get the values in object as a list.

Obj.values({ q: 9, r: 5, b: 3, n: 3, p: 1 })
[9, 5, 3, 3, 1]