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

Define meta and replace. #972

Open
wants to merge 1 commit into
base: 2.0-beatmode-retired
Choose a base branch
from

Conversation

JeffreyBenjaminBrown
Copy link
Contributor

@JeffreyBenjaminBrown JeffreyBenjaminBrown commented Nov 27, 2022

meta lets you apply a pattern whose elements are pattern transformations like fast 2. replace makes it easier to use meta, and probably has other uses; it lets you turn a pattern of easy-to-write symbols (integers are what I usually use) into a pattern of anything else, by providing a lookup table.

+meta :: forall a b.

  •    Pattern (Pattern a -> Pattern b) -> Pattern a -> Pattern b
    

+replace :: forall a b. Ord a =>

  •       [(a,b)] -> Pattern a -> Pattern b
    

Here's a minimal example using both.

This plays the pattern "ho*8"
at normal speed for the first half,
and at double speed for the second half.

let slang = [ (0, id)
, (1, fast 2) ]
in p 1 $ meta (replace slang "0 1") "ho*8"

  +meta :: forall a b.
  +        Pattern (Pattern a -> Pattern b) -> Pattern a -> Pattern b

  +replace :: forall a b. Ord a =>
  +           [(a,b)] -> Pattern a -> Pattern b

Here's a minimal example using both.

  This plays the pattern "ho*8"
  at normal speed for the first half,
  and at double speed for the second half.

  let slang = [ (0, id)
               , (1, fast 2) ]
     in p 1 $ meta (replace slang "0 1") "ho*8"
@JeffreyBenjaminBrown
Copy link
Contributor Author

On the forum, user @mvdirty managed to make one-liners out of both functions. His definition of meta is beautiful, and more general than mine:

meta :: Monad m => m (a -> m b) -> a -> m b
meta pf p = pf >>= ($ p)

It's simple enough that I no longer think meta deserves to exist.

His definition of replace is also beautiful, but complex enough that maybe it deserves to be defined in the codebase:

replace :: Show a => [(a, b)] -> Pattern String -> Pattern b
replace x = inhabit $ bimap show pure <$> x

The code I submitted is basically irrelevant at this point, but I'm leaving this pull request open just for discussion's sake.

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

Successfully merging this pull request may close these issues.

1 participant