Skip to content

Commit

Permalink
Merge pull request #8 from morgen-peschke/release.0.1.0
Browse files Browse the repository at this point in the history
Release.0.1.0
  • Loading branch information
morgen-peschke authored Aug 19, 2022
2 parents 0f71ca7 + 01e8170 commit b2ab589
Show file tree
Hide file tree
Showing 20 changed files with 1,367 additions and 16 deletions.
36 changes: 36 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/sh

if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=$(git hash-object -t tree /dev/null)
fi

# Redirect output to stderr.
exec 1>&2

# Cross platform projects tend to avoid non-ASCII filenames; prevent
# them from being added to the repository. We exploit the fact that the
# printable range starts at the space character and ends with tilde.
#
# Note that the use of brackets around a tr range is ok here, (it's
# even required, for portability to Solaris 10's /usr/bin/tr), since
# the square bracket bytes happen to fall in the designated range.
if test $(git diff --cached --name-only --diff-filter=A -z $against |
LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
then
cat <<\EOF
Error: Attempt to add a non-ASCII file name.
This can cause problems if you want to work with people on other platforms.
To be portable it is advisable to rename the file.
EOF
exit 1
fi

mill __.compile + \
__.test + \
__.checkFormat
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
target/
*~
.idea
out
out
.DS_Store
.bsp
project
36 changes: 31 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@ Common utility libraries for Scala, mostly small stuff I don't want to have mult
### SBT
```
libraryDependencies += Seq(
"com.github.morgen-peschke" % "commons-core" % "0.0.1",
"com.github.morgen-peschke" % "commons-collections" % "0.0.1"
"com.github.morgen-peschke" % "commons-core" % "0.1.0",
"com.github.morgen-peschke" % "commons-collections" % "0.1.0",
"com.github.morgen-peschke" % "commons-decline" % "0.1.0",
"com.github.morgen-peschke" % "commons-scalacheck" % "0.1.0" % Test
)
```

### Mill
```
def ivyDeps = Agg(
ivy"com.github.morgen-peschke::commons-core:0.0.1",
ivy"com.github.morgen-peschke::commons-collections:0.0.1"
ivy"com.github.morgen-peschke::commons-core:0.1.0",
ivy"com.github.morgen-peschke::commons-collections:0.1.0",
ivy"com.github.morgen-peschke::commons-decline:0.1.0",
ivy"com.github.morgen-peschke::commons-scalacheck:0.1.0"
)
```

Expand All @@ -33,9 +37,13 @@ things like `Future[Unit]`.

This is very similar to `akka.Done`, and exists as a lightweight alternative to the same.

#### `Slice`

A pure Scala, lightweight implementation of python's slice syntax (e.g `[start:stop:step]`).

### `commons-collections`

Helpers and extensons for working with the Scala standard library.
Helpers and extensions for working with the Scala standard library.

#### `TakeUntil`

Expand All @@ -44,4 +52,22 @@ An alternative to `IterableOnce#takeWhile`, which is primarily differentiated on
reversed.
2. The final element is also taken.

### `commons-scalacheck`

A collection of utilities and syntax to make working with Scalacheck `Gen` smoother.

Highlights include:
- `(0 to 10).choose` as a more flexible alternative to `Gen.chooseNum(0, 10)`
This is particularly handy because `(a until b by c).choose` is equivalent to something closer to this:
```scala
Gen.chooseNum(0, (b-a) - 1).map(l => a + (c * l))
```
- `Combinators.ranges(min, max)` generates `Range`s within those bounds
- `(g: Gen[A]).as.list(a to b)` as an alternative to `Gen.chooseNum(a, b).flatMap(Gen.listOfN(g, foo))`
Variants also exist to produce `Vector`, `Chain`, and the `NonEmpty*` equivalents, as well as one to
lift a `Gen[Char]` into a `Gen[String]`
- `(g: Gen[A]).optional` as an chaining alternative to `Gen.option(g)`

### `commons-decline`

Instances for Decline, notably one for `Slice` as it tends to very handy for CLI utilities.
73 changes: 67 additions & 6 deletions build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ val Scala12 = "2.12.16"
val Scala13 = "2.13.8"

trait CommonModule extends CrossScalaModule with ScalafmtModule with PublishModule {
def publishVersion = "0.0.1"
def publishVersion = "0.1.0"

def pomSettings = PomSettings(
description = "Scala Commons - common utilities for Scala projects",
Expand All @@ -29,7 +29,8 @@ trait CommonModule extends CrossScalaModule with ScalafmtModule with PublishModu
"-Ywarn-unused",
"-Ywarn-dead-code",
"-Ywarn-value-discard",
"-Xfatal-warnings"
"-Xfatal-warnings",
"-language:higherKinds"
)

def versionSpecificOptions(version: String) = version match {
Expand All @@ -51,18 +52,78 @@ object core extends Cross[CoreModule](Scala12, Scala13)
class CoreModule(val crossScalaVersion: String)
extends CommonModule {

def artifactName = "commons-core"
override def artifactName = "commons-core"

override def ivyDeps = Agg(
ivy"org.typelevel::cats-core:2.7.0",
ivy"org.rudogma::supertagged:2.0-RC2",
ivy"org.typelevel::cats-parse:0.3.7"
)

object test extends Tests with TestModule.ScalaTest {

override def moduleDeps = super.moduleDeps ++ Seq(scalacheck(crossScalaVersion))

override def ivyDeps = Agg(
ivy"org.scalacheck::scalacheck:1.16.0",
ivy"org.scalatest::scalatest:3.2.13",
ivy"org.scalatest::scalatest-wordspec:3.2.13",
ivy"org.scalatest::scalatest-propspec:3.2.13",
ivy"org.scalatestplus::scalacheck-1-16:3.2.12.0",
ivy"org.python:jython-slim:2.7.2"
)
}
}

object collections extends Cross[CollectionsModule](Scala12, Scala13)
class CollectionsModule(val crossScalaVersion: String)
extends CommonModule {

def artifactName = "commons-collections"
override def artifactName = "commons-collections"

object test extends Tests with TestModule.ScalaTest {

override def moduleDeps = super.moduleDeps ++ Seq(scalacheck(crossScalaVersion))

override def ivyDeps = Agg(
ivy"org.scalacheck::scalacheck:1.16.0",
ivy"org.scalatest::scalatest:3.2.13",
ivy"org.scalatest::scalatest-wordspec:3.2.13",
ivy"org.scalatest::scalatest-propspec:3.2.13",
ivy"org.scalatestplus::scalacheck-1-16:3.2.12.0",
ivy"org.python:jython-slim:2.7.2"
)
}
}

object scalacheck extends Cross[ScalaCheckModule](Scala12, Scala13)
class ScalaCheckModule(val crossScalaVersion: String)
extends CommonModule {

override def artifactName = "commons-scalacheck"

def moduleDeps = Seq(core(crossScalaVersion))
override def ivyDeps = Agg(
ivy"org.typelevel::cats-core:2.7.0",
ivy"org.scalacheck::scalacheck:1.16.0"
)

object test extends Tests with TestModule.ScalaTest {
def ivyDeps = Agg(ivy"org.scalatest::scalatest:3.2.13")
override def ivyDeps = Agg(
ivy"org.scalacheck::scalacheck:1.16.0",
ivy"org.scalatest::scalatest:3.2.13",
ivy"org.scalatest::scalatest-propspec:3.2.13",
ivy"org.scalatestplus::scalacheck-1-16:3.2.12.0"
)
}
}

object decline extends Cross[DeclineModule](Scala12, Scala13)
class DeclineModule(val crossScalaVersion: String)
extends CommonModule {

override def artifactName = "commons-decline"

override def moduleDeps = super.moduleDeps ++ Seq(core(crossScalaVersion))

override def ivyDeps = Agg(ivy"com.monovore::decline:2.3.0")
}
18 changes: 16 additions & 2 deletions collections/test/src/peschke/UnitSpec.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
package peschke

import org.scalatest.OptionValues
import org.scalacheck.Shrink
import org.scalatest.{EitherValues, OptionValues}
import org.scalatest.matchers.must.Matchers
import org.scalatest.prop.TableDrivenPropertyChecks
import org.scalatest.wordspec.AnyWordSpec
import org.scalatest.propspec.AnyPropSpec
import org.scalatestplus.scalacheck.ScalaCheckDrivenPropertyChecks

trait UnitSpec extends AnyWordSpec with Matchers with OptionValues
trait CommonSpec extends Matchers with EitherValues with OptionValues

trait UnitSpec extends AnyWordSpec with CommonSpec

trait PropSpec extends AnyPropSpec with CommonSpec with ScalaCheckDrivenPropertyChecks {
implicit def noShrink[T]: Shrink[T] = Shrink.shrinkAny
}

trait TableSpec extends AnyWordSpec with CommonSpec with TableDrivenPropertyChecks {
implicit def noShrink[T]: Shrink[T] = Shrink.shrinkAny
}
79 changes: 79 additions & 0 deletions core/src/peschke/python/Slice.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package peschke.python

import cats.syntax.all._

/** Represent a Python slice in Scala
*/
sealed abstract class Slice extends Product with Serializable {
def startOpt: Option[Long] = this match {
case Slice.All(_) => none
case Slice.FromStart(_, _) => none
case Slice.ToEnd(start, _) => start.some
case Slice.SubSlice(start, _, _) => start.some
case Slice.At(index) => index.some
}

def endOpt: Option[Long] = this match {
case Slice.All(_) => none
case Slice.FromStart(end, _) => end.some
case Slice.ToEnd(_, _) => none
case Slice.SubSlice(_, end, _) => end.some
case Slice.At(index) => (index + 1L).some
}
def step: Long
}
object Slice {
def apply(startOpt: Option[Long], endOpt: Option[Long], stepOpt: Option[Long])
: Slice = {
val step = stepOpt.getOrElse(1L)
(startOpt, endOpt) match {
case (Some(start), Some(end)) => SubSlice(start, end, step)
case (Some(start), None) => ToEnd(start, step)
case (None, None) => All(step)
case (None, Some(end)) => FromStart(end, step)
}
}

/** Equivalent to:
* {{{
* [:]
* [::]
* [::step]
* }}}
*/
final case class All(step: Long) extends Slice

/** Equivalent to:
* {{{
* [:end]
* [:end:]
* [:end:step]
* }}}
*/
final case class FromStart(end: Long, step: Long) extends Slice

/** Equivalent to:
* {{{
* [start:]
* [start::]
* [start::step]
* }}}
*/
final case class ToEnd(start: Long, step: Long) extends Slice

/** Equivalent to:
* {{{
* [start:end:slice]
* }}}
*/
final case class SubSlice(start: Long, end: Long, step: Long) extends Slice

/** Equivalent to:
* {{{
* [index]
* }}}
*/
final case class At(index: Long) extends Slice {
override def step: Long = 1L
}
}
Loading

0 comments on commit b2ab589

Please sign in to comment.