Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lookupName function. #41

Merged
merged 1 commit into from Aug 14, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Diagrams/Core/Types.hs
Expand Up @@ -59,6 +59,7 @@ module Diagrams.Core.Types
-- ** Modifying diagrams
-- *** Names
, nameSub
, lookupName
, withName
, withNameAll
, withNames
Expand Down Expand Up @@ -276,14 +277,20 @@ nameSub :: ( IsName n
=> (QDiagram b v m -> Subdiagram b v m) -> n -> QDiagram b v m -> QDiagram b v m
nameSub s n d = over QD (D.applyUpre . inj . toDeletable $ fromNames [(n,s d)]) d

-- | Lookup the most recent diagram associated with (some
-- qualification of) the given name.
lookupName :: IsName n
=> n -> QDiagram b v m -> Maybe (Subdiagram b v m)
lookupName n d = lookupSub (toName n) (subMap d) >>= listToMaybe

-- | Given a name and a diagram transformation indexed by a
-- subdiagram, perform the transformation using the most recent
-- subdiagram associated with (some qualification of) the name,
-- or perform the identity transformation if the name does not exist.
withName :: IsName n
=> n -> (Subdiagram b v m -> QDiagram b v m -> QDiagram b v m)
-> QDiagram b v m -> QDiagram b v m
withName n f d = maybe id f (lookupSub (toName n) (subMap d) >>= listToMaybe) d
withName n f d = maybe id f (lookupName n d) d

-- | Given a name and a diagram transformation indexed by a list of
-- subdiagrams, perform the transformation using the
Expand Down