Skip to content

Instantly share code, notes, and snippets.

@khibino
Created August 2, 2022 04:06
Show Gist options
  • Save khibino/41fa968f994f1c6a2fd6cef670829d58 to your computer and use it in GitHub Desktop.
Save khibino/41fa968f994f1c6a2fd6cef670829d58 to your computer and use it in GitHub Desktop.
-- printf like function, formatting package method
newtype Format r a = Format ((ShowS -> r) -> a)
string :: Format r (String -> r)
string = Format $ \k s -> k (s ++)
int :: Format r (Int -> r)
int = Format $ \k i -> k (show i ++)
(%) :: Format r a -> Format r' r -> Format r' a
Format x1 % Format x2 = Format $ \k -> x1 $ \s1 -> x2 $ \s2 -> k (s1 . s2)
infixr 9 %
lit :: String -> Format r r
lit s = Format ($ (s ++))
format :: Format String a -> a
format (Format x) = x ($ "")
exampleFmt :: Format r (String -> Int -> r)
exampleFmt = lit "example string: " % string % lit ", example int: " % int
example :: String
example = format exampleFmt "abc" 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment