Skip to content

Commit 430ebec

Browse files
committed
docs: Setup sbt and use the project
1 parent f1dd9f7 commit 430ebec

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,36 @@
11
# functional-programming-scala
22
[![Scala CI](https://github.com/helanto/functional-programming-scala/actions/workflows/scala.yml/badge.svg)](https://github.com/helanto/functional-programming-scala/actions/workflows/scala.yml)
33

4+
## Using this project.
5+
You need sbt installed to run this project. At the time of writing, `sbt` uses Java versions 8 or 11.
6+
To run the project download the sbt from sources, and store under `/opt`:
7+
```bash
8+
$ sudo ln -sf /opt/sbt-v1.11.0/bin/sbt /usr/local/bin/sbt
9+
$ vim /usr/local/bin/sbt
10+
#!/usr/bin/env bash
11+
# Manually edited
12+
export JAVA_HOME=/Library/Java/JavaVirtualMachines/amazon-corretto-11.jdk/Contents/Home
13+
```
14+
15+
Time to run the project:
16+
```bash
17+
$ sbt console
18+
scala > import programming.functional.Monoid
19+
import programming.functional.Monoid
20+
21+
scala> import programming.functional.Monoid._
22+
import programming.functional.Monoid._
23+
24+
scala> Monoid[String].zero.combine("Hello")
25+
val res0: String = Hello
26+
27+
scala> 8.combine(2)
28+
val res1: Int = 10
29+
30+
scala> false.combine(true)
31+
val res2: Boolean = true
32+
```
33+
434
## A story about kinds
535
**Type constructors** such as `List` or `Option` or `DStream` take other types as parameters to eventually produce concrete types. This reminds us of **functions** or **value constructors**, which take values as parameters to produce new values. Similarities do not end here; type constructors can be partially applied in the same fashion that functions can:
636
```scala

src/main/scala/programming/functional/Monoid.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,11 @@ object Monoid {
6060

6161
def zero: List[A] = List.empty[A]
6262
}
63+
64+
/// Gives access to the Monoid type class methods using an OOP style.
65+
implicit class MonoidOps[A](val a: A) extends AnyVal {
66+
def combine(b: A)(implicit monoid: Monoid[A]): A = monoid.combine(a, b)
67+
68+
def zero(implicit monoid: Monoid[A]): A = monoid.zero
69+
}
6370
}

0 commit comments

Comments
 (0)