Giter Club home page Giter Club logo

to-string-haskell's Introduction

to-string

A toString converter for String-like types and any Show a => a type.

branch Travis status
master Build Status
develop Build Status

Installation

Add this library to extra-deps in your stack.yaml like the following if you use Stack.

...
extra-deps: 
- git: https://github.com/nwtgck/to-string-haskell.git
  commit: 0c8f2951606e185feacddb28983b30c527c3eb17
...

Usages

toString 89 == "89"
toString "hello" == "hello"
{-# LANGUAGE OverloadedStrings #-}
toString ("I'm a ByteString" :: BS.ByteString) == ("I'm a ByteString" :: String)
{-# LANGUAGE OverloadedStrings #-}

toString ("I'm a Text" :: T.Text) == ("I'm a Text" :: String)

Supported String-like types

  • String
  • Data.ByteString.Char8.ByteString
  • Data.ByteString.Lazy.Char8.ByteString
  • Data.ByteString.Short.ShortByteString
  • Data.ByteString.UTF8.ByteString
  • Data.ByteString.Lazy.UTF8.ByteString
  • Data.Text.Text
  • Data.Text.Lazy.Text

Any Show a => a

Any Show a => a type is also an instance of type class ToString.
So Int, Double, Maybe a ... which are Show instances are also instances of ToString

Executable example

{-# LANGUAGE OverloadedStrings #-}

import Data.String.ToString
import qualified Data.ByteString as BS
import qualified Data.Text as T

-- (This is an orignal type deriving `Show`)
data MyMaybe a = 
    MyJust a
  | MyNothing
  deriving Show

main :: IO ()
main = do
  let i :: Int
      i = 10
  putStrLn (toString i)
  -- => 10

  let ch :: Char
      ch = 'd'
  putStrLn (toString ch)
  -- => 'd'

  let myMay1 :: MyMaybe Double
      myMay1 = MyJust 1.89 
  putStrLn (toString myMay1)
  -- => MyJust 1.89

  let bs :: BS.ByteString
      bs = "I'm a ByteString!"
  putStrLn (toString bs)
  -- => I'm a ByteString!

  let text :: T.Text
      text = "I'm a Text!"
  putStrLn (toString text)
  -- => I'm a Text!

Output

10
'd'
MyJust 1.89
I'm a ByteString!
I'm a Text!

to-string-haskell's People

Contributors

nwtgck avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.