module Miscellaneous( if', (?), selectFrom ) where import Control.Monad as Con import qualified Data.List as List import qualified Data.Maybe as Maybe if' :: Bool -> t -> t -> t if' a b c = if a then b else c infixr 1 ? (?) :: Bool -> t -> t -> t (?) = if' -- kinda functions like poor man's sql -- first argument is the indices of the items you want in the results -- second argument is index-item pairs to dictate what records are acceptable to select from -- third argument is the list of items that makes up the record under consideration -- then if the record was deemed acceptable you get the bits you wanted -- (note that all indices start from 1) selectFrom :: (Num t, Eq t, Eq a, Enum t) => [t] -> [(t,a)] -> [a] -> Maybe [a] selectFrom pick has from = let tailFunc r i = let check = List.lookup (fst i) has in if (Maybe.isNothing check || Maybe.fromJust check == snd i) then if (List.elem (fst i) pick) then Just (r ++ [snd i]) else Just r else Nothing in Con.foldM tailFunc [] (zip [1,2..] from)