Last active
August 11, 2022 16:50
-
-
Save robkuz/c2ce150b104e8c56e3f0b6580fede7ea to your computer and use it in GitHub Desktop.
Some thoughts on Purescript Tuples, Records and Dict
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
--The only way to create what other languages call a **Dictionary** Is to create a `Array (Tuple String a)`. | |
--This is excessively lengthy (imo). Specifically as the change to 0.7 also removed the Haskell Tuple syntax. | |
--example | |
d = [Tuple "x1" 1, Tuple "x2" 2, Tuple "y1" 3, Tuple "y2" 4] | |
-- prior t0 0.7 you could at least do it this way | |
d = [("x1", 1), ("x2", 2), ("y1", 3), ("y2", 4)] | |
-- the problem with the above however is that it is possible to assign the same "label" multiple times. | |
-- Records on the other hand check for the occurance of the same label and you get a compile time error. | |
d = {x1: 1, x2: 2, y1: 3, y2: 4m} | |
-- so all of this much shorter AND compile time checked as well. | |
-- How sweet! |
(Reposting accidentally deleted comment:)
You can use /\
from Data.Tuple.Nested
:
Map.fromFoldable ["x1" /\ 1, ... ]`
oh! just seeing all these good comments. thx.
yeah the infix operator makes it a bit nicer indeed.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also, I don't think PureScript ever had Haskell-like tuple syntax. Maybe you're confusing it with Elm? See this pull request, for instance, which was opened in August 2014, and which would have added tuples to the language (had it not been closed): purescript/purescript#556.