In #haskell, is there a version of "bracket" that works on lists? Say I give it a list [a] and "acquire :: a -> IO b" as well as "release :: a -> IO c" and it'll try to acquire "IO [b]" and if one of the acquires fails, rolls back the already acquired resources?

I could fiddle around with resourcet, but is that the only option? For now, my custom-made function for this makes me uneasy. 🥲

Follow

@pmidden foldr your list into a series of `bracket open close` actions with the base case being your actual multi-bracketed code.

@dpwiz I think this doesn't make sense in my case. This would acquire all the resources, get the "result" and release the resource immediately. It's a use case, just not my use case. I want to acquire all the resources, do something with all at once, and then release them all at once.

@pmidden Hm.. What's the difference between getting the result then releasing and doing something with them then releasing? 🤔

You want a function like this?
```haskell
bracketAll :: [(IO a, a -> IO ())] -> ([a] -> IO b) -> IO b
```

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.