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

Simplify rev #1139

Open
yaxu opened this issue Feb 4, 2025 · 3 comments
Open

Simplify rev #1139

yaxu opened this issue Feb 4, 2025 · 3 comments

Comments

@yaxu
Copy link
Member

yaxu commented Feb 4, 2025

I think @polymorphicengine pointed out that rev could just negate time, rather than the complex mirroring it's doing now.

Here's an implementation of that:

negateArc (Arc b e) = Arc (negate e) (negate b)
rev' = withQueryArc negateArc . withResultArc negateArc

Can we drop this in or does it have different behaviour? I think it's similar but the cycles are also in reverse order?

for comparison:

rev :: Pattern a -> Pattern a
rev p =
keepMeta p $
splitQueries $
p
{ query = \st ->
map makeWholeAbsolute $
mapParts (mirrorArc (midCycle $ arc st)) $
map
makeWholeRelative
( query
p
st
{ arc = mirrorArc (midCycle $ arc st) (arc st)
}
)
}
where
makeWholeRelative :: Event a -> Event a
makeWholeRelative e@Event {whole = Nothing} = e
makeWholeRelative (Event c (Just (Arc s e)) p'@(Arc s' e') v) =
Event c (Just $ Arc (s' - s) (e - e')) p' v
makeWholeAbsolute :: Event a -> Event a
makeWholeAbsolute e@Event {whole = Nothing} = e
makeWholeAbsolute (Event c (Just (Arc s e)) p'@(Arc s' e') v) =
Event c (Just $ Arc (s' - e) (e' + s)) p' v
midCycle :: Arc -> Time
midCycle (Arc s _) = sam s + 0.5
mapParts :: (Arc -> Arc) -> [Event a] -> [Event a]
mapParts f es = (\(Event c w p' v) -> Event c w (f p') v) <$> es
-- Returns the `mirror image' of a 'Arc' around the given point in time
mirrorArc :: Time -> Arc -> Arc
mirrorArc mid' (Arc s e) = Arc (mid' - (e - mid')) (mid' + (mid' - s))

@sss-create
Copy link
Collaborator

A quick test worked great! Wow, what a simplification.

@polymorphicengine
Copy link
Collaborator

i guess the difference comes in once a pattern is longer than one cycle:
currently rev <[1 2] [3 4]> == <[2 1] [4 3]>, but with the above (i think) it would be <[4 3] [2 1]>, so yes the cycles are also in reverse order

i think both behaviors are useful

@yaxu
Copy link
Member Author

yaxu commented Feb 6, 2025

Yes we could call the new one something like revall or revpat

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants