Skip to content

Add Newtype exercise to calculate wattage. - #273

Merged
milesfrain merged 15 commits into
purescript-contrib:masterfrom
shaunplee:newtype-exercise
Dec 31, 2020
Merged

milesfrain merged 15 commits into
purescript-contrib:masterfrom
shaunplee:newtype-exercise

Conversation

@shaunplee

Copy link
Copy Markdown

One simple proposal to address #255 using an extension of the existing example in the Newtype section.

@shaunplee shaunplee closed this Dec 30, 2020
@shaunplee shaunplee reopened this Dec 30, 2020
@shaunplee shaunplee closed this Dec 30, 2020
@shaunplee
shaunplee deleted the newtype-exercise branch December 30, 2020 06:30
@milesfrain

Copy link
Copy Markdown
Member

I think this looks good overall.
Might want to make this a bit more straightforward for readers with:

calculateWattage :: Amp -> Volt -> Watt

@shaunplee
shaunplee restored the newtype-exercise branch December 30, 2020 15:43
@shaunplee shaunplee reopened this Dec 30, 2020
@shaunplee

Copy link
Copy Markdown
Author

Might want to make this a bit more straightforward for readers with:

calculateWattage :: Amp -> Volt -> Watt

Yes, I like that better.

I also missed that there was already a definition of Watt, so I've rewritten the exercise accordingly and also rewrote the test to use the existing MakeWatt constructor.

Comment thread text/chapter5.md Resolved
Comment thread exercises/chapter5/test/Main.purs Outdated
Comment on lines +5 to +11
import Test.NoPeeking.Solutions

import ChapterExamples (Amp(..), current, fromString, gcd, gcdV2, isEmpty, livesInLA, lzs, partialFunction, showPerson, showPersonV2, sortPair, takeFive, toString, unknownPerson)
import Test.NoPeeking.Solutions as Solutions
import ChapterExamples (Amp(..), current, fromString, gcd, gcdV2, isEmpty, livesInLA, lzs, partialFunction, showPerson, showPersonV2, sortPair, takeFive, toString, unknownPerson, Volt(..), Watt(..))
import Data.Int (round)
import Data.Maybe (Maybe(Just, Nothing))
import Data.Person (Person)
import Data.Picture (Shape(..), Picture, getCenter, origin)
import Data.Picture (Shape(..), Picture, getCenter, origin, shapeBounds)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's rebase this on the fix for #274 (in progress). That should simplify some of these import diffs. There are also some complications in this PR due to this chapter is missing the automated solutions reset flag. I'll fix that as well before rebasing this PR.

import Test.NoPeeking.Solutions  -- Note to reader: Delete this line

@milesfrain

Copy link
Copy Markdown
Member

Let me know if you're actively rebasing this. I was going to do that now to clean-up the merge conflict I introduced.

Comment thread exercises/chapter5/test/Main.purs Outdated
Comment on lines +114 to +115
let
(MakeWatt w) = calculateWattage (Amp 0.5) (Volt 120.0)
Assert.equal 60.0 w
Assert.equal (MakeWatt 60.0)
$ calculateWattage (Amp 0.5) (Volt 120.0)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this is a little more idomatic

@shaunplee shaunplee Dec 30, 2020 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I think I tried something like that, but as I recall, the compiler complained about the lack of an Eq instance for Watt. I also thought it would be unreasonable to ask the reader to include a deriving Eq or something to their definition of Watt just to satisfy the tests, because they haven't been exposed to type classes yet.

If this works, I'm good with it.

@milesfrain

Copy link
Copy Markdown
Member

Let me know if you're ready for this PR to merge.

@milesfrain

Copy link
Copy Markdown
Member

I also missed that there was already a definition of Watt, so I've rewritten the exercise accordingly and also rewrote the test to use the existing MakeWatt constructor.

I'm thinking this exercise might be even more valuable to readers if we don't rely on an existing constructor, so they get to create their own. That would involve the following changes:

  • Change the MakeWatt example in the text to MakeColumb
  • Restore your manual unwrapping in the test (since we can't ask readers to derive their own Eq and Show instances yet)

This could be a follow-up PR.

Also, sorry if the force push on your branch causes any inconvenience. I find that makes rebases easier to manage (assuming you're not also actively working on the same branch).

@shaunplee

Copy link
Copy Markdown
Author

Sorry, my builds keep failing because it seems I'm missing some step. It looks like some issue with testing the Shape implementation in Picture.purs versus ShapeExt implementation in Solutions.purs but I'm not exactly clear on how this is handled.

Other misc changes:
- No need for Columb Eq and Show
- Construct with Watt, rather than MakeWatt
@milesfrain

Copy link
Copy Markdown
Member

This is unfortunately a bit of a tricky (and rare) situation with the book's workaround for both allowing CI on "official" solutions, while also allowing resetting solutions when the exercise involves modifying an existing ADT.
Anyway, it's fixable by just avoiding the duplicate shapeBounds import from Data.Picture until the other implicit import from Test.NoPeeking.Solutions is cleared away during solutions reset.

This is fixed now. Awaiting your approval to merge.

@shaunplee

Copy link
Copy Markdown
Author

@milesfrain Thanks for your help!

@milesfrain
milesfrain merged commit ae33aca into purescript-contrib:master Dec 31, 2020
@shaunplee
shaunplee deleted the newtype-exercise branch December 31, 2020 16:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants