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

array: add S.size #414

Closed
wants to merge 1 commit into from
Closed

array: add S.size #414

wants to merge 1 commit into from

Conversation

davidchambers
Copy link
Member

@davidchambers davidchambers commented Jun 14, 2017

This is equivalent to Haskell's length function.

I quite often find myself using S.prop('length') as Array a -> Integer; it will be convenient to be able to use S.length S.size in these situations instead. :)

Copy link
Member

@gabejohnson gabejohnson left a comment

Choose a reason for hiding this comment

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

As I mentioned in #413 I think size or count would be a more appropriate name given that not all Foldable structures have a "length" (e.g. sets and trees).

@davidchambers
Copy link
Member Author

Thanks for raising the question of naming, Gabe. I took the name from Haskell (and Ramda), but we're not forced to follow Haskell's lead.

Let's indicate our preferences via emoji:

  • 👍 = length
  • 😄 = size
  • ❤️ = count

@davidchambers
Copy link
Member Author

@Avaq? @safareli? @scott-christopher? Do you have preferences?

@Avaq
Copy link
Member

Avaq commented Jun 15, 2017

Oh - I had cast my vote using a mobile app. Guess it didn't work. I prefer size, because JavaScript has conditioned me to treat length like a Number.

@davidchambers davidchambers force-pushed the davidchambers/length branch from e98696c to 9985ade Compare June 15, 2017 12:31
@davidchambers davidchambers changed the title array: add S.length array: add S.size Jun 15, 2017
eq(S.size(Nil), 0);
eq(S.size(Cons('foo', Nil)), 1);
eq(S.size(Cons('foo', Cons('bar', Nil))), 2);
eq(S.size(Cons('foo', Cons('bar', Cons('baz', Nil)))), 3);
Copy link
Member Author

Choose a reason for hiding this comment

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

Now that #413 has been merged we're able to include tests for lists. :)

@safareli
Copy link
Member

the function could also be moved in s-type-classes

@davidchambers
Copy link
Member Author

Good point, Irakli! It would be nice to decide upon a rule we can apply to tell us whether a particular function warrants inclusion in sanctuary-type-classes. Does anyone have a suggestion?

@safareli
Copy link
Member

If a function is polymorphic and it's input have some algebraic constraints, maybe.

@gabejohnson
Copy link
Member

gabejohnson commented Jun 15, 2017

It's interesting that given the definition of reduce for Pair, size(Pair(1, 2)) === 1.

Though the "size" of a Pair is encoded in the type, so it really doesn't matter.

@davidchambers
Copy link
Member Author

I like your suggestion, Irakli. :)

I will open a pull request to define size in sanctuary-type-classes. There are no doubt some other S functions whose implementations should move to Z too.

@davidchambers
Copy link
Member Author

Superseded by sanctuary-js/sanctuary-type-classes#61

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.

4 participants