https://github.com/konn/safe-printf
Well-typed, flexible and variadic printf in Haskell.
https://github.com/konn/safe-printf
Last synced: about 1 month ago
JSON representation
Well-typed, flexible and variadic printf in Haskell.
- Host: GitHub
- URL: https://github.com/konn/safe-printf
- Owner: konn
- License: other
- Created: 2015-06-10T06:36:10.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2015-06-10T14:45:32.000Z (almost 10 years ago)
- Last Synced: 2025-03-24T21:38:44.391Z (about 2 months ago)
- Language: Haskell
- Size: 141 KB
- Stars: 6
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
safe-printf -- Well-typed, variadic and flexible printf functions for Haskell
=============================================================================[](https://travis-ci.org/konn/safe-printf)
[](http://hackage.haskell.org/package/safe-printf)## What is this?
Haskell's standard `Text.Printf` module provides variadic `printf` function but not type-safe.
This library provides an alternative for this, more type-safe version of `printf` function,
combinators and quasiquoters.The current implementation is just a proof-of-concept, so it is not so efficient and provides
APIs only for `String` value generation. In future, we will support `Text`
types and improve the effiiciency.## Install
```sh
$ git clone https://github.com/konn/safe-printf.git
$ cd safe-printf
$ cabal install
```## Usage
We provide two interfaces to construct format: smart constructors and quasiquoters.### Smart constructors
You need `OverloadedStrings` extension.```haskell
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Text.Printf.Safe (printf, (%), (><))
import Text.Printf.Safe.Combinators (b', d, _S)
main = do
putStrLn $ printf ("1 + 2 = " %d >< " and 0 == 1 is " %_S >< "." ) (1 + 2) (0 == 1)
putStrLn $ printf ("42 is " % b' '0' 10 >< "in binary.") 42
putStrLn $ printf ("48% of people answers that the negation of True is" %(show . not) >< ".") True
```### Quasiquote interface
Quiasiquote interface provides more readable way for generating formats.```haskell
{-# LANGUAGE QuasiQuotes #-}
module Main where
import Text.Printf.Safe (printf, fmt)
main = do
putStrLn $ printf [fmt|1 + 2 = %d and 0 == 1 is %S.|] (1 + 2) (0 == 1)
putStrLn $ printf [fmt|42 is %010b in binary.|] 42
putStrLn $ printf [fmt|48%% of people answers that the negation of True is %{show . not}.|] True
```## TODO
* Support `Text` and perhaps `Builder`.
* Improve efficiency.
* Provide IO functions?## Licence
BSD3
## Copyright
(c) Hiromi ISHII 2015