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 generalized repetition #23

Closed
rpglover64 opened this issue Feb 18, 2016 · 3 comments
Closed

Add generalized repetition #23

rpglover64 opened this issue Feb 18, 2016 · 3 comments
Milestone

Comments

@rpglover64
Copy link

matchAll :: Scraper a b -> Scraper a [b], generalizing htmls, attrs, texts, and chroots from their singular forms.

This is useful in case I want to matchAll (html "a" <* (attr "title" "a" >>= \x -> guard (somePredicate x))).

(Yes, I actually ran into this.)

many doesn't solve the problem because it's alternation; replicateM and friends don't solve the problem because each Scraper looks from the current spot.

@fimad
Copy link
Owner

fimad commented Feb 19, 2016

How would matchAll work if you passed different selectors to html and attr?

It seems like it may not make sense with different selectors and that you would need something with a type like :: Selectable s => (s -> Scraper str a) -> (s -> Scraper str b) -> s -> Scraper s [(a, b)] so that you can ensure that you are selecting on the same elements. Of course this doesn't generalize as nicely :/

Another, though less intuitive option, is to abuse chroots and Any:

chroots "a" $ do
    x <- attr "title" Any
    guard (somePredicate x)
    html Any

@rpglover64
Copy link
Author

How would matchAll work if you passed different selectors to html and attr?

Presumably, it would return the empty list, since no single element has both selectors.

I hadn't thought of the chroots trick; I think it should be added to the chroots documentation, and that may be enough.

@fimad fimad added this to the 0.3.1.1 milestone May 28, 2016
fimad added a commit that referenced this issue May 29, 2016
This commit adds the resolutions to #23 and #25 to README.md under a
new tips & tricks section. Full source code for each is given in a new
example as well.

Additionally a backtracking bug was found in `chroot` while
documenting these tricks and is fixed in this commit as well.
fimad added a commit that referenced this issue May 29, 2016
@fimad
Copy link
Owner

fimad commented May 29, 2016

Closing out for now, if you feel like the current documentation isn't extensive or visible enough please reopen.

@fimad fimad closed this as completed May 29, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants