Skip to content

Commit

Permalink
lovelly
Browse files Browse the repository at this point in the history
  • Loading branch information
esteban committed Sep 19, 2021
0 parents commit 831181e
Show file tree
Hide file tree
Showing 17 changed files with 393 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# https://EditorConfig.org

root = true

[*]
charset = utf-8
end_of_line = unset
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.ammonite/
.bloop/
.bsp/
.dotty-ide*
.idea/
.metals/
.vscode/
*.worksheet.sc
metals.sbt
target/
5 changes: 5 additions & 0 deletions .sbtopts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-J-Xss8m
-J-Xms1g
-J-Xmx8g
-J-XX:ReservedCodeCacheSize=1g
-J-XX:MaxMetaspaceSize=2g
85 changes: 85 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
version = "3.0.0-RC6"

align {
stripMargin = true
}

assumeStandardLibraryStripMargin = false

binPack {
literalArgumentLists = false
}

continuationIndent {
withSiteRelativeToExtends = 3
}

includeNoParensInSelectChains = true

indent {
caseSite = 5
}

indentOperator {
topLevelOnly = false
}

maxColumn = 100

newlines {
alwaysBeforeElseAfterCurlyIf = true
avoidInResultType = true
beforeCurlyLambdaParams = multilineWithCaseOnly
}

project {
excludeFilters = [
".metals"
]
}

rewrite {
rules = [
PreferCurlyFors
RedundantBraces
RedundantParens
SortModifiers
]
sortModifiers {
order = [
final
sealed
abstract
override
implicit
private
protected
lazy
]
}
}

rewriteTokens {
"⇒" = "=>"
"→" = "->"
"←" = "<-"
}

runner {
dialect = scala3
}

spaces {
inImportCurlyBraces = true
}

trailingCommas = multiple

verticalMultiline {
arityThreshold = 3
atDefnSite = true
excludeDanglingParens = []
newlineAfterImplicitKW = true
newlineAfterOpenParen = true
newlineBeforeImplicitKW = false
}
34 changes: 34 additions & 0 deletions aliases.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import Util._

addCommandAlias("l", "projects")
addCommandAlias("ll", "projects")
addCommandAlias("ls", "projects")
addCommandAlias("cd", "project")
addCommandAlias("root", "cd playground3")
addCommandAlias("c", "compile")
addCommandAlias("ca", "Test / compile")
addCommandAlias("t", "test")
addCommandAlias("r", "run")
addCommandAlias("rs", "reStart")
addCommandAlias("s", "reStop")
addCommandAlias(
"up2date",
"reload plugins; dependencyUpdates; reload return; dependencyUpdates",
)

onLoadMessage +=
s"""|
|╭─────────────────────────────────╮
|│ List of defined ${styled("aliases")}
|├─────────────┬───────────────────┤
|│ ${styled("l")} | ${styled("ll")} | ${styled("ls")} │ projects │
|│ ${styled("cd")} │ project │
|│ ${styled("root")} │ cd root │
|│ ${styled("c")} │ compile │
|│ ${styled("ca")} │ compile all │
|│ ${styled("t")} │ test │
|│ ${styled("r")} │ run │
|│ ${styled("rs")} │ reStart │
|│ ${styled("s")} │ reStop │
|│ ${styled("up2date")} │ dependencyUpdates │
|╰─────────────┴───────────────────╯""".stripMargin
43 changes: 43 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import Dependencies._

ThisBuild / organization := "com.estebanmarin"
ThisBuild / scalaVersion := "3.0.1"

ThisBuild / scalacOptions ++=
Seq(
"-deprecation",
"-feature",
"-language:implicitConversions",
"-unchecked",
"-Xfatal-warnings",
"-Yexplicit-nulls", // experimental (I've seen it cause issues with circe)
"-Ykind-projector",
"-Ysafe-init", // experimental (I've seen it cause issues with circe)
) ++ Seq("-rewrite", "-indent") ++ Seq("-source", "future")

lazy val `playground3` =
project
.in(file("."))
.settings(name := "playground3")
.settings(commonSettings)
.settings(dependencies)

lazy val commonSettings = Seq(
update / evictionWarningOptions := EvictionWarningOptions.empty,
Compile / console / scalacOptions --= Seq(
"-Wunused:_",
"-Xfatal-warnings",
),
Test / console / scalacOptions :=
(Compile / console / scalacOptions).value,
)

lazy val dependencies = Seq(
libraryDependencies ++= Seq(
// main dependencies
),
libraryDependencies ++= Seq(
org.scalatest.scalatest,
org.scalatestplus.`scalacheck-1-15`,
).map(_ % Test),
)
15 changes: 15 additions & 0 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import sbt._

object Dependencies {
case object org {
case object scalatest {
val scalatest =
"org.scalatest" %% "scalatest" % "3.2.9"
}

case object scalatestplus {
val `scalacheck-1-15` =
"org.scalatestplus" %% "scalacheck-1-15" % "3.2.9.0"
}
}
}
51 changes: 51 additions & 0 deletions project/Util.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import scala.util._
import scala.sys.process._

import sbt._

object Util {
def styled(in: Any): String =
scala.Console.CYAN + in + scala.Console.RESET

def prompt(projectName: String): String =
gitPrompt
.fold(projectPrompt(projectName)) { g =>
s"$g:${projectPrompt(projectName)}"
}

private def projectPrompt(projectName: String): String =
s"sbt:${styled(projectName)}"

def projectName(state: State): String =
Project
.extract(state)
.currentRef
.project

private def gitPrompt: Option[String] =
for {
b <- branch.map(styled)
h <- hash.map(styled)
} yield s"git:$b:$h"

private def branch: Option[String] =
run("git rev-parse --abbrev-ref HEAD")

private def hash: Option[String] =
run("git rev-parse --short HEAD")

private def run(command: String): Option[String] =
Try(
command
.split(" ")
.toSeq
.!!(noopProcessLogger)
.trim
).toOption

private val noopProcessLogger: ProcessLogger =
ProcessLogger(_ => (), _ => ())

val Cctt: String =
"compile->compile;test->test"
}
1 change: 1 addition & 0 deletions project/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=1.5.5
11 changes: 11 additions & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
ThisBuild / scalaVersion := "2.12.14"
ThisBuild / useSuperShell := false
ThisBuild / autoStartServer := false

update / evictionWarningOptions := EvictionWarningOptions.empty

addSbtPlugin("com.timushev.sbt" % "sbt-rewarn" % "0.1.3")
addSbtPlugin("com.timushev.sbt" % "sbt-updates" % "0.5.3")
addSbtPlugin("com.typesafe.sbt" % "sbt-git" % "1.0.1")
addSbtPlugin("io.spray" % "sbt-revolver" % "0.9.1")
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.3")
1 change: 1 addition & 0 deletions project/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("com.timushev.sbt" % "sbt-updates" % "0.5.3")
4 changes: 4 additions & 0 deletions project/sbt.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Global / excludeLintKeys ++= Set(
autoStartServer,
evictionWarningOptions,
)
31 changes: 31 additions & 0 deletions sbt.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Util._

Global / onChangedBuildSource := ReloadOnSourceChanges

Global / excludeLintKeys ++= Set(
autoStartServer,
turbo,
evictionWarningOptions,
)

Test / parallelExecution := false
Test / testOptions += Tests.Argument(TestFrameworks.ScalaTest, "-oSD")
Test / turbo := true

ThisBuild / autoStartServer := false
ThisBuild / includePluginResolvers := true
ThisBuild / turbo := true
ThisBuild / useSuperShell := false

ThisBuild / watchBeforeCommand := Watch.clearScreen
ThisBuild / watchTriggeredMessage := Watch.clearScreenOnTrigger
ThisBuild / watchForceTriggerOnAnyChange := true

ThisBuild / shellPrompt := { state => s"${prompt(projectName(state))}> " }
ThisBuild / watchStartMessage := {
case (iteration, ProjectRef(build, projectName), commands) =>
Some {
s"""|~${commands.map(styled).mkString(";")}
|Monitoring source files for ${prompt(projectName)}...""".stripMargin
}
}
63 changes: 63 additions & 0 deletions src/main/scala/com/estebanmarin/playground3/Main.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.estebanmarin
package playground3

import scala.annotation.tailrec

object Main extends App:

println("" * 100)

def isSorted[A](as: Array[A], ordered: (A, A) => Boolean): Boolean =
@tailrec
def go(n: Int): Boolean =
if (n >= as.length - 1) true
//if any of them is not ordered, cancel the recursive call
else if (!ordered(as(n), as(n + 1))) false
else go(n + 1)

go(0)

// println(isSorted[Int](as = Array(7, 5, 1, 3), ordered = (a: Int, b: Int) => a > b))
// println(
// isSorted(Array("Scala", "Exercises"), (x: String, y: String) => x.length < y.length)
// )

def partial1[A, B, C](a: A, f: (A, B) => C): B => C =
(b: B) => f(a, b)

def curry[A, B, C](f: (A, B) => C): A => (B => C) =
(a: A) => (b: B) => f(a, b)

// def f(a: Int, b: Int): Int = a + b
// def g(a: Int)(b: Int): Int = a + b

// println(curry(f)(1)(1) == f(1, 1))
// println(curry(f)(1)(1) == g(1)(1))

def uncurry[A, B, C](f: A => B => C): (A, B) => C =
(a: A, b: B) => f(a)(b)

// println(uncurry(g)(1, 1) == g(1)(1))
// println(uncurry(g)(1, 1) == f(1, 1))

def f(b: Int): Int = b / 2
def g(a: Int): Int = a + 2

def compose[A, B, C](f: B => C, g: A => B): A => C =
(a: A) => f(g(a))

def x: (Int, Int) => Boolean = (x: Int, y: Int) => x < y
def y: Function2[Int, Int, Boolean] = (x: Int, y: Int) => x < y
def z = new Function2[Int, Int, Boolean] {
override def apply(a: Int, b: Int): Boolean = a < b
}

lazy val test = (f: Function2[Int, Int, Boolean], x: Int, y: Int) => f(x, y)
lazy val test2 = (f: (Int, Int) => Boolean, x: Int, y: Int) => f(x, y)

lazy val t3 = List(1, 2, 3)

println(compose(f, g)(2))
println(compose(g, f)(2))

println("" * 100)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.estebanmarin
package playground3

final class ExampleSuite extends TestSuite:
test("hello world") {
1 `shouldBe` 1
}
Loading

0 comments on commit 831181e

Please sign in to comment.