Basic usage

84 views
Skip to first unread message

Karim Douieb

unread,
Aug 23, 2014, 5:19:48 AM8/23/14
to alge...@googlegroups.com
Hi all, I am trying to use Algebird in some basic context. I just want to create a function op that takes an array of a certain type (Integer in the following case, but I want it to work for string or any other basic types as well). The function just print the summation of the element in the array. 

  import com.twitter.algebird._

  val dataInt: Array[Int]= Array(1, 1, 2, 2, 3, 3, 4, 4, 5, 5)
  op(dataInt)

  def op[A: Monoid]( xs: Array[A]): Unit = {
   val m: Monoid[A] = implicitly[Monoid[A]]
   val res = xs.fold( m.zero )( m.plus )
   println("res = " + res)
 }

this is giving me the following error

[error] .../src/main/scala/algebird.scala:36: value zero is not a member of Monoid[A]
[error]     val res = xs.fold( m.zero )( m.plus )

If I replace Monoid by Ring then it works fine for an array of Int but then there is no way to use it with an array of String since there is no Ring type class for String in Algebird. How can I fix this?

I am pretty new to Scala so sorry if I am missing something obvious.

Oscar Boykin

unread,
Aug 23, 2014, 11:57:35 PM8/23/14
to Karim Douieb, algebird
I don't think algebird is on your classpath.

In the repo:

./sbt algebird-core/console

scala>  import com.twitter.algebird._

import com.twitter.algebird._


scala> 


scala>   val dataInt: Array[Int]= Array(1, 1, 2, 2, 3, 3, 4, 4, 5, 5)

dataInt: Array[Int] = Array(1, 1, 2, 2, 3, 3, 4, 4, 5, 5)


scala>   op(dataInt)

<console>:15: error: not found: value op

                op(dataInt)

                ^


scala> 


scala>   def op[A: Monoid]( xs: Array[A]): Unit = {

     |    val m: Monoid[A] = implicitly[Monoid[A]]

     |    val res = xs.fold( m.zero )( m.plus )

     |    println("res = " + res)

     |  }

op: [A](xs: Array[A])(implicit evidence$1: com.twitter.algebird.Monoid[A])Unit


scala> op(dataInt)

res = 30





--
You received this message because you are subscribed to the Google Groups "algebird" group.
To unsubscribe from this group and stop receiving emails from it, send an email to algebird+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Oscar Boykin :: @posco :: http://twitter.com/posco

Vincent Ohprecio

unread,
Sep 2, 2014, 3:50:20 PM9/2/14
to alge...@googlegroups.com
Here are the instructions to get Algebird running in the REPL:
Reply all
Reply to author
Forward
0 new messages