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

Implement fromRange function #9

Closed
chshersh opened this issue Apr 3, 2019 · 6 comments · Fixed by #17
Closed

Implement fromRange function #9

chshersh opened this issue Apr 3, 2019 · 6 comments · Fixed by #17
Labels
enhancement New feature or request Hacktoberfest https://hacktoberfest.digitalocean.com/

Comments

@chshersh
Copy link
Contributor

chshersh commented Apr 3, 2019

This can be useful as an alternative to [from .. to] syntax in lists. So I propose to add function with the following type:

fromRange :: Int -> Int -> Slist Int
fromRange from to = ...

Probably this function can be generalized further to work not only with Int 🤔

@vrom911 vrom911 added the enhancement New feature or request label Apr 3, 2019
@vrom911
Copy link
Member

vrom911 commented Apr 3, 2019

Great idea!
Probably it could be generalized to Enum a => a, so I can reuse those functions.

@chshersh
Copy link
Contributor Author

chshersh commented Apr 3, 2019

@vrom911 Sounds nice!

@BlackCapCoder
Copy link

SList implements IsList, so you can already use the [from .. to] syntax if you enable XOverloadedLists.
Ranges are actually just syntatic sugar for enumFromTo, and with OverloadedLists that is just passed to fromList, so:

fromRange :: Enum a => a -> a -> SList a
fromRange from to = fromList $ enumFromTo from to

@chshersh
Copy link
Contributor Author

chshersh commented Apr 8, 2019

@BlackCapCoder It's true that Slist has IsList instance and it's possible to implement fromRange using the fromList function. However, fromList runs in O(n) time since it's requires to calculate the length of the list. If you use fromList [from .. to] there is no way to know the length of the list in advance.

At the same time it's possible to implement fromRange function in a such way that it runs in constant time without fully evaluating list! So instead of inefficient [from .. to] you get an extremely efficient fromRange from to.

P.S. The IsList typeclass also contains fromListN function which can be used to create data structures from list more efficiently if you know the length of the list in advance. And Slist contains efficient implementation of the fromListN function.

@vrom911 vrom911 added the Hacktoberfest https://hacktoberfest.digitalocean.com/ label Sep 29, 2019
@zfnmxt
Copy link
Contributor

zfnmxt commented Oct 4, 2019

Is there any desire to create an analog of any of the other enum "range" functions, i.e. enumFrom, enumFromThen, and enumFromThenTo?

@vrom911
Copy link
Member

vrom911 commented Oct 5, 2019

@zfnmxt , I think that fromRange is sufficient at the moment 🙂

vrom911 added a commit that referenced this issue Oct 7, 2019
Co-Authored-By: Veronika Romashkina <vrom911@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Hacktoberfest https://hacktoberfest.digitalocean.com/
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants