Skip to content

Commit 9905107

Browse files
committed
Add Bounded instance
1 parent bba91ee commit 9905107

File tree

2 files changed

+52
-44
lines changed

2 files changed

+52
-44
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ instance ordChar :: Ord Char
5353

5454
Characters can be compared with `compare`, `>`, `>=`, `<` and `<=`.
5555

56+
#### `boundedChar`
57+
58+
``` purescript
59+
instance boundedChar :: Bounded Char
60+
```
61+
62+
5663
#### `showChar`
5764

5865
``` purescript
@@ -454,7 +461,4 @@ char :: String -> Char
454461

455462
Converts a string of length `1` to a character.
456463

457-
**Unsafe:** throws runtime exception if length is not `1`.
458-
459-
460-
464+
**Unsafe:** throws runtime exception if length is not `1`.

src/Data/Char/Char.purs

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,48 @@
11
-- | A type and functions for single characters.
22
module Data.Char
3-
( Char(),
4-
charString,
5-
fromCharCode,
6-
toCharCode
3+
( Char()
4+
, charString
5+
, fromCharCode
6+
, toCharCode
77
) where
88

9-
--| A unicode character.
10-
newtype Char = Char String
11-
12-
-- | Returns the string of length `1` containing only the given character.
13-
charString :: Char -> String
14-
charString (Char s) = s
15-
16-
-- | Returns the numeric Unicode value of the character.
17-
foreign import toCharCode
18-
"""
19-
function toCharCode(c) {
20-
return c.charCodeAt(0);
21-
}
22-
""" :: Char -> Number
23-
24-
-- | Constructs a character from the given Unicode numeric value.
25-
foreign import fromCharCode
26-
"""
27-
function fromCharCode(c) {
28-
return String.fromCharCode(c);
29-
}
30-
""" :: Number -> Char
31-
32-
-- | Characters can be compared for equality with `==` and `/=`.
33-
instance eqChar :: Eq Char where
34-
(==) (Char a) (Char b) = a == b
35-
36-
(/=) a b = not (a == b)
37-
38-
-- | Characters can be compared with `compare`, `>`, `>=`, `<` and `<=`.
39-
instance ordChar :: Ord Char where
40-
compare (Char a) (Char b) = a `compare` b
41-
42-
-- | Characters can be rendered as a string with `show`.
43-
instance showChar :: Show Char where
44-
show (Char s) = "Char " ++ show s
9+
--| A unicode character.
10+
newtype Char = Char String
11+
12+
-- | Returns the string of length `1` containing only the given character.
13+
charString :: Char -> String
14+
charString (Char s) = s
15+
16+
-- | Returns the numeric Unicode value of the character.
17+
foreign import toCharCode
18+
"""
19+
function toCharCode(c) {
20+
return c.charCodeAt(0);
21+
}
22+
""" :: Char -> Number
23+
24+
-- | Constructs a character from the given Unicode numeric value.
25+
foreign import fromCharCode
26+
"""
27+
function fromCharCode(c) {
28+
return String.fromCharCode(c);
29+
}
30+
""" :: Number -> Char
31+
32+
-- | Characters can be compared for equality with `==` and `/=`.
33+
instance eqChar :: Eq Char where
34+
(==) (Char a) (Char b) = a == b
35+
36+
(/=) a b = not (a == b)
37+
38+
-- | Characters can be compared with `compare`, `>`, `>=`, `<` and `<=`.
39+
instance ordChar :: Ord Char where
40+
compare (Char a) (Char b) = a `compare` b
41+
42+
instance boundedChar :: Bounded Char where
43+
top = fromCharCode 0
44+
bottom = fromCharCode 65535
45+
46+
-- | Characters can be rendered as a string with `show`.
47+
instance showChar :: Show Char where
48+
show (Char s) = "Char " ++ show s

0 commit comments

Comments
 (0)