on master help sign in
Give a slightly not terrible error message
8349a85Huw Campbell
examples/main/shakespeare.hs+1−1
@@ -69,7 +69,7 @@ loadShakespeare :: FilePath -> ExceptT String IO (Vector Int, M.Map Char Int, Ve
69loadShakespeare path = do
69loadShakespeare path = do
70 contents <- lift $ readFile path
70 contents <- lift $ readFile path
71 let annotated = annotateCapitals contents
71 let annotated = annotateCapitals contents
72− (m,cs) <- ExceptT . return . note "Couldn't fit data in hotMap" $ hotMap (Proxy :: Proxy 40) annotated
72+ (m,cs) <- ExceptT . return $ hotMap (Proxy :: Proxy 40) annotated
73 hot <- ExceptT . return . note "Couldn't generate hot values" $ traverse (`M.lookup` m) annotated
73 hot <- ExceptT . return . note "Couldn't generate hot values" $ traverse (`M.lookup` m) annotated
74 return (V.fromList hot, m, cs)
74 return (V.fromList hot, m, cs)
75
75
src/Grenade/Utils/OneHot.hs+4−3
@@ -52,15 +52,16 @@ oneHot hot =
52
52
53-- | Create a one hot map from any enumerable.
53-- | Create a one hot map from any enumerable.
54-- Returns a map, and the ordered list for the reverse transformation
54-- Returns a map, and the ordered list for the reverse transformation
55−hotMap :: (Ord a, KnownNat n) => Proxy n -> [a] -> Maybe (Map a Int, Vector a)
55+hotMap :: (Ord a, KnownNat n) => Proxy n -> [a] -> Either String (Map a Int, Vector a)
56hotMap n as =
56hotMap n as =
57 let len = fromIntegral $ natVal n
57 let len = fromIntegral $ natVal n
58 uniq = [ c | (c:_) <- group $ sort as]
58 uniq = [ c | (c:_) <- group $ sort as]
59 hotl = length uniq
59 hotl = length uniq
60 in if hotl == len
60 in if hotl == len
61 then
61 then
62− Just (M.fromList $ zip uniq [0..], V.fromList uniq)
62+ Right (M.fromList $ zip uniq [0..], V.fromList uniq)
63− else Nothing
63+ else
64+ Left ("Couldn't create hotMap of size " ++ show len ++ " from vector with " ++ show hotl ++ " unique characters")
64
65
65-- | From a map and value, create a 1D Shape
66-- | From a map and value, create a 1D Shape
66-- with one index hot (1) with the rest 0.
67-- with one index hot (1) with the rest 0.