Skip to content

Add 'Arbitrary' instance for records#82

Merged
paf31 merged 1 commit into
purescript:masterfrom
sharkdp:arbitrary-records
Sep 2, 2017
Merged

Add 'Arbitrary' instance for records#82
paf31 merged 1 commit into
purescript:masterfrom
sharkdp:arbitrary-records

Conversation

@sharkdp
Copy link
Copy Markdown
Contributor

@sharkdp sharkdp commented Sep 2, 2017

This adds an Arbitrary instance for general records:

Example

type Person =
  { name :: { first :: String
            , last :: String
            }
  , age :: Int
  , height :: Number
  }

> persons ← randomSample' 10 (arbitrary :: Gen Person)

Note: This is the first time that I'm using the new (and awesome) typelevel-programming features of PureScript, so I'm not sure if I'm doing this correctly 😄

@LiamGoodacre
Copy link
Copy Markdown
Member

Very cool 👍

arbitraryRecord _ = do
value <- arbitrary
previous <- arbitraryRecord (RLProxy :: RLProxy listRest)
pure $ insert (SProxy :: SProxy key) value previous
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes me think we should add ST support to purescript-records, so we can build the record in place.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That said, I'm not sure what type we'd give to in-place insertion of a new field :/

@paf31 paf31 merged commit a308baf into purescript:master Sep 2, 2017
@paf31
Copy link
Copy Markdown
Contributor

paf31 commented Sep 2, 2017

Thanks, looks great! I'll think a bit about the ST thing.

@paf31
Copy link
Copy Markdown
Contributor

paf31 commented Sep 2, 2017

Here's something I put together for building records efficiently in place:

http://try.purescript.org/?backend=core&gist=27a137278caa203b3ebbd42f7386a0db

I might add this to purescript-records.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants