These are chat archives for non/algebra

16th
May 2016
Srepfler Srdan
@schrepfler
May 16 2016 21:51
does Algebra have scaladocs page?
Srepfler Srdan
@schrepfler
May 16 2016 22:53
I’ve this problem

I have defined a function

override def value(implicit monoid: Monoid[Int]): Int = monoid.combineAll(payload.withDefaultValue(0).valuesIterator)

but when I try to compile this with a simple test I get back could not find implicit value for parameter monoid

[error] /Users/schrepfler/Documents/sources/crdt-kit/src/test/scala/net/sigmalab/crdt/GCounterImplTest.scala:17: could not find implicit value for parameter monoid: algebra.Monoid[Int]
[error]     assert(0 == counter123.value)
Monoid is algebra’s monoid
I’m trying to make this value work for things other than Int
Erik Osheim
@non
May 16 2016 22:54
This message was deleted
P. Oscar Boykin
@johnynek
May 16 2016 22:54
do you have an implicit Monoid[Int] in scope?
Erik Osheim
@non
May 16 2016 22:54
currently algebra doesn't provide a default Monoid[Int] -- this is probably what is tripping you up
(and it's something that @johnynek and i were just talking about fixing.)
Srepfler Srdan
@schrepfler
May 16 2016 23:05
I’ve first tried to add it to my class (GCounterImpl)
that didn’t work
but then I’ve imported cats.implicits_ to my test and that worked
P. Oscar Boykin
@johnynek
May 16 2016 23:05
what did you add to you class exactly?
Srepfler Srdan
@schrepfler
May 16 2016 23:05
to the class nothing
P. Oscar Boykin
@johnynek
May 16 2016 23:05
the implicit needs to be in scope at all call sites
Yilin Wei
@yilinwei
May 16 2016 23:08
@schrepfler the MonoidInstances for the standard types aren't in the Monoid companion object or their own companion object.
You always need to import the implicits for those.
P. Oscar Boykin
@johnynek
May 16 2016 23:09
that’s fine to leave it as it is, you might just want to be clear how it works.
this works because where you call the code, your import includes importing an implicit Monoid[Int].
Yilin Wei
@yilinwei
May 16 2016 23:09
If you're happy if that particular instance, you can just remove the implicit from the method since you know the type.
and add the import in the class.
and it would also work:
Srepfler Srdan
@schrepfler
May 16 2016 23:10
I want to refactor this further and support all things algebra monoid supports
so not only ints
P. Oscar Boykin
@johnynek
May 16 2016 23:10
implicit val intM: Monoid[Int] = new Monoid[Int] { def empty = 0 def combine(a: Int, b: Int) = a + b }
Yilin Wei
@yilinwei
May 16 2016 23:10
Ah, ok. Then you'll have to remember to import for the std types.
Srepfler Srdan
@schrepfler
May 16 2016 23:10
@sp(Int, Long, Float, Double)
P. Oscar Boykin
@johnynek
May 16 2016 23:11
also, I think you want CommutativeMonoid
there are lots or commutative monoids you might want to use with this (max of a timestamp can be interesting)
(hyperloglog to get approximate unique counts)
also, for crdts, you might want to look at Semilattice which is a main property that CRDTs are using.
wait...
schrepfler @schrepfler Java developer continues nodding with the head pretending to understand :)
P. Oscar Boykin
@johnynek
May 16 2016 23:15
you are using Monoid[Int] I think you want Monoid[T] and replace Int with T everywhere
commutative monoid means a + b == b + a
Srepfler Srdan
@schrepfler
May 16 2016 23:16
yes I do
that’s the next step
P. Oscar Boykin
@johnynek
May 16 2016 23:16
which is what you want with a distributed counter
Srepfler Srdan
@schrepfler
May 16 2016 23:16
:D
indeed
P. Oscar Boykin
@johnynek
May 16 2016 23:16
also, I don’t see why you have assert(amt >= 0, s"GCounters can only grow, increment $amt is negative”)
I don’t see why you care if counters are positive, and that is not well defined for a lot of interesting cases
Semilattice is in algebra, and it means combine(a, a) == a
like max, or min.
CRDTs use this, and I assume you are doing something with crdts based on the name of the project
Srepfler Srdan
@schrepfler
May 16 2016 23:18
I’ve followed Noel Welsh’s presentation more/less
and try to be faithfull to that comprehensive review of CRDT’s abstract
as far as API goes
I don’t remember now why it could only hold this property for growing only to be honest, need to revisit it
P. Oscar Boykin
@johnynek
May 16 2016 23:20
honestly, you are probably better off using Numeric which is built into scala, or spire or algebird if you just want to get something going
I imagine it is because the CRDT is going to use the max semilattice down the line, which is going to fail if you do subtraction
but just a guess...
Srepfler Srdan
@schrepfler
May 16 2016 23:21
probably
for that use case I’ll implement the PNCounter
which is essentially made of two GCounters
P. Oscar Boykin
@johnynek
May 16 2016 23:22
(also, note, your GCounter type would have Monoid[GCounter[Id, T, E]] if there is a Monoid[E])
Srepfler Srdan
@schrepfler
May 16 2016 23:23
spire’s Numeric seems ok
P. Oscar Boykin
@johnynek
May 16 2016 23:24
for what you have here: http://www.scala-lang.org/api/2.11.8/#scala.math.Numeric that would work
Srepfler Srdan
@schrepfler
May 16 2016 23:25
so then Monoid[T :> s.m.Numeric] ?
P. Oscar Boykin
@johnynek
May 16 2016 23:25
algebra, algebird, cats, spire, etc… are about writing abstract, and reusable code. Here it is pretty concrete.
no.
if you have a Numeric[T] you can build a Monoid[T] but not in the way you have above.
def fromNumeric[T](num: Numeric[T]): Monoid[T] = …
not Monoid[T >: Numeric]