{-# language DeriveGeneric #-}
module FlexTask.InputTypes (
MultipleChoice(..),
Hidden(..),
SingleInputList(..),
SingleChoiceSelection,
singleChoiceAnswer,
singleChoiceEmpty,
getAnswer,
getAnswerAsIndex,
MultipleChoiceSelection,
multipleChoiceAnswer,
multipleChoiceEmpty,
getAnswers,
getAnswersAsIndices,
) where
import Data.List.Extra (nubSort)
import GHC.Generics (Generic)
import Yesod (PathPiece(..))
newtype MultipleChoice a = MultipleChoice
{ forall a. MultipleChoice a -> [a]
getChoices :: [a]
} deriving (MultipleChoice a -> MultipleChoice a -> Bool
(MultipleChoice a -> MultipleChoice a -> Bool)
-> (MultipleChoice a -> MultipleChoice a -> Bool)
-> Eq (MultipleChoice a)
forall a. Eq a => MultipleChoice a -> MultipleChoice a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => MultipleChoice a -> MultipleChoice a -> Bool
== :: MultipleChoice a -> MultipleChoice a -> Bool
$c/= :: forall a. Eq a => MultipleChoice a -> MultipleChoice a -> Bool
/= :: MultipleChoice a -> MultipleChoice a -> Bool
Eq, (forall x. MultipleChoice a -> Rep (MultipleChoice a) x)
-> (forall x. Rep (MultipleChoice a) x -> MultipleChoice a)
-> Generic (MultipleChoice a)
forall x. Rep (MultipleChoice a) x -> MultipleChoice a
forall x. MultipleChoice a -> Rep (MultipleChoice a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (MultipleChoice a) x -> MultipleChoice a
forall a x. MultipleChoice a -> Rep (MultipleChoice a) x
$cfrom :: forall a x. MultipleChoice a -> Rep (MultipleChoice a) x
from :: forall x. MultipleChoice a -> Rep (MultipleChoice a) x
$cto :: forall a x. Rep (MultipleChoice a) x -> MultipleChoice a
to :: forall x. Rep (MultipleChoice a) x -> MultipleChoice a
Generic, Int -> MultipleChoice a -> ShowS
[MultipleChoice a] -> ShowS
MultipleChoice a -> String
(Int -> MultipleChoice a -> ShowS)
-> (MultipleChoice a -> String)
-> ([MultipleChoice a] -> ShowS)
-> Show (MultipleChoice a)
forall a. Show a => Int -> MultipleChoice a -> ShowS
forall a. Show a => [MultipleChoice a] -> ShowS
forall a. Show a => MultipleChoice a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> MultipleChoice a -> ShowS
showsPrec :: Int -> MultipleChoice a -> ShowS
$cshow :: forall a. Show a => MultipleChoice a -> String
show :: MultipleChoice a -> String
$cshowList :: forall a. Show a => [MultipleChoice a] -> ShowS
showList :: [MultipleChoice a] -> ShowS
Show)
newtype Hidden a = Hidden {forall a. Hidden a -> a
getHidden :: a} deriving (Hidden a -> Hidden a -> Bool
(Hidden a -> Hidden a -> Bool)
-> (Hidden a -> Hidden a -> Bool) -> Eq (Hidden a)
forall a. Eq a => Hidden a -> Hidden a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => Hidden a -> Hidden a -> Bool
== :: Hidden a -> Hidden a -> Bool
$c/= :: forall a. Eq a => Hidden a -> Hidden a -> Bool
/= :: Hidden a -> Hidden a -> Bool
Eq,Int -> Hidden a -> ShowS
[Hidden a] -> ShowS
Hidden a -> String
(Int -> Hidden a -> ShowS)
-> (Hidden a -> String) -> ([Hidden a] -> ShowS) -> Show (Hidden a)
forall a. Show a => Int -> Hidden a -> ShowS
forall a. Show a => [Hidden a] -> ShowS
forall a. Show a => Hidden a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> Hidden a -> ShowS
showsPrec :: Int -> Hidden a -> ShowS
$cshow :: forall a. Show a => Hidden a -> String
show :: Hidden a -> String
$cshowList :: forall a. Show a => [Hidden a] -> ShowS
showList :: [Hidden a] -> ShowS
Show)
instance PathPiece a => PathPiece (Hidden a) where
fromPathPiece :: Text -> Maybe (Hidden a)
fromPathPiece = (a -> Hidden a) -> Maybe a -> Maybe (Hidden a)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Hidden a
forall a. a -> Hidden a
Hidden (Maybe a -> Maybe (Hidden a))
-> (Text -> Maybe a) -> Text -> Maybe (Hidden a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Maybe a
forall s. PathPiece s => Text -> Maybe s
fromPathPiece
toPathPiece :: Hidden a -> Text
toPathPiece = a -> Text
forall s. PathPiece s => s -> Text
toPathPiece (a -> Text) -> (Hidden a -> a) -> Hidden a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Hidden a -> a
forall a. Hidden a -> a
getHidden
newtype SingleInputList a = SingleInputList {forall a. SingleInputList a -> [a]
getList :: [a]} deriving (SingleInputList a -> SingleInputList a -> Bool
(SingleInputList a -> SingleInputList a -> Bool)
-> (SingleInputList a -> SingleInputList a -> Bool)
-> Eq (SingleInputList a)
forall a. Eq a => SingleInputList a -> SingleInputList a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => SingleInputList a -> SingleInputList a -> Bool
== :: SingleInputList a -> SingleInputList a -> Bool
$c/= :: forall a. Eq a => SingleInputList a -> SingleInputList a -> Bool
/= :: SingleInputList a -> SingleInputList a -> Bool
Eq,Int -> SingleInputList a -> ShowS
[SingleInputList a] -> ShowS
SingleInputList a -> String
(Int -> SingleInputList a -> ShowS)
-> (SingleInputList a -> String)
-> ([SingleInputList a] -> ShowS)
-> Show (SingleInputList a)
forall a. Show a => Int -> SingleInputList a -> ShowS
forall a. Show a => [SingleInputList a] -> ShowS
forall a. Show a => SingleInputList a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> SingleInputList a -> ShowS
showsPrec :: Int -> SingleInputList a -> ShowS
$cshow :: forall a. Show a => SingleInputList a -> String
show :: SingleInputList a -> String
$cshowList :: forall a. Show a => [SingleInputList a] -> ShowS
showList :: [SingleInputList a] -> ShowS
Show)
newtype SingleChoiceSelection = SingleChoiceSelection
{SingleChoiceSelection -> Int
getAnswer :: Int
} deriving (Int -> SingleChoiceSelection -> ShowS
[SingleChoiceSelection] -> ShowS
SingleChoiceSelection -> String
(Int -> SingleChoiceSelection -> ShowS)
-> (SingleChoiceSelection -> String)
-> ([SingleChoiceSelection] -> ShowS)
-> Show SingleChoiceSelection
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SingleChoiceSelection -> ShowS
showsPrec :: Int -> SingleChoiceSelection -> ShowS
$cshow :: SingleChoiceSelection -> String
show :: SingleChoiceSelection -> String
$cshowList :: [SingleChoiceSelection] -> ShowS
showList :: [SingleChoiceSelection] -> ShowS
Show,SingleChoiceSelection -> SingleChoiceSelection -> Bool
(SingleChoiceSelection -> SingleChoiceSelection -> Bool)
-> (SingleChoiceSelection -> SingleChoiceSelection -> Bool)
-> Eq SingleChoiceSelection
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SingleChoiceSelection -> SingleChoiceSelection -> Bool
== :: SingleChoiceSelection -> SingleChoiceSelection -> Bool
$c/= :: SingleChoiceSelection -> SingleChoiceSelection -> Bool
/= :: SingleChoiceSelection -> SingleChoiceSelection -> Bool
Eq,(forall x. SingleChoiceSelection -> Rep SingleChoiceSelection x)
-> (forall x. Rep SingleChoiceSelection x -> SingleChoiceSelection)
-> Generic SingleChoiceSelection
forall x. Rep SingleChoiceSelection x -> SingleChoiceSelection
forall x. SingleChoiceSelection -> Rep SingleChoiceSelection x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. SingleChoiceSelection -> Rep SingleChoiceSelection x
from :: forall x. SingleChoiceSelection -> Rep SingleChoiceSelection x
$cto :: forall x. Rep SingleChoiceSelection x -> SingleChoiceSelection
to :: forall x. Rep SingleChoiceSelection x -> SingleChoiceSelection
Generic)
getAnswerAsIndex :: SingleChoiceSelection -> Int
getAnswerAsIndex :: SingleChoiceSelection -> Int
getAnswerAsIndex = Int -> Int -> Int
forall a. Num a => a -> a -> a
subtract Int
1 (Int -> Int)
-> (SingleChoiceSelection -> Int) -> SingleChoiceSelection -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SingleChoiceSelection -> Int
getAnswer
{-# DEPRECATED singleChoiceEmpty
"This function only existed to satisfy a legacy interface in Autotool. It will be removed in a future version."
#-}
singleChoiceEmpty :: SingleChoiceSelection
singleChoiceEmpty :: SingleChoiceSelection
singleChoiceEmpty = Int -> SingleChoiceSelection
singleChoiceAnswer Int
0
singleChoiceAnswer :: Int -> SingleChoiceSelection
singleChoiceAnswer :: Int -> SingleChoiceSelection
singleChoiceAnswer = Int -> SingleChoiceSelection
SingleChoiceSelection
type MultipleChoiceSelection = MultipleChoice SingleChoiceSelection
getAnswers :: MultipleChoiceSelection -> [Int]
getAnswers :: MultipleChoiceSelection -> [Int]
getAnswers = (SingleChoiceSelection -> Int) -> [SingleChoiceSelection] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map SingleChoiceSelection -> Int
getAnswer ([SingleChoiceSelection] -> [Int])
-> (MultipleChoiceSelection -> [SingleChoiceSelection])
-> MultipleChoiceSelection
-> [Int]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MultipleChoiceSelection -> [SingleChoiceSelection]
forall a. MultipleChoice a -> [a]
getChoices
getAnswersAsIndices :: MultipleChoiceSelection -> [Int]
getAnswersAsIndices :: MultipleChoiceSelection -> [Int]
getAnswersAsIndices = (Int -> Int) -> [Int] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map (Int -> Int -> Int
forall a. Num a => a -> a -> a
subtract Int
1) ([Int] -> [Int])
-> (MultipleChoiceSelection -> [Int])
-> MultipleChoiceSelection
-> [Int]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MultipleChoiceSelection -> [Int]
getAnswers
multipleChoiceEmpty :: MultipleChoiceSelection
multipleChoiceEmpty :: MultipleChoiceSelection
multipleChoiceEmpty = [SingleChoiceSelection] -> MultipleChoiceSelection
forall a. [a] -> MultipleChoice a
MultipleChoice []
multipleChoiceAnswer :: [Int] -> MultipleChoiceSelection
multipleChoiceAnswer :: [Int] -> MultipleChoiceSelection
multipleChoiceAnswer = [SingleChoiceSelection] -> MultipleChoiceSelection
forall a. [a] -> MultipleChoice a
MultipleChoice ([SingleChoiceSelection] -> MultipleChoiceSelection)
-> ([Int] -> [SingleChoiceSelection])
-> [Int]
-> MultipleChoiceSelection
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> SingleChoiceSelection) -> [Int] -> [SingleChoiceSelection]
forall a b. (a -> b) -> [a] -> [b]
map Int -> SingleChoiceSelection
singleChoiceAnswer ([Int] -> [SingleChoiceSelection])
-> ([Int] -> [Int]) -> [Int] -> [SingleChoiceSelection]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Int] -> [Int]
forall a. Ord a => [a] -> [a]
nubSort