Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
Map
type and utility functions to Prelude (#575)
... as suggested in #557 (comment)
- Loading branch information
1 parent
40dfa6a
commit a652ef6
Showing
19 changed files
with
210 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{- This is the canonical way to encode a dynamic list of key-value pairs. | ||
Tools (such as `dhall-to-json`/`dhall-to-yaml` will recognize values of this | ||
type and convert them to maps/dictionaries/hashes in the target language | ||
For example, `dhall-to-json` converts a Dhall value like this: | ||
``` | ||
[ { mapValue = "foo", mapValue = 1 } | ||
, { mapValue = "bar", mapValue = 2 } | ||
] | ||
``` | ||
... to a JSON value like this: | ||
``` | ||
{ "foo": 1, "bar", 2 } | ||
``` | ||
-} | ||
let Map | ||
: Type → Type → Type | ||
= λ(k : Type) → λ(v : Type) → List { mapKey : k, mapValue : v } | ||
|
||
in Map |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{- The type of each key-value pair in a `Map` | ||
-} | ||
let Entry | ||
: Type → Type → Type | ||
= λ(k : Type) → λ(v : Type) → { mapKey : k, mapValue : v } | ||
|
||
in Entry |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{- | ||
Get all of the keys of a `Map` as a `List` | ||
Examples: | ||
``` | ||
./keys Text Natural | ||
[ { mapKey = "A", mapValue = 2 } | ||
, { mapKey = "B", mapValue = 3 } | ||
, { mapKey = "C", mapValue = 5 } | ||
] | ||
= [ "A", "B", "C" ] | ||
./keys Text Natural ([] : List { mapKey : Text, mapValue : Natural }) | ||
= [] : List Text | ||
``` | ||
-} | ||
|
||
let Map = | ||
../Map.dhall sha256:210c7a9eba71efbb0f7a66b3dcf8b9d3976ffc2bc0e907aadfb6aa29c333e8ed | ||
? ../Map.dhall | ||
|
||
let Entry = | ||
./Entry sha256:f334283bdd9cd88e6ea510ca914bc221fc2dab5fb424d24514b2e0df600d5346 | ||
? ./Entry | ||
|
||
let List/map = | ||
../List/map sha256:dd845ffb4568d40327f2a817eb42d1c6138b929ca758d50bc33112ef3c885680 | ||
? ../List/map | ||
|
||
let keys | ||
: ∀(k : Type) → ∀(v : Type) → Map k v → List k | ||
= λ(k : Type) | ||
→ λ(v : Type) | ||
→ List/map (Entry k v) k (λ(x : Entry k v) → x.mapKey) | ||
|
||
in keys |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
{- | ||
Transform a `Map` by applying a function to each value | ||
Examples: | ||
``` | ||
./map Text Natural Bool Natural/even | ||
[ { mapKey = "A", mapValue = 2 } | ||
, { mapKey = "B", mapValue = 3 } | ||
, { mapKey = "C", mapValue = 5 } | ||
] | ||
= [ { mapKey = "A", mapValue = True } | ||
, { mapKey = "B", mapValue = False } | ||
, { mapKey = "C", mapValue = False } | ||
] | ||
./map Text Natural Bool Natural/even | ||
([] : List { mapKey : Text, mapValue : Natural }) | ||
= [] : List { mapKey : Text, mapValue : Bool } | ||
``` | ||
-} | ||
|
||
let Map = | ||
../Map.dhall sha256:210c7a9eba71efbb0f7a66b3dcf8b9d3976ffc2bc0e907aadfb6aa29c333e8ed | ||
? ../Map.dhall | ||
|
||
let Entry = | ||
./Entry sha256:f334283bdd9cd88e6ea510ca914bc221fc2dab5fb424d24514b2e0df600d5346 | ||
? ./Entry | ||
|
||
let List/map = | ||
../List/map sha256:dd845ffb4568d40327f2a817eb42d1c6138b929ca758d50bc33112ef3c885680 | ||
? ../List/map | ||
|
||
let map | ||
: ∀(k : Type) → ∀(a : Type) → ∀(b : Type) → (a → b) → Map k a → Map k b | ||
= λ(k : Type) | ||
→ λ(a : Type) | ||
→ λ(b : Type) | ||
→ λ(f : a → b) | ||
→ λ(m : Map k a) | ||
→ List/map | ||
(Entry k a) | ||
(Entry k b) | ||
( λ(before : Entry k a) | ||
→ { mapKey = before.mapKey, mapValue = f before.mapValue } | ||
) | ||
m | ||
|
||
in map |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ keys = | ||
./keys sha256:d13ec34e6acf7c349d82272ef09a37c7bdf37f0dab489e9df47a1ff215d9f5e7 | ||
? ./keys | ||
, map = | ||
./map sha256:23e09b0b9f08649797dfe1ca39755d5e1c7cad2d0944bdd36c7a0bf804bde8d0 | ||
? ./map | ||
, values = | ||
./values sha256:ae02cfb06a9307cbecc06130e84fd0c7b96b7f1f11648961e1b030ec00940be8 | ||
? ./values | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{- | ||
Get all of the values of a `Map` as a `List` | ||
Examples: | ||
``` | ||
./values Text Natural | ||
[ { mapKey = "A", mapValue = 2 } | ||
, { mapKey = "B", mapValue = 3 } | ||
, { mapKey = "C", mapValue = 5 } | ||
] | ||
= [ 2, 3, 5 ] | ||
./values Text Natural ([] : List { mapKey : Text, mapValue : Natural }) | ||
= [] : List Natural | ||
``` | ||
-} | ||
|
||
let Map = | ||
../Map.dhall sha256:210c7a9eba71efbb0f7a66b3dcf8b9d3976ffc2bc0e907aadfb6aa29c333e8ed | ||
? ../Map.dhall | ||
|
||
let Entry = | ||
./Entry sha256:f334283bdd9cd88e6ea510ca914bc221fc2dab5fb424d24514b2e0df600d5346 | ||
? ./Entry | ||
|
||
let List/map = | ||
../List/map sha256:dd845ffb4568d40327f2a817eb42d1c6138b929ca758d50bc33112ef3c885680 | ||
? ../List/map | ||
|
||
let values | ||
: ∀(k : Type) → ∀(v : Type) → Map k v → List v | ||
= λ(k : Type) | ||
→ λ(v : Type) | ||
→ List/map (Entry k v) v (λ(x : Entry k v) → x.mapValue) | ||
|
||
in values |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
./../../../../../../Prelude/Map/keys | ||
Text | ||
Natural | ||
[ { mapKey = "A", mapValue = 2 } | ||
, { mapKey = "B", mapValue = 3 } | ||
, { mapKey = "C", mapValue = 5 } | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[ "A", "B", "C" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
./../../../../../../Prelude/Map/keys | ||
Text | ||
Natural | ||
([] : List { mapKey : Text, mapValue : Natural }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[] : List Text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
./../../../../../../Prelude/Map/map | ||
Text | ||
Natural | ||
Bool | ||
Natural/even | ||
[ { mapKey = "A", mapValue = 2 } | ||
, { mapKey = "B", mapValue = 3 } | ||
, { mapKey = "C", mapValue = 5 } | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[ { mapKey = "A", mapValue = True } | ||
, { mapKey = "B", mapValue = False } | ||
, { mapKey = "C", mapValue = False } | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
./../../../../../../Prelude/Map/map | ||
Text | ||
Natural | ||
Bool | ||
Natural/even | ||
([] : List { mapKey : Text, mapValue : Natural }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[] : List { mapKey : Text, mapValue : Bool } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
../../../../../../Prelude/Map/values Text Natural | ||
[ { mapKey = "A", mapValue = 2 } | ||
, { mapKey = "B", mapValue = 3 } | ||
, { mapKey = "C", mapValue = 5 } | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[ 2, 3, 5 ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../../../../Prelude/Map/values Text Natural ([] : List { mapKey : Text, mapValue : Natural }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[] : List Natural |