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 a variant unfoldrExactN, returning the resulting generator #447

Open
Bodigrim opened this issue Aug 18, 2022 · 3 comments
Open

Add a variant unfoldrExactN, returning the resulting generator #447

Bodigrim opened this issue Aug 18, 2022 · 3 comments

Comments

@Bodigrim
Copy link
Contributor

I have a job for

unfoldrExactN_ :: Int -> (b -> (a, b)) -> b -> (Vector a, b)

but it is surprisingly difficult to emulate: the best I managed to do was put b into State and use replicateM.

How do you feel about extending vector with such function?

@lehins
Copy link
Contributor

lehins commented Aug 18, 2022

Going through State + replicateM is slow, unless maybe if we specialize it as well like it is done for ST and IO when doing unstreaming.

One fallback always is to use mutable interface instead, but that means no fusion.

So, this function is incredibly useful, as you probably saw in haskell/random#133 so I am definitely all for adding unfoldr that returns the last value. Any volunteers?

@Shimuuar
Copy link
Contributor

This is very tricky function to write inside stream fusion framework. b is hidden inside existential state of stream and we don't have any way to access it:

data Stream m a = forall s. Stream (s -> m (Step s a)) s

data Step s a where
  Yield :: a -> s -> Step s a
  Skip  :: s -> Step s a
  Done  :: Step s a

On top of it Done discards state.

@Shimuuar
Copy link
Contributor

There's consensus that such function is useful. Although it could be only implemented without fusion support. I'm mrking it as help wanted

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

3 participants