Hi all,
I am slowing advancing with my Haskell skills, and I was able to the read yaml files and extract information from them according to a given custom data type. However, when I try to more some stuff inside a main = do to a function to make more compact the code there are some problems that indicated that probably I don’t understand the IO ().
This piece of code is working
{-# LANGUAGE DeriveGeneric #-}
import GHC.Generics
import Data.Yaml
import qualified Data.ByteString.Lazy as BSL
import GHC.Exception.Type
import Data.Typeable
import qualified Data.Text as DT
data Dataset = Dataset {
dataName :: DT.Text
, dataKind :: DT.Text
} deriving (Show, Generic)
instance FromJSON Dataset
main = do
let fp = "test_single.yaml"
ymlData <- BSL.readFile fp
let mydata = decodeThrow (BSL.toStrict ymlData) :: Either SomeException [Dataset]
result <-
case mydata of
Left exc -> error $ "Could not parse: " ++ show exc
Right mydata -> return mydata
print result
However, when I try to create a function like:
parseDataset mydataset = case mydataset of
Left exc -> error $ "Could not parse: " ++ show exc
Right mydataset -> return mydataset
readDatasets fname = parseDataset $ decodeThrow (BSL.toStrict yamldata) :: Either SomeException [Dataset]
where yamldata = BSL.readFile fname
main = do
print $ readDatasets "test_single.yaml"
Then it’s not working.
Well, I tried <$>
or fmap
but it seems that I don’t know how to implement them when I have to indicate the type with :: Either SomeException [Dataset]
Probably, it is a lack of understanding of the IO () from my side. So I appreciate a lot any help with this piece of code, or any reference to read about.
Thanks!!
Note the feedback of the error is
• Couldn't match expected type ‘BSL.ByteString’
with actual type ‘IO BSL.ByteString’
• In the first argument of ‘BSL.toStrict’, namely ‘yamldata’
In the first argument of ‘decodeThrow’, namely
‘(BSL.toStrict yamldata)’
In the second argument of ‘($)’, namely
‘decodeThrow (BSL.toStrict yamldata)’