ekmett / ekmett/streams

Possible Monad instance for Data.Stream.Future

Open
#22 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Haskell
Stars
25
Forks
14
PR merge metrics
No merged PRs in 30d

Description

I have an idea on the type `Data.Stream.Future` provided by this package.
I'm not sure if it's good or not, but let me explain.

`Data.Stream.Future.Future` can have a `Monad` instance below. Like the instance of `Stream`, this Monad joins `Future (Future a))` by taking diagonal.

```haskell
-- | 'index' but returns the last value for indices larger than
-- the last index.
--
-- It's 'predict' because it guesses the stream will repeat
-- the same value after the last index.
--
-- > predict 0 = extract
predict :: Int -> Future a -> a
predict n aas
| n < 0 = error "predict: negative index"
| otherwise = case aas of
Last a -> a
a :< as | n == 0 -> a
| otherwise -> predict (n - 1) as

instance Monad Future where
aas >>= k = join' (fmap k aas)
where
join' = go 0
go !i (bs :< bss) = predict i bs :< go (succ i) bss
go !i (Last bs) = predict i $ duplicate bs
```

This behavior is incompatible with the current `Applicative Future` instance.
The current `Applicative` zips two `Future` streams by cutting the longer stream to the length of the shorter stream.
The compatible `Applicative` zips two `Future` streams by extending the shorter stream to the length of the longer stream, repeating the last value.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.