Skip to content

Commit

Permalink
initial examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Maciej Kowalski committed Apr 27, 2017
0 parents commit 17221f7
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Created by .ignore support plugin (hsz.mobi)
### Scala template
*.class
*.log
### SBT template
# Simple Build Tool
# http://www.scala-sbt.org/release/docs/Getting-Started/Directories.html#configuring-version-control

.idea/
dist/*
target/
lib_managed/
src_managed/
project/boot/
project/plugins/project/
.history
.cache
.lib/

42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
Basic scala
===========
(types.scala)
- Product, Coproduct
- ADT (algebraic data types)
- forcomprehension = flatMap + map
- Universal types
- implicit parameters
- implicit classes

Functions
=========
(functions.scala)
- Function composition
- Polimorphic functions
- Identity function

Typeclasses
===========
(typeclasses.scala)
- Adhoc polimorphism
- map, flatMap

Functional patterns
===================
(patterns.scala)
- Monoid
- Functor
- Applicative
- Monad

Generic programming
=========
- Use of typeclasses for the greater good :)
- Introduction into generic programming
- Singleton type
- Symbols
- CSV generator
- Diff

[Read more](https://gist.github.com/jdegoes/97459c0045f373f4eaf126998d8f65dc)

42 changes: 42 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import sbt._

resolvers += Resolver.sonatypeRepo("releases")
resolvers += Resolver.sonatypeRepo("snapshots")

scalaVersion := "2.12.1"

val monocleVersion = "1.4.0"

val scalazVersion = "7.2.7"

val matryoshkaVersion = "0.18.3"

val catsVersion = "0.9.0"

libraryDependencies ++= Seq(
"org.typelevel" %% "cats" % "0.9.0",
"com.chuusai" %% "shapeless" % "2.3.2",
"com.lihaoyi" % "ammonite" % "0.8.2" % "test" cross CrossVersion.full

// "com.github.julien-truffaut" %% "monocle-core" % monocleVersion,
// "com.github.julien-truffaut" %% "monocle-generic" % monocleVersion,
// "com.github.julien-truffaut" %% "monocle-macro" % monocleVersion,
// "com.github.julien-truffaut" %% "monocle-state" % monocleVersion,
// "com.github.julien-truffaut" %% "monocle-refined" % monocleVersion,
// "com.github.julien-truffaut" %% "monocle-law" % monocleVersion % "test",
// "org.scalaz" %% "scalaz-core" % scalazVersion,
// "com.slamdata" %% "matryoshka-core" % matryoshkaVersion
)
val start =
"""
| println("witam")
|
""".stripMargin

initialCommands in console := start

// for @Lenses macro support
addCompilerPlugin("org.scalamacros" %% "paradise" % "2.1.0" cross CrossVersion.full)

// For kind projector, `Either[String, ?]`
addCompilerPlugin("org.spire-math" %% "kind-projector" % "0.9.3")
1 change: 1 addition & 0 deletions project/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=0.13.15
13 changes: 13 additions & 0 deletions src/main/scala/fp/functions.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package fp

object functions {

object identity {
def apply[T](): T = ??? // TODO
}
// think of polimorphic function as functions that
// take a type are return a function
// A : Type => a : A => A

val idint = identity.apply[Int] _
}
42 changes: 42 additions & 0 deletions src/main/scala/fp/patterns.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package fp

import java.util

object patterns {

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

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

// Functors are descriptions of computations
// map(fa)(id) == fa
// map(fa)(f compose g) == map(map(fa)(g), f)
trait Functor[F[_]] {
def map[A, B](fa: F[A])(f: A => B): F[B]
}

object Functor {
implicit val optionFunctor: Functor[Option] = ???

object Syntax {
implicit class FunctorSyntax[F[_], A](val fa: F[A]) extends AnyVal {
def map[B](f: A => B)(implicit FF: Functor[F]): F[B] = FF.map(fa)(f)
}

}
}
val jul: java.util.List[Int] = null

// TODO make this work
// val mapped = jul.map(_ + 1)

case class Box[T](value: T)

// TODO make this compile without implementing map method for Box type
// Box(123).map(_ + 1)

}
36 changes: 36 additions & 0 deletions src/main/scala/fp/typeclasses.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package fp

object typeclasses {
trait Show[A] {
def show(value: A): String
}

object Show {
// def apply[A](implicit s: Show[A]): Show[A] = s
def apply[A: Show](): Show[A] = Show[A]

def pure[A](func: A => String): Show[A] = ???

implicit val showInt: Show[Int] = ???

implicit val showString: Show[String] = ???

implicit val showBoolean: Show[String] = ???
}

trait CsvEncoder[A] {
def encode(value: A): List[String]
}

object CsvEncoder {
def pure[A](f: A => List[String]): CsvEncoder[A] = ???

implicit val intEncode: CsvEncoder[Int] = ???

implicit val stringEncode: CsvEncoder[String] = ???
}

case class User(name: String, age: Int)


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

class types {
// domain Food: Vegetables, Meat, Fruit
// codomain Feeling: Like, Love, Hate

// def like(food: Food): Feeling = ???
}

0 comments on commit 17221f7

Please sign in to comment.