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

Generalize andM and similar functions #35

Closed
gromakovsky opened this issue Mar 7, 2017 · 4 comments
Closed

Generalize andM and similar functions #35

gromakovsky opened this issue Mar 7, 2017 · 4 comments

Comments

@gromakovsky
Copy link
Member

Currently they only work on lists, but can be generalized I suppose.

@chshersh
Copy link
Contributor

chshersh commented Mar 7, 2017

Okay, seems like those functions indeed can be generalized. This seems to work:

λ: let andM = foldr (\elem rest -> elem >>= \b -> if b then rest else pure False) (pure True) 
λ: :t andM
andM :: (Foldable t, Monad m) => t (m Bool) -> m Bool
λ: andM [Just True, Just False]
Just False
λ: andM [Just True, Just False, Nothing]
Just False
λ: andM [Just True, Nothing, Just False]
Nothing
λ: andM [putStrLn "1" >> pure True, putStrLn "2" >> pure False, putStrLn "3" >> undefined]
1
2
False

But don't know how it behaves with space leaks. Using for instead of whenJust introduces spaces leaks. That's why we have whenJust specialization. So I'm more cautious after that bug with such general definitions. @neongreen Could you check this definition on space-leakiness?

Also I don't know whether it's a good idea:

λ: andM (Just [True, False])
[ True , False ]
λ: andM (Just [False])
[ False ]
λ: andM (Just [False])

I expect some bugs after converting [Maybe Bool] into Maybe [Bool].

@neongreen
Copy link
Contributor

I expect some bugs after converting [Maybe Bool] into Maybe [Bool]

This is not an issue if we use NontrivialContainer instead of Foldable.

Using for instead of whenJust introduces spaces leaks.

Instead of your definition I would just use the original list definition together with toList, to be safe.

@gromakovsky
Copy link
Member Author

The only drawback is that version with toList is probably less efficient in some cases.

@chshersh
Copy link
Contributor

chshersh commented Mar 9, 2017

We have #21 for that ;)
But I don't expect noticeable performance degrade.

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