From 56b29314adb88905acb247202bdb23b3fc7f8caf Mon Sep 17 00:00:00 2001 From: Marty Sluijtman Date: Mon, 13 Nov 2023 23:49:04 +0100 Subject: [PATCH] Bits of optimisation --- app/Main.hs | 2 +- flake.nix | 1 - lib/DND/Sheet/Parser.hs | 29 +++++++++++++++-------------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index 73e1297..0f7b19c 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -10,4 +10,4 @@ main = do createExample testfile sheet <- parseSheet testfile putStrLn $ "wrote example character named \"" ++ getName sheet ++ "\" to: " ++ testfile - listSkills sheet \ No newline at end of file + listSkillNames sheet diff --git a/flake.nix b/flake.nix index d540be6..0390e42 100644 --- a/flake.nix +++ b/flake.nix @@ -20,7 +20,6 @@ jailbreakUnbreak = pkg: pkgs.haskell.lib.doJailbreak (pkg.overrideAttrs (_: { meta = { }; })); - # DON'T FORGET TO PUT YOUR PACKAGE NAME HERE, REMOVING `throw` packageName = "sheet-parser-hs"; in { packages.${packageName} = diff --git a/lib/DND/Sheet/Parser.hs b/lib/DND/Sheet/Parser.hs index eee84ae..1be1bbb 100644 --- a/lib/DND/Sheet/Parser.hs +++ b/lib/DND/Sheet/Parser.hs @@ -1,35 +1,36 @@ module DND.Sheet.Parser -(getSheet -, parseSheet +( parseSheet +--, getSheet , listSkills , createExample , getName +, listSkillNames ) where import Data.Aeson import DND.Sheet.Content import DND.Bob(bob) +import Control.Monad import qualified Data.ByteString.Lazy as B import qualified Data.ByteString.Lazy.UTF8 as BSU import Data.Maybe (fromJust) -getSheet :: FilePath -> IO B.ByteString -getSheet = B.readFile +--getSheet :: FilePath -> IO B.ByteString +--getSheet = B.readFile parseSheet :: FilePath -> IO Character -parseSheet x = do - file <- B.readFile x - let character = decode file :: Maybe Character - return $ fromJust character +parseSheet x = unwrap . decode =<< B.readFile x + where unwrap (Just y) = return y + unwrap Nothing = error $ "Failed to parse character sheet: " ++ x -listSkills :: Character -> IO () -listSkills = mapM_ (mapM_ putStrLn) - . filter (not . null) - . map (map skillName . skills) - . stats +listSkills :: Character -> [Skill] +listSkills = join . map skills . stats + +listSkillNames :: Character -> IO () +listSkillNames = mapM_ (putStrLn . skillName) . listSkills createExample :: FilePath -> IO () createExample = flip encodeFile bob getName :: Character -> String -getName = charName . preamble \ No newline at end of file +getName = charName . preamble