Skip to content

Instantly share code, notes, and snippets.

@wuct
Last active August 15, 2018 03:26
Show Gist options
  • Save wuct/358438133d0e1f66b23b916cea0be95f to your computer and use it in GitHub Desktop.
Save wuct/358438133d0e1f66b23b916cea0be95f to your computer and use it in GitHub Desktop.
PureScript printf using instance chain
module Main where
import Prelude
import Effect (Effect)
import Effect.Class.Console (log)
class Printf r
where printf :: String -> r
instance printfString :: Printf String
where printf = identity
else instance printfStringFunc :: Printf r => Printf (String -> r)
where printf a b = printf $ a <> " " <> b
else instance printfFunc :: (Show a, Printf r) => Printf (a -> r)
where printf s b = printf $ s <> " " <> show b
printf' :: a. Printf a => a
printf' = printf ""
main :: Effect Unit
main = do
log $ printf' 1 "a" 123 2 "b" { a: 123 }
@wuct
Copy link
Author

wuct commented Aug 15, 2018

I was trying to remove line:17-18 but failed, here is what I came up with:

class Print a r | a -> r
  where print :: a -> r

instance printString :: Print String String
  where print = identity
else instance printFunc :: Print a r => Print (String -> a) (String -> r) 
  where print a b = print $ a <> b -- won't compile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment