-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractScore.hs
More file actions
71 lines (54 loc) · 2.54 KB
/
AbstractScore.hs
File metadata and controls
71 lines (54 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
-- This file is part of KSQuant2.
-- Copyright (c) 2010 - 2011, Kilian Sprotte. All rights reserved.
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
module AbstractScore (Score(scoreParts)
, Part(partVoices)
, Voice(voiceItems)
, singleVoices2Score
, score2singlePartVoices)
where
import qualified Lisp as L (Sexp
, toSexp
, fromSexp
, LispVal(LispList)
, mapcar'
, mapcarUpToPlist
, fromLispList
, nil)
data Score a = Score { scoreParts :: [Part a] } deriving (Eq,Show)
data Part a = Part { partVoices :: [Voice a], partAttributes :: L.LispVal } deriving (Eq,Show)
data Voice a = Voice { voiceItems :: a } deriving (Eq,Show)
-- Functor
instance Functor Score where
fmap f x = Score $ map (fmap f) (scoreParts x)
instance Functor Part where
fmap f x = x { partVoices = map (fmap f) (partVoices x) }
instance Functor Voice where
fmap f x = x { voiceItems = f . voiceItems $ x }
-- Sexp
instance (L.Sexp a) => L.Sexp (Score a) where
toSexp s = L.LispList $ map L.toSexp (scoreParts s)
fromSexp = Score . L.mapcar' L.fromSexp
instance (L.Sexp a) => L.Sexp (Part a) where
toSexp s = L.LispList $ map L.toSexp (partVoices s) ++ L.fromLispList (partAttributes s)
fromSexp = uncurry Part . L.mapcarUpToPlist L.fromSexp
instance (L.Sexp a) => L.Sexp (Voice a) where
toSexp = L.toSexp . voiceItems
fromSexp = Voice . L.fromSexp
singleVoices2Score :: [a] -> Score a
singleVoices2Score a = Score { scoreParts = map p a }
where p v = Part { partVoices = [Voice v]
, partAttributes = L.nil }
partSingleVoice :: Part a -> a
partSingleVoice p = voiceItems . head $ partVoices p
score2singlePartVoices :: Score a -> [a]
score2singlePartVoices s = map partSingleVoice $ scoreParts s