You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@crunch.apache.org by jw...@apache.org on 2012/08/07 04:45:35 UTC

git commit: CRUNCH-35: Add scrunch.PTable#mapKeys

Updated Branches:
  refs/heads/master fd1fc3ad6 -> a92a523fb


CRUNCH-35: Add scrunch.PTable#mapKeys

Signed-off-by: jwills <jw...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/incubator-crunch/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-crunch/commit/a92a523f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-crunch/tree/a92a523f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-crunch/diff/a92a523f

Branch: refs/heads/master
Commit: a92a523fb72a3d3180d43115762442e4ccf881f3
Parents: fd1fc3a
Author: Brian Martin <br...@gmail.com>
Authored: Mon Aug 6 18:21:00 2012 -0700
Committer: jwills <jw...@apache.org>
Committed: Mon Aug 6 19:36:17 2012 -0700

----------------------------------------------------------------------
 .../src/main/scala/org/apache/scrunch/PTable.scala |   14 ++++++
 .../scala/org/apache/scrunch/MapKeysFnTest.scala   |   36 +++++++++++++++
 2 files changed, 50 insertions(+), 0 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-crunch/blob/a92a523f/scrunch/src/main/scala/org/apache/scrunch/PTable.scala
----------------------------------------------------------------------
diff --git a/scrunch/src/main/scala/org/apache/scrunch/PTable.scala b/scrunch/src/main/scala/org/apache/scrunch/PTable.scala
index b6d95a6..7f506e5 100644
--- a/scrunch/src/main/scala/org/apache/scrunch/PTable.scala
+++ b/scrunch/src/main/scala/org/apache/scrunch/PTable.scala
@@ -44,6 +44,12 @@ class PTable[K, V](val native: JTable[K, V]) extends PCollectionLike[CPair[K, V]
     parallelDo(mapValuesFn[K, V, T](f), ptype)
   }
 
+  def mapKeys[T](f: K => T)(implicit pt: PTypeH[T]) = {
+    val ptf = getTypeFamily()
+    val ptype = ptf.tableOf(pt.get(ptf), native.getValueType())
+    parallelDo(mapKeysFn[K, V, T](f), ptype)
+  }
+
   def flatMap[T, To](f: (K, V) => Traversable[T])
       (implicit pt: PTypeH[T], b: CanParallelTransform[T, To]): To = {
     b(this, flatMapFn(f), pt.get(getTypeFamily()))
@@ -158,6 +164,10 @@ trait SMapTableValuesFn[K, V, T] extends MapFn[CPair[K, V], CPair[K, T]] with Fu
   override def map(input: CPair[K, V]) = CPair.of(input.first(), apply(input.second()))
 }
 
+trait SMapTableKeysFn[K, V, T] extends MapFn[CPair[K, V], CPair[T, V]] with Function1[K, T] {
+  override def map(input: CPair[K, V]) = CPair.of(apply(input.first()), input.second())
+}
+
 object PTable {
   def filterFn[K, V](fn: (K, V) => Boolean) = {
     new SFilterTableFn[K, V] { def apply(k: K, v: V) = fn(k, v) }
@@ -167,6 +177,10 @@ object PTable {
     new SMapTableValuesFn[K, V, T] { def apply(v: V) = fn(v) }
   }
 
+  def mapKeysFn[K, V, T](fn: K => T) = {
+    new SMapTableKeysFn[K, V, T] { def apply(k: K) = fn(k) }
+  }
+
   def mapFn[K, V, T](fn: (K, V) => T) = {
     new SMapTableFn[K, V, T] { def apply(k: K, v: V) = fn(k, v) }
   }

http://git-wip-us.apache.org/repos/asf/incubator-crunch/blob/a92a523f/scrunch/src/test/scala/org/apache/scrunch/MapKeysFnTest.scala
----------------------------------------------------------------------
diff --git a/scrunch/src/test/scala/org/apache/scrunch/MapKeysFnTest.scala b/scrunch/src/test/scala/org/apache/scrunch/MapKeysFnTest.scala
new file mode 100644
index 0000000..011a1b4
--- /dev/null
+++ b/scrunch/src/test/scala/org/apache/scrunch/MapKeysFnTest.scala
@@ -0,0 +1,36 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+package org.apache.scrunch
+
+import _root_.org.scalatest.junit.JUnitSuite
+import _root_.org.junit.Test
+
+class MapKeysFnTest extends JUnitSuite {
+
+  @Test
+  def testMapKeys() {
+    val orig = Mem.tableOf(1 -> "a", 2 -> "b", 3 -> "c")
+    val inc = orig.mapKeys(_ + 1)
+
+    assert(
+      inc.keys.materialize
+        .zip(orig.keys.materialize)
+        .forall(x => x._1 == x._2 + 1))
+  }
+
+}
\ No newline at end of file