A unifying typeclass describing collections and higher-order data transformation and manipulation actions common to a wide variety of data processing tasks. Inspired by the Scala collections API.
Write an algorithm that accepts a type that adheres to the Data type class and watch it work everywhere! Data describes higher-order-functions that manipulate and transform generic collections. It makes use of the type class design pattern for ad-hoc polymorphism (instead of the common inheritence-based sub-typing polymorphism). This is a powerful and incredibly flexible mechanism for describing generic behaviors: it is akin to static duck typing.
Implementations for concrete types include:
Add the following to your build.sbt
file:
libraryDependencies ++= Seq("io.malcolmgreaves" %% "abstract_data" % "X.Y.Z")
Where X.Y.Z
is the most recent one from sonatype.
We strive for high code coverage. Check out all of the tests.
For a rather small use case, check out Sum, which shows how to implement the common sum
functionality on a Data
type class instance:
object Sum extends Serializable {
// Brings implicits in scope for things like `map`, `flatMap`, etc.
// as object oriented style infix notation. These are still using
// the type class method definitions!
import Data.ops._
def apply[N: Numeric: ClassTag, D[_]: Data](data: D[N]): N = {
val add = implicitly[Numeric[N]].plus _
data.aggregate(implicitly[Numeric[N]].zero)(add, add)
}
def apply[N: Numeric](first: N, numbers: N*): N = {
val add = implicitly[Numeric[N]].plus _
numbers.foldLeft(first)(add)
}
}
Now you can sum elements of a Traversable
whose elements are Numeric
. For instance:
implicit val t = fif.TravData
Sum(Traversable(1.0, 2.0, 3.0)) == 6.0
Sum(1, 2, 3) == 6
We <3 contributions! We want this code to be useful and used! We use pull requests to review and discuss changes, fixes, and improvements.
Copyright 2015 Malcolm Greaves
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.