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

length None = 0 #40

Closed
robkuz opened this issue Feb 27, 2017 · 7 comments
Closed

length None = 0 #40

robkuz opened this issue Feb 27, 2017 · 7 comments

Comments

@robkuz
Copy link

robkuz commented Feb 27, 2017

Description

I just realized (via runtime err) that legth None = 0.
So my question is why is that? It makes sense on the one hand that something none existing is 0 but then ...

@gusty
Copy link
Member

gusty commented Feb 27, 2017

Interesting.

The idea is that option are containers, something like a list that can be either empty or with only one element (actually that was the way I emulated options 15 years ago in other languages).

So why does it works here?

It works because Option is a Foldable, which supports the operation toSeq and that's used in the default implementation of length. This function will return either 0 or 1 when applied to options.

We can certainly change that behavior, but to me looks good in principle, unless you have a good reason to remove it.

Sometime ago there was a suggestion in the F# user voice to make Option<'T> implement IEnumerable<'T> which was declined for technical limitation.
If it wasn't for that technical limitation you would be able without this library to do Seq.length None = 0.

@robkuz
Copy link
Author

robkuz commented Feb 27, 2017

No need to change that. It was only surprising (not knowing that).
effectively I had a Option<Option<string>> and a Option<string> which I both piped thru the same generic code. At a later code I was checking the len of the inner value and was surprised that it was always 0 when in effect I was (unconciously) expecting a compiler error.

maybe this should be document

@robkuz robkuz closed this as completed Feb 27, 2017
@gusty
Copy link
Member

gusty commented Feb 27, 2017

Actually, on my free time, I'm working on documenting the library.
Have a look at the abstractions here for example.
I still need to add the page for Foldable, but you will see as in the others, all methods and all types that are supported.
Although it's a lot of work, but it's something that needs to be done, given the size this library is getting, in terms of features and abstractions.

PS: I think in other languages/libraries (Haskell, Purescript, Idris, Scalaz) it's also a Foldable.

@robkuz
Copy link
Author

robkuz commented Feb 27, 2017

Hey that looks sweet!
Can we have some samples for each of those. I am willing to help on this.

@mausch
Copy link
Member

mausch commented Feb 27, 2017

There was quite a bit of criticism and confusion in the Haskell community around this when they were discussing whether to make Foldable part of the Prelude, notably around tuples. https://mail.haskell.org/pipermail/libraries/2016-February/026805.html

Personally I think it's fine.

@gusty
Copy link
Member

gusty commented Feb 27, 2017

@mausch Interesting discussion. Here's foldable in purescript, they have lot of interesting instances we still don't. I think is a good source of inspiration since it's relatively new and they don't have the "breaking change problem", like Haskell libraries.

@gusty
Copy link
Member

gusty commented Feb 28, 2017

@robkuz It would be really, really nice to have some samples either linked or at the foot of each abstraction page.

I just updated the docs to the latest version I have. If you want to add more stuff or samples, please do it. If you have questions about how to add things to the docs, please open an Issue with the questions and I will help.

Thanks in advance.

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

No branches or pull requests

3 participants