Skip to content

Commit

Permalink
Introduce Scalafmt (#2562)
Browse files Browse the repository at this point in the history
  • Loading branch information
joan38 authored and kailuowang committed Oct 18, 2018
1 parent 1b7551b commit 774fb51
Show file tree
Hide file tree
Showing 399 changed files with 8,050 additions and 6,951 deletions.
6 changes: 6 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
maxColumn = 120
continuationIndent.defnSite = 2
assumeStandardLibraryStripMargin = true
danglingParentheses = true
rewrite.rules = [AvoidInfix, SortImports, RedundantBraces, RedundantParens, SortModifiers]
docstrings = JavaDoc
1 change: 0 additions & 1 deletion alleycats-core/src/main/scala/alleycats/ConsK.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@ object ConsK extends ConsK0 {

@imports[ConsK]
trait ConsK0

26 changes: 16 additions & 10 deletions alleycats-core/src/main/scala/alleycats/std/map.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,25 @@ trait MapInstances {

// toList is inconsistent. See https://github.com/typelevel/cats/issues/1831
implicit def alleycatsStdInstancesForMap[K]: Traverse[Map[K, ?]] =
new Traverse[Map[K, ?]] {
new Traverse[Map[K, ?]] {

This comment has been minimized.

Copy link
@rexingQ

rexingQ Dec 28, 2019

yh


def traverse[G[_], A, B](fa: Map[K, A])(f: A => G[B])(implicit G: Applicative[G]): G[Map[K, B]] = {
val gba: Eval[G[Map[K, B]]] = Always(G.pure(Map.empty))
val gbb = Foldable.iterateRight(fa, gba){ (kv, lbuf) =>
G.map2Eval(f(kv._2), lbuf)({ (b, buf) => buf + (kv._1 -> b)})
}.value
val gbb = Foldable
.iterateRight(fa, gba) { (kv, lbuf) =>
G.map2Eval(f(kv._2), lbuf)({ (b, buf) =>
buf + (kv._1 -> b)
})
}
.value
G.map(gbb)(_.toMap)
}

override def map[A, B](fa: Map[K, A])(f: A => B): Map[K, B] =
fa.map { case (k, a) => (k, f(a)) }

def foldLeft[A, B](fa: Map[K, A], b: B)(f: (B, A) => B): B =
fa.foldLeft(b) { case (x, (k, a)) => f(x, a)}
fa.foldLeft(b) { case (x, (k, a)) => f(x, a) }

def foldRight[A, B](fa: Map[K, A], lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] =
Foldable.iterateRight(fa.values, lb)(f)
Expand All @@ -45,11 +49,13 @@ trait MapInstances {

override def toList[A](fa: Map[K, A]): List[A] = fa.values.toList

override def collectFirst[A, B](fa: Map[K, A])(pf: PartialFunction[A, B]): Option[B] = fa.collectFirst(new PartialFunction[(K, A), B] {
override def isDefinedAt(x: (K, A)) = pf.isDefinedAt(x._2)
override def apply(v1: (K, A)) = pf(v1._2)
})
override def collectFirst[A, B](fa: Map[K, A])(pf: PartialFunction[A, B]): Option[B] =
fa.collectFirst(new PartialFunction[(K, A), B] {
override def isDefinedAt(x: (K, A)) = pf.isDefinedAt(x._2)
override def apply(v1: (K, A)) = pf(v1._2)
})

override def collectFirstSome[A, B](fa: Map[K, A])(f: A => Option[B]): Option[B] = collectFirst(fa)(Function.unlift(f))
override def collectFirstSome[A, B](fa: Map[K, A])(f: A => Option[B]): Option[B] =
collectFirst(fa)(Function.unlift(f))
}
}
8 changes: 4 additions & 4 deletions alleycats-core/src/main/scala/alleycats/std/set.scala
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ object SetInstances {

override def get[A](fa: Set[A])(idx: Long): Option[A] = {
@tailrec
def go(idx: Int, it: Iterator[A]): Option[A] = {
def go(idx: Int, it: Iterator[A]): Option[A] =
if (it.hasNext) {
if (idx == 0) Some(it.next) else {
if (idx == 0) Some(it.next)
else {
it.next
go(idx - 1, it)
}
} else None
}
if (idx < Int.MaxValue && idx >= 0L) go(idx.toInt, fa.toIterator) else None
if (idx < Int.MaxValue && idx >= 0L) go(idx.toInt, fa.toIterator) else None
}

override def size[A](fa: Set[A]): Long = fa.size.toLong
Expand Down
3 changes: 2 additions & 1 deletion alleycats-core/src/main/scala/alleycats/std/try.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ object TryInstances {
def coflatMap[A, B](fa: Try[A])(f: Try[A] => B): Try[B] = Try(f(fa))
def extract[A](p: Try[A]): A = p.get

def tailRecM[A, B](a: A)(f: (A) => Try[Either[A, B]]): Try[B] = cats.instances.try_.catsStdInstancesForTry.tailRecM(a)(f)
def tailRecM[A, B](a: A)(f: (A) => Try[Either[A, B]]): Try[B] =
cats.instances.try_.catsStdInstancesForTry.tailRecM(a)(f)
}
}

Expand Down
4 changes: 1 addition & 3 deletions alleycats-core/src/main/scala/alleycats/syntax/all.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package alleycats.syntax

object all
extends EmptySyntax
with FoldableSyntax
object all extends EmptySyntax with FoldableSyntax
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ object foldable extends FoldableSyntax
trait FoldableSyntax {
implicit class ExtraFoldableOps[F[_]: Foldable, A](fa: F[A]) {
def foreach(f: A => Unit): Unit =
fa.foldLeft(()) { (_, a) => f(a) }
fa.foldLeft(()) { (_, a) =>
f(a)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,18 @@ import org.scalacheck.Arbitrary
import org.scalacheck.Prop._
import org.typelevel.discipline.Laws


trait FlatMapRecTests[F[_]] extends Laws {
def laws: FlatMapLaws[F]

def tailRecM[A: Arbitrary](implicit
ArbFA: Arbitrary[F[A]],
ArbAFA: Arbitrary[A => F[A]],
EqFA: Eq[F[A]]
): RuleSet = {
EqFA: Eq[F[A]]): RuleSet =
new DefaultRuleSet(
name = "flatMapTailRec",
parent = None,
"tailRecM consistent flatMap" -> forAll(laws.tailRecMConsistentFlatMap[A] _))
}
"tailRecM consistent flatMap" -> forAll(laws.tailRecMConsistentFlatMap[A] _)
)
}

object FlatMapRecTests {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package alleycats
package tests


import alleycats.std.MapInstances
import catalysts.Platform
import cats._
Expand All @@ -25,7 +24,8 @@ trait TestSettings extends Configuration with Matchers {
maxDiscardedFactor = if (Platform.isJvm) PosZDouble(5.0) else PosZDouble(50.0),
minSize = PosZInt(0),
sizeRange = if (Platform.isJvm) PosZInt(10) else PosZInt(5),
workers = PosInt(1))
workers = PosInt(1)
)

lazy val slowCheckConfiguration: PropertyCheckConfiguration =
if (Platform.isJvm) checkConfiguration
Expand All @@ -36,7 +36,17 @@ trait TestSettings extends Configuration with Matchers {
* An opinionated stack of traits to improve consistency and reduce
* boilerplate in Alleycats tests. Derived from Cats.
*/
trait AlleycatsSuite extends FunSuite with Matchers with GeneratorDrivenPropertyChecks with Discipline with TestSettings with AllInstances with AllSyntax with TestInstances with StrictCatsEquality with MapInstances {
trait AlleycatsSuite
extends FunSuite
with Matchers
with GeneratorDrivenPropertyChecks
with Discipline
with TestSettings
with AllInstances
with AllSyntax
with TestInstances
with StrictCatsEquality
with MapInstances {
implicit override val generatorDrivenConfig: PropertyCheckConfiguration =
checkConfiguration

Expand All @@ -48,7 +58,5 @@ trait AlleycatsSuite extends FunSuite with Matchers with GeneratorDrivenProperty
sealed trait TestInstances {
// To be replaced by https://github.com/rickynils/scalacheck/pull/170
implicit def arbitraryTry[A: Arbitrary]: Arbitrary[Try[A]] =
Arbitrary(Gen.oneOf(
arbitrary[A].map(Success(_)),
arbitrary[Throwable].map(Failure(_))))
Arbitrary(Gen.oneOf(arbitrary[A].map(Success(_)), arbitrary[Throwable].map(Failure(_))))
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@ import alleycats.std.all._

class IterableTests extends AlleycatsSuite {

test("foldLeft sum == sum"){
test("foldLeft sum == sum") {
val it = Iterable(1, 2, 3)
Foldable[Iterable].foldLeft(it, 0){
Foldable[Iterable].foldLeft(it, 0) {
case (b, a) => a + b
} shouldEqual(it.sum)
} shouldEqual (it.sum)
}

test("foldRight early termination"){
Foldable[Iterable].foldRight(Iterable(1, 2, 3), Eval.now("KO")){
test("foldRight early termination") {
Foldable[Iterable]
.foldRight(Iterable(1, 2, 3), Eval.now("KO")) {
case (2, _) => Eval.now("OK")
case (a, b) => b
}.value shouldEqual(Eval.now("OK").value)
}
}
.value shouldEqual (Eval.now("OK").value)
}

}
3 changes: 0 additions & 3 deletions alleycats-tests/src/test/scala/alleycats/tests/SetSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,3 @@ class SetSuite extends AlleycatsSuite {

checkAll("Foldable[Set]", SerializableTests.serializable(Foldable[Set]))
}



Loading

0 comments on commit 774fb51

Please sign in to comment.