View Variance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.ktz.example.blog; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class Variance { | |
public static void main(String[] args) { | |
String str = ""; | |
Object obj = str; |
View Variance.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val obj: Object = "Hello World!" | |
val listString: List[String] = List.empty | |
val listAny: List[Any] = listString |
View ScalaList.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type List[+A] = scala.collection.immutable.List[A] |
View ApplyNUnapply.sc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person(val name: String, val age: Int, val weight: Int) | |
object Person { | |
def apply(name: String, age: Int, weight: Int): Person = new Person(name, age, weight) | |
def unapply(arg: Person): Option[(String, Int, Int)] = | |
if(arg.age >= 20) Some(arg.name, arg.age, arg.weight) | |
else None | |
} |
View ReaderExample.sc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait AuthService { | |
def isLogged(name: String): Boolean | |
} | |
class AuthServiceChar3 extends AuthService{ | |
override def isLogged(name: String): Boolean = name.length == 3 | |
} | |
class AuthServiceChar5 extends AuthService{ | |
override def isLogged(name: String): Boolean = name.length == 5 |
View WriterExample.sc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cats.data.Writer | |
def greetW(name: String, logged: Boolean): Writer[List[String], String] = | |
Writer(List("Compose a greeting"), { | |
val userName = if(logged) name else "User" | |
s"Hello $userName" | |
}) | |
def isLoggedW(name: String): Writer[List[String], Boolean] = | |
Writer(List("Checking if user is logged in"), name.length == 3) |
View CartesianProductTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.example.ktz.blog | |
import cats.syntax.all._ | |
import cats.instances.future._ | |
import org.scalatest._ | |
import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.concurrent.duration._ | |
import scala.concurrent.{Await, Future} |
View ApplicativeExample.sc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def getSome(a: Int): Option[Int] = Some(a) | |
def getNone(a: Int): Option[Int] = None | |
def add(a: Int, b: Int): Int = a + b | |
val aOpt = getSome(1) | |
val bOpt = getSome(2) |
View ImplicitlyExample.sc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
implicit val optionInt: Option[Int] = Some(1) | |
implicit val optionBoolean: Option[Boolean] = Some(true) | |
def getImplicitOptionInt(implicit oInt: Option[Int]): Int = oInt.get | |
def getImplicitOptionBoolean(implicit oBoolean: Option[Boolean]): Boolean = oBoolean.get | |
getImplicitOptionInt | |
getImplicitOptionBoolean | |
def getImplicitlyOptionA[A: Option]: A = implicitly[Option[A]].get |
View ImplicitlyOneExample.sc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
implicit val string: String = "Hello" | |
implicit val boolean: Boolean = true | |
implicit val int: Int = 0 | |
def getImplicitT[T](implicit t: T): T = t | |
getImplicitT[Int] | |
getImplicitT[String] | |
getImplicitT[Boolean] |
OlderNewer