squiggle/packages/website/docs/Api/Dictionary.md

100 lines
1.6 KiB
Markdown
Raw Permalink Normal View History

2022-06-05 20:59:45 +00:00
---
2022-06-11 00:35:48 +00:00
sidebar_position: 2
2022-06-06 03:02:17 +00:00
title: Dictionary
2022-06-05 20:59:45 +00:00
---
2022-06-11 15:57:02 +00:00
2022-06-13 04:19:28 +00:00
Squiggle dictionaries work similar to Python dictionaries. The syntax is similar to objects in Javascript.
Dictionaries are unordered and duplicates are not allowed. They are meant to be immutable, like most types in Squiggle.
**Example**
2022-06-13 19:10:24 +00:00
2022-06-13 04:19:28 +00:00
```javascript
valueFromOfficeItems = {
keyboard: 1,
chair: 0.01 to 0.5,
headphones: "ToDo"
}
valueFromHomeItems = {
monitor: 1,
bed: 0.2 to 0.6,
lights: 0.02 to 0.2,
coffee: 5 to 20
}
homeToItemsConversion = 0.1 to 0.4
conversionFn(i) = [i[0], i[1] * homeToItemsConversion]
updatedValueFromHomeItems = valueFromHomeItems |> Dict.toList |> map(conversionFn) |> Dict.fromList
allItems = merge(valueFromOfficeItems, updatedValueFromHomeItems)
```
2022-06-11 00:35:48 +00:00
### toList
2022-06-11 15:57:02 +00:00
2022-06-06 05:16:29 +00:00
```
2022-06-11 00:35:48 +00:00
Dict.toList: (dict<'a>) => list<list<string|a>>
2022-06-05 20:59:45 +00:00
```
2022-06-11 00:35:48 +00:00
```js
2022-06-11 15:57:02 +00:00
Dict.toList({ foo: 3, bar: 20 }); // [["foo", 3], ["bar", 20]]
2022-06-06 05:16:29 +00:00
```
2022-06-11 00:35:48 +00:00
### fromList
2022-06-11 15:57:02 +00:00
2022-06-06 05:16:29 +00:00
```
2022-06-11 00:35:48 +00:00
Dict.fromList: (list<list<string|'a>>) => dict<'a>
2022-06-05 20:59:45 +00:00
```
2022-06-11 15:57:02 +00:00
2022-06-11 00:35:48 +00:00
```js
2022-06-11 15:57:02 +00:00
Dict.fromList([
["foo", 3],
["bar", 20],
]); // {foo: 3, bar: 20}
2022-06-06 05:16:29 +00:00
```
### keys
2022-06-11 15:57:02 +00:00
2022-06-06 05:16:29 +00:00
```
Dict.keys: (dict<'a>) => list<string>
2022-06-05 20:59:45 +00:00
```
2022-06-11 15:57:02 +00:00
2022-06-11 00:35:48 +00:00
```js
2022-06-11 15:57:02 +00:00
Dict.keys({ foo: 3, bar: 20 }); // ["foo", "bar"]
2022-06-11 00:35:48 +00:00
```
2022-06-06 05:16:29 +00:00
### values
2022-06-11 15:57:02 +00:00
2022-06-06 05:16:29 +00:00
```
Dict.values: (dict<'a>) => list<'a>
2022-06-05 20:59:45 +00:00
```
2022-06-11 15:57:02 +00:00
2022-06-11 00:35:48 +00:00
```js
2022-06-11 15:57:02 +00:00
Dict.values({ foo: 3, bar: 20 }); // [3, 20]
2022-06-11 00:35:48 +00:00
```
2022-06-06 05:16:29 +00:00
### merge
2022-06-11 15:57:02 +00:00
2022-06-06 05:16:29 +00:00
```
Dict.merge: (dict<'a>, dict<'b>) => dict<'a|b>
2022-06-05 20:59:45 +00:00
```
2022-06-11 00:35:48 +00:00
```js
2022-06-11 15:57:02 +00:00
first = { a: 1, b: 2 };
snd = { b: 3, c: 5 };
Dict.merge(first, snd); // {a: 1, b: 3, c: 5}
2022-06-11 00:35:48 +00:00
```
2022-06-06 05:16:29 +00:00
### mergeMany
2022-06-11 15:57:02 +00:00
2022-06-05 20:59:45 +00:00
```
2022-06-06 05:16:29 +00:00
Dict.mergeMany: (list<dict<'a>>) => dict<'a>
2022-06-11 00:35:48 +00:00
```
```js
2022-06-11 15:57:02 +00:00
first = { a: 1, b: 2 };
snd = { b: 3, c: 5 };
Dict.mergeMany([first, snd]); // {a: 1, b: 3, c: 5}
```