The safe-printf package

[ Tags: bsd3, library, text ] [ Propose Tags ]

More type-safe, flexible replacement of variadic printf for Text.Printf.


[Skip to Readme]

Properties

Versions 0.1.0.0
Dependencies base (==4.*), haskell-src-meta (==0.6.*), template-haskell (>=2.9 && <2.11), th-lift (>=0.6 && <0.8) [details]
License BSD3
Copyright (c) Hiromi ISHII 2015
Author Hiromi ISHII
Maintainer konn.jinro_at_gmail.com
Category Text
Home page https://github.com/konn/safe-printf
Source repository head: git clone git://github.com/konn/safe-printf.git
Uploaded Wed Jun 10 14:43:39 UTC 2015 by HiromiIshii
Distributions NixOS:0.1.0.0
Downloads 196 total (17 in the last 30 days)
Rating (no votes yet) [estimated by rule of succession]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2015-06-11 [all 1 reports]
Hackage Matrix CI

Modules

[Index]

Downloads

Maintainer's Corner

For package maintainers and hackage trustees


Readme for safe-printf-0.1.0.0

[back to package description]

safe-printf -- Well-typed, variadic and flexible printf functions for Haskell

Build Status loop-effin

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

$ git clone https://github.com/konn/safe-printf.git
$ cd safe-printf
$ cabal install
<!-- ```sh $ cabal install safe-printf ``` -->

Usage

We provide two interfaces to construct format: smart constructors and quasiquoters.

Smart constructors

You need OverloadedStrings extension.

{-# 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.

{-# 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