@hackage / polyvariadic

Creation and application of polyvariadic functions

Latest0.3.0.4

About

Metadata

  • Last updated , by fgaz
  • License BSD-3-Clause
  • Maintained by: Francesco Gazzetta <fgaz@fgaz.me>

  • Lottery factor: 0

Links

Installation

Tested Compilers

  1. 9.2.2
  2. 9.0.2
  3. 8.10.2
  4. 8.8.2
  5. 8.6.5
  6. 8.4.4
  7. 8.2.2
  8. 8.0.2
  9. 7.10.3

Readme

polyvariadic

Creation and application of polyvariadic functions

Build Status Hackage

For example, the classic printf:

{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
import Data.Function.Polyvariadic
import Data.Accumulator

magicChar = '%'
notMagicChar = (/= magicChar)

data PrintfAccum = PrintfAccum { done :: String, todo :: String }

instance Show x => Accumulator PrintfAccum x where
  accumulate x (PrintfAccum done (_:todo)) = PrintfAccum
                                              (done ++ show x ++ takeWhile notMagicChar todo)
                                              (dropWhile notMagicChar todo)
  accumulate _ acc = acc

printf' str = polyvariadic
               (PrintfAccum (takeWhile notMagicChar str) (dropWhile notMagicChar str))
               done
>>> printf' "aaa%bbb%ccc%ddd" "TEST" 123 True
"aaa\"TEST\"bbb123cccTrueddd"