Skip to content

Added the (..) function as an alias to range. #14

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

Merged
merged 2 commits into from
Aug 23, 2014
Merged

Added the (..) function as an alias to range. #14

merged 2 commits into from
Aug 23, 2014

Conversation

jdantonio
Copy link
Contributor

Page 38 of the Leanpub book (version published on 2014-08-16) defines a (..) function that is an alias for range. The book then uses this function repeatedly in following examples. This PR adds this function to the Data.Array library.

@jdantonio
Copy link
Contributor Author

I'm new to PureScript so I apologize if this function appears somewhere else in the standard library and I have simply missed it. I've been programming primarily in Ruby for several years now and this is the notation that Ruby uses for creating a Range object. I find the syntax very intuitive and feel it leads to very expressive code.

@garyb
Copy link
Member

garyb commented Aug 23, 2014

I suppose the only downside of adding this is it would mean having to change the book to use import Data.Array hiding ((..)), and then redefining it which might be a bit odd.

I'll let @paf31 weigh in though. :)

@jdantonio
Copy link
Contributor Author

@garyb Thank you for the feedback. That's a very good point, which I hadn't thought of at first. But after reading your comment I went back to the book and discovered that it does something very similar with the length function. On page 35 the book has an example which creates a length function using recursion and null:

import Data.Array (null)
import Data.Array.Unsafe (tail)

length :: forall a. [a] -> Number
length arr =
  if null arr
    then 0
    else 1 + length (tail arr)

This has the same signature as length from the standard library, but a very different implementation. So irrespective of what @paf31 decides about (..), an update to the book regarding redefining and hiding methods may be helpful.

@paf31
Copy link
Contributor

paf31 commented Aug 23, 2014

I think this would be useful. I'd just need to change the wording in the book a bit. I'll be doing a release of the book later today, so it's no big deal. Thanks!

@@ -287,6 +288,9 @@ foreign import range
\ };\
\}" :: Number -> Number -> [Number]

(..) :: Number -> Number -> [Number]
(..) = Data.Array.range
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can just say (..) = range since we're in the same module.

@jdantonio
Copy link
Contributor Author

I've updated the function definition based on the suggestion from @paf31.

paf31 added a commit that referenced this pull request Aug 23, 2014
Added the (..) function as an alias to range.
@paf31 paf31 merged commit cba046d into purescript:master Aug 23, 2014
@paf31
Copy link
Contributor

paf31 commented Aug 23, 2014

Thanks! 👍

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

Successfully merging this pull request may close these issues.

3 participants