Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
chuwy committed Sep 2, 2021
0 parents commit b71c043
Show file tree
Hide file tree
Showing 16 changed files with 261 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.bsp/
target/
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Version 0.1.0 (2021-MM-DD)
--------------------------
Initial release
13 changes: 13 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004

Copyright (C) 2020 Anton Parkhomenko <[email protected]>

Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

0. You just DO WHAT THE FUCK YOU WANT TO.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# otus-bats

## Quickstart

```bash
$ sbt assembly
```

## Copyright and License

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004

Copyright (C) 2021 Anton Parkhomenko <[email protected]>

Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

0. You just DO WHAT THE FUCK YOU WANT TO.

[license]: http://www.wtfpl.net/
12 changes: 12 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
lazy val root = project.in(file("."))
.settings(
name := "otusbats",
version := "0.1.0-rc1",
organization := "me.chuwy",
scalaVersion := "2.13.5"
)
.settings(BuildSettings.buildSettings)
.settings(BuildSettings.scalifySettings)
.settings(libraryDependencies ++= Dependencies.all)
.settings(BuildSettings.helpersSettings)

34 changes: 34 additions & 0 deletions project/BuildSettings.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// SBT
import sbt._
import Keys._

/**
* To enable any of these you need to explicitly add Settings value to build.sbt
*/
object BuildSettings {

// Makes package (build) metadata available withing source code
lazy val scalifySettings = Seq(
sourceGenerators in Compile += Def.task {
val file = (sourceManaged in Compile).value / "settings.scala"
IO.write(file, """package me.chuwy.otusbats.generated
|object ProjectMetadata {
| val version = "%s"
| val name = "%s"
| val organization = "%s"
| val scalaVersion = "%s"
|}
|""".stripMargin.format(version.value, name.value, organization.value, scalaVersion.value))
Seq(file)
}.taskValue
)

lazy val buildSettings = Seq[Setting[_]](
addCompilerPlugin("org.typelevel" %% "kind-projector" % "0.13.0" cross CrossVersion.full)
)


lazy val helpersSettings = Seq[Setting[_]](
initialCommands := "import me.chuwy.otusbats._"
)
}
23 changes: 23 additions & 0 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import sbt._

object Dependencies {

object V {
// Scala
val circe = "0.13.0"
val http4s = "0.21.13"
// Scala (test only)
val specs2 = "4.10.5"
val scalaCheck = "1.15.1"
}

// Scala
val all = List(
"io.circe" %% "circe-parser" % V.circe,
"org.http4s" %% "http4s-dsl" % V.http4s,

"org.specs2" %% "specs2-core" % V.specs2 % Test,
"org.specs2" %% "specs2-scalacheck" % V.specs2 % Test,
"org.scalacheck" %% "scalacheck" % V.scalaCheck % 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.4.6
1 change: 1 addition & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("io.github.davidgregory084" % "sbt-tpolecat" % "0.1.16")
13 changes: 13 additions & 0 deletions src/main/scala/me.chuwy/otusbats/Example.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package me.chuwy.otusbats

import cats.Show
import cats.implicits._

object Example {

def example[A: Show, B: Show](a: A, b: B) =
show"A is $a, B is $b"

def example[A, B](implicit eva: Show[A], evb: Show[B]) = ???

}
50 changes: 50 additions & 0 deletions src/main/scala/me.chuwy/otusbats/Functor.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package me.chuwy.otusbats

import cats.Mo

trait Functor[F[_]] {
def map[A, B](fa: F[A])(f: A => B): F[B]
}

object Functor {
// 1. Instances
implicit val optionFunctor: Functor[Option] =
new Functor[Option] {
def map[A, B](fa: Option[A])(f: A => B): Option[B] =
fa match {
case Some(value) => Some(f(value))
case None => None
}
}

implicit val listFunctor: Functor[List] =
new Functor[List] {
def map[A, B](fa: List[A])(f: A => B): List[B] =
fa.map(f)
}

val e: Either[String, Int] = Right(3)

val a: Option[Int] = ???

// val list = List(Some(1), None, Some(3))
// list.map { option => option.map(f) }
// nested(list)(i => i.toString)

def apply[F[_]](implicit ev: Functor[F]): Functor[F] = ev

// 2. Combinators

def nested[F[_]: Functor, G[_]: Functor, A, B](fga: F[G[A]])(f: A => B): F[G[B]] =
Functor[F].map(fga) { ga =>
Functor[G].map(ga)(f)
}

case class NeFunctor[A](a: A, counter: Int) {
def map[B](f: A => B): NeFunctor[B] =
NeFunctor(f(a), counter + 1)
}



}
13 changes: 13 additions & 0 deletions src/main/scala/me.chuwy/otusbats/Monad.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package me.chuwy.otusbats

trait Monad[F[_]] extends Functor[F] { self =>
def flatMap[A, B](fa: F[A])(f: A => F[B]): F[B]

def point[A](a: A): F[A]

def flatten[A](fa: F[F[A]]): F[A]
}

object Monad {

}
8 changes: 8 additions & 0 deletions src/main/scala/me.chuwy/otusbats/Monoid.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package me.chuwy.otusbats

trait Monoid[A] extends Semigroup[A] {
def zero: A
}

object Monoid {
}
7 changes: 7 additions & 0 deletions src/main/scala/me.chuwy/otusbats/Order.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package me.chuwy.otusbats

trait Order[A] {
def compare(x: A, y: A): Option[Boolean]
}


12 changes: 12 additions & 0 deletions src/main/scala/me.chuwy/otusbats/Semigroup.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package me.chuwy.otusbats

trait Semigroup[A] {
def combine(x: A, y: A): A
}

object Semigroup {

implicit val semigroupInt = new Semigroup[Int] {
override def combine(x: Int, y: Int): Int = x + y
}
}
44 changes: 44 additions & 0 deletions src/main/scala/me.chuwy/otusbats/Show.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package me.chuwy.otusbats


trait Show[A] {
def show(a: A): String
}

object Show {

// 1.1 Instances

implicit val stringShow: Show[String] =
new Show[String] {
def show(a: String): String = a
}

implicit val intShow: Show[Int] =
new Show[Int] {
def show(a: Int): String = a.toString
}


// 1.2 Instances with conditional implicit

implicit def listShow[A](implicit ev: Show[A]): Show[List[A]] =
new Show[List[A]] {
def show(as: List[A]): String = "[" ++ as.map(a => ev.show(a)).mkString(",") ++ "]"
}


// 2. Summoner

def apply[A](implicit ev: Show[A]): Show[A] = ev

// 3. Syntax extensions

implicit class ShowOps[A](a: A) {
def show(implicit ev: Show[A]): String =
ev.show(a)
}

// 4. Helper constructors

}

0 comments on commit b71c043

Please sign in to comment.