AJChapman / formatting

Format strings type-safely with combinators
BSD 3-Clause "New" or "Revised" License
164 stars 39 forks source link

Added ‘char’ (c) and ‘asInt’ instances formatting Enum #3

Closed Icelandjack closed 10 years ago

Icelandjack commented 10 years ago

Simple character formatter and a formatter that formats values as an Int using fromEnum:

ghci> mapM_ TIO.putStrLn [ format (char % " has value " % asInt) ch ch | ch <- "abc" ]
a has value 97
b has value 98
c has value 99

As a vague motivator the Rust tutorial has an examples of formatting using what is effectively fromEnum:

println!( "North => {}", North as int );

which would be:

ghci> data Direction = South | North | East | West deriving Enum
ghci> format ("North => " % asInt) North
"North => 1"
chrisdone commented 10 years ago

Good one, that Char was missing never occurred to me. I'll merge this and the other one in a bit, have to go shopping, back in 40~.

chrisdone commented 10 years ago

Rebased and merged.