Skip to content

Conversation

@sritchie
Copy link
Collaborator

@sritchie sritchie commented Nov 24, 2016

This PR:

  • Creates Gen.scala with generators for Algebird data structures
  • Creates Arbitrary.scala with arbitrary instances for Algebird data structures
  • Moves the generators for Interval, First, Last, Min, Max and ExpHist into these objects
  • moves the data structures from OrderedSemigroup.scala into their own files
  • adds full tests for min, max, first, last
  • adds + operator to min, max, first, last
  • adds min operator to Min and max operator to Max
  • adds FirstAggregator and LastAggregator
  • adds Monoid[Max[Vector[T]]] and Monoid[Max[Stream[T]]]
  • adds tut-based documentation for first, last, min and max.

This sets up the structure I need for upcoming documentation PRs. With each PR I'd like to spruce up each data structure, move its tests into its own file, get the documentation looking good and add arbitrary and gen instances to Gen and Arbitrary.

Following PRs should be smaller now that the structure for this is in place.

@sritchie sritchie force-pushed the sritchie/ordering_docs branch from b2f31b0 to eb33436 Compare November 24, 2016 18:52
Copy link
Collaborator Author

@sritchie sritchie left a comment

Choose a reason for hiding this comment

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

Comments on what I've got so far.

implicit def semigroup[T](implicit ord: Ordering[T]): Semigroup[Max[T]] =
Semigroup.from[Max[T]] { (l, r) => if (ord.gteq(l.get, r.get)) l else r }

implicit def ordering[T](implicit ord: Ordering[T]): Ordering[Max[T]] =
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ah, these gets ordering instances as well.

import org.scalacheck.Arbitrary.{ arbitrary => getArbitrary }
import Gen.oneOf

trait ExpHistGen {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

what do you think of this style, vs jamming everything into a single file?

class CollectionSpecification extends CheckProperties {
import com.twitter.algebird.BaseProperties._

implicit def arbMin[T: Arbitrary]: Arbitrary[Min[T]] =
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

moved to Arbitrary.scala`

implicit def arbAndVal: Arbitrary[AndVal] =
Arbitrary { implicitly[Arbitrary[Boolean]].arbitrary.map{ b => AndVal(b) } }

property("MinSemigroup is a commutative semigroup") {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

moved into Min and Max tests

}
}

object ExpHistGenerators {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

moved to ExpHistInstances.scala in the scalacheck package


case class PosNum[T: Numeric](value: T)

object PosNum {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

All this is moved to Gen.scala and Arbitrary.scala

object Generators {
// The new scalacheck oneOf results in diverging implicits here
// should follow up an investigation?
def oneOf[T](g1: Gen[T], g2: Gen[T], gs: Gen[T]*) = for {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I managed to get rid of this diverging implicit expansion by adding a [T] parameter onto the intervalArb sub-generators.

}

property("Max[Long] is a commutative monoid") {
commutativeMonoidLaws[Max[Long]]
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

bumped these up to commutative monoid tests from the old monoid-only tests.

import com.twitter.algebird.scalacheck.NonEmptyVector
import org.scalacheck.Prop.forAll

class MinSpec extends CheckProperties {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

tests work great as long as you extend CheckProperties.


`First` and `Last`.

## Algebraic Properties
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

WIP!

@codecov-io
Copy link

codecov-io commented Nov 24, 2016

Current coverage is 65.21% (diff: 92.30%)

Merging #578 into develop will increase coverage by 0.77%

@@            develop       #578   diff @@
==========================================
  Files           111        119     +8   
  Lines          4572       4651    +79   
  Methods        4154       4221    +67   
  Messages          0          0          
  Branches        379        391    +12   
==========================================
+ Hits           2946       3033    +87   
+ Misses         1626       1618     -8   
  Partials          0          0          

Powered by Codecov. Last update 776d7a6...e1cbebb

}
}

property("Last.aggregator returns the first item") {
Copy link

Choose a reason for hiding this comment

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

Hello @sritchie,
I was just checking this pr. I think the above statement meant to be ".. last item".

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah, thank you @morazow! I'll make the change tomorrow when I add the actual documentation updates.

@sritchie sritchie changed the title [WIP] Restructure and document First/Last and Min/Max Restructure and document First/Last and Min/Max Nov 27, 2016
implicit def semigroup[T]: Semigroup[First[T]] = new Semigroup[First[T]] {
def plus(l: First[T], r: First[T]): First[T] = l

override def sumOption(iter: TraversableOnce[First[T]]): Option[First[T]] =
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

should be more efficient.

0
}

implicit def vectorMonoid[T: Ordering]: Monoid[Max[Vector[T]]] =
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I followed cats here by only implementing this for Stream and Vector in addition to list.

@sritchie
Copy link
Collaborator Author

@johnynek if we can get #579 in we can close this, since that PR builds on this one and you reviewed everything that's in here already.

@sritchie
Copy link
Collaborator Author

Closed in favor of #529

@sritchie sritchie closed this Nov 28, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants