-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathTypeClasses.purs
46 lines (36 loc) · 1.33 KB
/
TypeClasses.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
module Main where
import Prelude
import Effect (Effect)
import Effect.Console (log)
import TryPureScript (render, withConsole)
-- A type class for types which can be used with
-- string interpolation.
class Interpolate a where
interpolate :: a -> String
instance interpolateString :: Interpolate String where
interpolate = identity
instance interpolateInt :: Interpolate Int where
interpolate = show
-- A type class for printf functions
-- (each list of argument types will define a type class instance)
class Printf r where
printfWith :: String -> r
-- An instance for no function arguments
-- (just return the accumulated string)
instance printfString :: Printf String where
printfWith = identity
-- An instance for adding another argument whose
-- type is an instance of Interpolate
instance printfShow :: (Interpolate a, Printf r) => Printf (a -> r) where
printfWith s a = printfWith (s <> interpolate a)
-- Our generic printf function
printf :: forall r. (Printf r) => r
printf = printfWith ""
-- Now we can create custom formatters using different argument
-- types
debug :: String -> Int -> String -> String
debug uri status msg = printf "[" uri "] " status ": " msg
main :: Effect Unit
main = render =<< withConsole do
log $ debug "http://www.purescript.org" 200 "OK"
log $ debug "http://bad.purescript.org" 404 "Not found"