-
Notifications
You must be signed in to change notification settings - Fork 346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds Algebird-Bijection #181
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
algebird-bijection/src/main/scala/com/twitter/algebird/bijection/AlgebirdBijections.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
Copyright 2012 Twitter, Inc. | ||
|
||
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. | ||
*/ | ||
|
||
package com.twitter.algebird.bijection | ||
|
||
import com.twitter.algebird.{ Field, Group, Monoid, Ring, Semigroup } | ||
import com.twitter.bijection.{ AbstractBijection, Bijection, ImplicitBijection, Conversion, Reverse } | ||
|
||
import Conversion.asMethod // "as" syntax | ||
|
||
/** | ||
* Bijections on Algebird's abstract algebra datatypes. | ||
* | ||
* @author Oscar Boykin | ||
* @author Sam Ritchie | ||
*/ | ||
|
||
class BijectedSemigroup[T, U](implicit val sg: Semigroup[T], val bij: ImplicitBijection[T, U]) extends Semigroup[U] { | ||
override def plus(l: U, r: U): U = sg.plus(l.as[T], r.as[T]).as[U] | ||
} | ||
|
||
class BijectedMonoid[T, U](implicit val monoid: Monoid[T], bij: ImplicitBijection[T, U]) extends BijectedSemigroup[T, U] with Monoid[U] { | ||
override def zero: U = monoid.zero.as[U] | ||
} | ||
|
||
class BijectedGroup[T, U](implicit val group: Group[T], bij: ImplicitBijection[T, U]) extends BijectedMonoid[T, U] with Group[U] { | ||
override def negate(u: U): U = group.negate(u.as[T]).as[U] | ||
override def minus(l: U, r: U): U = group.minus(l.as[T], r.as[T]).as[U] | ||
} | ||
|
||
class BijectedRing[T, U](implicit val ring: Ring[T], bij: ImplicitBijection[T, U]) extends BijectedGroup[T, U] with Ring[U] { | ||
override def one: U = ring.one | ||
override def times(l: U, r: U): U = ring.times(l.as[T], r.as[T]).as[U] | ||
override def product(iter: TraversableOnce[U]): U = | ||
ring.product(iter map { _.as[T] }).as[U] | ||
} | ||
|
||
class BijectedField[T, U](implicit val field: Field[T], bij: ImplicitBijection[T, U]) extends BijectedRing[T, U] with Field[U] { | ||
override def div(l: U, r: U): U = field.div(l.as[T], r.as[T]).as[U] | ||
override def inverse(u: U): U = field.inverse(u.as[T]).as[U] | ||
} | ||
|
||
trait AlgebirdBijections { | ||
implicit def semigroupBijection[T, U](implicit bij: ImplicitBijection[T, U]): Bijection[Semigroup[T], Semigroup[U]] = | ||
new AbstractBijection[Semigroup[T], Semigroup[U]] { | ||
override def apply(sg: Semigroup[T]) = new BijectedSemigroup[T, U]()(sg, bij) | ||
override def invert(sg: Semigroup[U]) = new BijectedSemigroup[U, T]()(sg, Reverse(bij.bijection)) | ||
} | ||
|
||
implicit def monoidBijection[T, U](implicit bij: ImplicitBijection[T, U]): Bijection[Monoid[T], Monoid[U]] = | ||
new AbstractBijection[Monoid[T], Monoid[U]] { | ||
override def apply(mon: Monoid[T]) = new BijectedMonoid[T, U]()(mon, bij) | ||
override def invert(mon: Monoid[U]) = new BijectedMonoid[U, T]()(mon, Reverse(bij.bijection)) | ||
} | ||
|
||
implicit def groupBijection[T, U](implicit bij: ImplicitBijection[T, U]): Bijection[Group[T], Group[U]] = | ||
new AbstractBijection[Group[T], Group[U]] { | ||
override def apply(group: Group[T]) = new BijectedGroup[T, U]()(group, bij) | ||
override def invert(group: Group[U]) = new BijectedGroup[U, T]()(group, Reverse(bij.bijection)) | ||
} | ||
|
||
implicit def ringBijection[T, U](implicit bij: ImplicitBijection[T, U]): Bijection[Ring[T], Ring[U]] = | ||
new AbstractBijection[Ring[T], Ring[U]] { | ||
override def apply(ring: Ring[T]) = new BijectedRing[T, U]()(ring, bij) | ||
override def invert(ring: Ring[U]) = new BijectedRing[U, T]()(ring, Reverse(bij.bijection)) | ||
} | ||
|
||
implicit def fieldBijection[T, U](implicit bij: ImplicitBijection[T, U]): Bijection[Field[T], Field[U]] = | ||
new AbstractBijection[Field[T], Field[U]] { | ||
override def apply(field: Field[T]) = new BijectedField[T, U]()(field, bij) | ||
override def invert(field: Field[U]) = new BijectedField[U, T]()(field, Reverse(bij.bijection)) | ||
} | ||
} | ||
|
||
object AlgebirdBijections extends AlgebirdBijections |
25 changes: 25 additions & 0 deletions
25
algebird-bijection/src/test/scala/com/twitter/algebird/bijection/AlgebirdBijectionLaws.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright 2010 Twitter Inc. | ||
* | ||
* 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. | ||
*/ | ||
|
||
package com.twitter.algebird.bijection | ||
|
||
import com.twitter.bijection.{ BaseProperties, Bijection } | ||
import org.scalacheck.{ Arbitrary, Properties } | ||
|
||
object AlgebirdBijectionLaws extends Properties("AlgebirdBijections") with BaseProperties { | ||
// TODO: Fill in tests. Ideally we'd publish an algebird-testing | ||
// module before merging this in. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you make bij not val and instead add:
or
Bijection[T, U] = bij.bijection
if you preferbecause ImplicitBijections are kind of ugly plumbing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and similarly below (paging Dr. Macro).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.