Skip to content

phone-number 1.2.0.3: enforce area/exchange not starting with 1 #534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions exercises/phone-number/examples/success-standard/src/Phone.hs
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
module Phone (number) where
import Data.Char (isDigit, isLetter)
import Data.Char (isDigit)

number :: String -> Maybe String
number input
| any isLetter input = Nothing
number input = clean input >>= check
Copy link
Member Author

@petertseng petertseng May 9, 2017

Choose a reason for hiding this comment

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

pointfree.io tells me this is number = (check =<<) . clean but I'm not sure I consider that better.

Copy link
Contributor

Choose a reason for hiding this comment

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

You code is definitely clearer!

If I where to rewrite it, I would probably "point-free-fy it" with number = clean >=> check, but it has the inconvenience of an additional import.

Does it type-checks?


check :: String -> Maybe String
check ('0':_) = Nothing
check ('1':_) = Nothing
check (_:_:_:'0':_) = Nothing
check (_:_:_:'1':_) = Nothing
check s = Just s

clean :: String -> Maybe String
clean input
| len == 10 = Just digits
| len == 11 && head digits == '1' = Just $ tail digits
| otherwise = Nothing
Expand Down
2 changes: 1 addition & 1 deletion exercises/phone-number/package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: phone-number
version: 1.0.0.2
version: 1.2.0.3

dependencies:
- base
Expand Down
38 changes: 23 additions & 15 deletions exercises/phone-number/test/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,34 @@ data Case = Case { description :: String
cases :: [Case]
cases =
[ Case { description = "cleans the number"
, input = "(123) 456-7890"
, expected = Just "1234567890"
, input = "(223) 456-7890"
, expected = Just "2234567890"
}
, Case { description = "cleans number with dots"
, input = "123.456.7890"
, expected = Just "1234567890"
, Case { description = "cleans numbers with dots"
, input = "223.456.7890"
, expected = Just "2234567890"
}
, Case { description = "cleans numbers with multiple spaces"
, input = "123 456 7890 "
, expected = Just "1234567890"
, input = "223 456 7890 "
, expected = Just "2234567890"
}
, Case { description = "invalid when 9 digits"
, input = "123456789"
, expected = Nothing
}
, Case { description = "invalid when 11 digits"
, input = "21234567890"
, Case { description = "invalid when 11 digits does not start with a 1"
, input = "22234567890"
, expected = Nothing
}
, Case { description = "valid when 11 digits and first is 1"
, input = "11234567890"
, expected = Just "1234567890"
, Case { description = "valid when 11 digits and starting with 1"
, input = "12234567890"
, expected = Just "2234567890"
}
, Case { description = "valid when 11 digits and starting with 1 even with punctuation"
, input = "+1 (223) 456-7890"
, expected = Just "2234567890"
}
, Case { description = "invalid when 12 digits"
, Case { description = "invalid when more than 11 digits"
, input = "321234567890"
, expected = Nothing
}
Expand All @@ -58,8 +62,12 @@ cases =
, input = "123-@:!-7890"
, expected = Nothing
}
, Case { description = "invalid with right number of digits but letters mixed in"
, input = "1a2b3c4d5e6f7g8h9i0j"
, Case { description = "invalid if area code does not start with 2-9"
, input = "(123) 456-7890"
, expected = Nothing
}
, Case { description = "invalid if exchange code does not start with 2-9"
, input = "(223) 056-7890"
, expected = Nothing
}
]