Int
मूल्यों के विशेष मामले के लिए, यहाँ उन्हें एक मध्यवर्ती चरण में Strings
का उपयोग किए बिना सख्त Text
मूल्यों में परिवर्तित करने के कोड है:
import Data.Text
import Data.Text.Lazy (toStrict)
import Data.Text.Lazy.Builder (toLazyText)
import Data.Text.Lazy.Builder.Int (decimal)
showIntegral :: Integral a => a -> T.Text
showIntegral = toStrict. toLazyText . decimal
मॉड्यूल Data.Text.Lazy.Builder.RealFloat
चल बिन्दु मूल्यों के लिए इसी तरह की सुविधा प्रदान करता है।
import Data.Text
import Data.Text.Lazy (toStrict)
import Data.Text.Lazy.Builder (toLazyText)
import Data.Text.Lazy.Builder.Int (decimal)
import Data.Text.Lazy.Builder.RealFloat (realFloat)
class ShowText a where
showText :: a -> Text
instance ShowText Int where
showText = toStrict . toLazyText . decimal
instance ShowText Float where
showText = toStrict . toLazyText . realFloat
फिर हम अधिक उदाहरणों को जोड़कर शुरू कर सकते हैं (tuples के लिए एक उदाहरण के लिए उपयोगी होगा):
इन के साथ हम Show
typeclass के हमारे अपने संस्करण परिभाषित कर सकते हैं।
http://hackage.haskell.org/packages/archive/bytestring-show/0.3.5.1/doc/html/Text-Show-ByteString.html – applicative