GithubHelp home page GithubHelp logo

mschneiderwng / mango Goto Github PK

View Code? Open in Web Editor NEW

This project forked from feijoas/mango

0.0 2.0 0.0 440 KB

The purpose of the Mango library is to provide Guava (Google's core libraries) functionalities to Scala

License: Apache License 2.0

mango's Introduction

The Mango library

The purpose of the Mango library is to provide Guava (Google's core libraries) functionalities to Scala. This is primarily achieved through wrappers around Guava classes and converter between Java and Scala. Also the package structure is intended to mirror the one from Guava.

However there are differences from the Guava libraries:

  • Whenever null is in Guava to indicate the absence of a value we use Option
  • Mango is more restrictive when passing null as arguments to library functions. The recommendation is to never use null at all.
  • In rare cases the method names are changed to conform to the Scala standard library
  • The Mango library uses Type Classes when appropriate

This is a beta version of the library. Many modules are not published yet because they are either not implemented, the test coverage is too low or the documentation is not complete. We publish these modules as soon as they are ready.

We use Travis CI for continuous integration:

Downloading

Mango is programmed against guava-14.0.1 using Scala 2.10. If you want to run the tests you will also need the guava-testlib-14.0.1.

To use the Mango library in sbt add the following dependency to your project file:

resolvers ++= Seq(
    "Sonatype Snapshots" at "http://oss.sonatype.org/content/repositories/snapshots",
    "Sonatype Releases" at "http://oss.sonatype.org/content/repositories/releases"
)

libraryDependencies += "org.feijoas" %% "mango" % "0.7"

Examples

Suppliers

import org.feijoas.mango.common.base.Suppliers._

// a supplier is just a function () => T
val supplier = () => 3                      //> supplier  : () => Int 
                                            //= function0
// convert to a Guava supplier
val gSupplier = supplier.asJava 			//> gSupplier  : com.google.common.base.Supplier[Int] 
                                            //= AsGuavaSupplier(function0)

// create s supplier that memoize its return
// value for 10 seconds
val memSupplier = memoizeWithExpiration(supplier, 10, TimeUnit.SECONDS)
                                              //> memSupplier  : () => Int  
                                              //= Suppliers.memoizeWithExpiration(function0, 10, SECONDS)

Caches

import java.util.concurrent.TimeUnit
import org.feijoas.mango.common.cache._

// the function to cache
val expensiveFnc = (str: String) => str.length  //> expensiveFnc  : String => Int 

// create a cache with a maximum size of 100 and 
// exiration time of 10 minutes
val cache = CacheBuilder.newBuilder()
.maximumSize(100)
.expireAfterWrite(10, TimeUnit.MINUTES)
.build(expensiveFnc)              //> cache  : LoadingCache[String,Int]

cache("MyString")                 //> res0: Int = 8

BloomFilter & Funnel

import org.feijoas.mango.common.hash.Funnel._
import org.feijoas.mango.common.hash.BloomFilter._

// A Funnel describes how to decompose a particular object type into primitive field values.
// For example, if we had
case class Person(id: Integer, firstName: String, lastName: String, birthYear: Int)

// our Funnel might look like
implicit val personFunnel = new Funnel[Person] {
 override def funnel(person: Person, into: PrimitiveSink) = {
    into
    .putInt(person.id)
    .putString(person.firstName, Charsets.UTF_8)
    .putString(person.lastName, Charsets.UTF_8)
    .putInt(person.birthYear)
 }
}

val friends: BloomFilter[Person] = BloomFilter.create(500, 0.01)
friendsList.foreach { case p: Person => friends.put(p) }

// much later
if (friends.mightContain(dude)) {
	// the probability that dude reached this place if he isn't a friend is 1%
	// we might, for example, start asynchronously loading things for dude while we do a more expensive exact check
}

See the individual packages for more examples and documentation.

Converter

Conversions to and from the Guava libraries are done with the .asJava and .asScala methods respectively. These methods are imported together with the utility functions of the class. For example:

// import converter for com.google.common.base.Function
import org.feijoas.mango.common.base.Functions._

val fnc = (str: String)=> str.length   //> fnc  : String => Int = function1
fnc.asJava                             //> res0: com.google.common.base.Function[String,Int] 
                                       //= AsGuavaFunction(function1)

Build

Just clone the git repository and build Mango in the following way:

sbt update
sbt compile

Don't forget to test

sbt test

Help

Besides the Scaladoc there is an excellent user guide Guava Explained which should be sufficient for almost all questions.

License

Apache License, Version 2.0

mango's People

Contributors

mschneiderwng avatar feijoas avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.