Skip to content

Instantly share code, notes, and snippets.

@Cmdv
Last active September 1, 2021 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cmdv/b19cf64a2290dfde91504afbf5430515 to your computer and use it in GitHub Desktop.
Save Cmdv/b19cf64a2290dfde91504afbf5430515 to your computer and use it in GitHub Desktop.
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module Main where
import RIO
import qualified RIO.Map as M
data Question = Question
{ qId :: Text
, qText :: Text
, qScore :: Int
}
deriving (Show, Eq)
data Report = Report
{ rId :: Text
, rName :: Text
, questions :: Map Text Int
}
deriving (Show, Eq)
reports' :: [Report]
reports' = [ Report "id1" "name1" mempty
, Report "id2" "name2" mempty
, Report "id3" "name3" mempty
]
questions' :: [Question]
questions' = [ Question "id1" "Q1" 10
, Question "id1" "Q2" 20
, Question "id2" "Q3" 30
, Question "id2" "Q4" 40
, Question "id3" "Q5" 50
, Question "id3" "Q5" 60
]
myfunc :: [Question] -> [Report] -> [Report]
myfunc questions oldCallsReports = do
let testMerge :: Question -> Report -> Maybe Report
testMerge Question{..} Report{..} =
if rId == qId
then Just $ Report {questions = M.insert qText qScore questions, ..}
else Nothing
t <- questions
take 1 $ mapMaybe (testMerge t) oldCallsReports
poop :: [Report]
poop = myfunc questions' reports'
main :: IO ()
main = print poop
--
-- Returns:
-- [ Report {rId = "id1", rName = "name1", questions = fromList [("Q1",10)]}
-- , Report {rId = "id1", rName = "name1", questions = fromList [("Q2",10)]}
-- , Report {rId = "id1", rName = "name1", questions = fromList [("Q3",10)]}
-- , Report {rId = "id1", rName = "name1", questions = fromList [("Q4",10)]}
-- , Report {rId = "id1", rName = "name1", questions = fromList [("Q5",10)]}
-- , Report {rId = "id1", rName = "name1", questions = fromList [("Q6",10)]}
-- ]
--
-- But expected:
-- [ Report {rId = "id1", rName = "name1", questions = fromList [("Q1",10), ("Q2",20)]}
-- , Report {rId = "id2", rName = "name1", questions = fromList [("Q3",30), ("Q4",40)]}
-- , Report {rId = "id3", rName = "name1", questions = fromList [("Q5",50), ("Q6",60)]}
-- ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment