Skip to content

Instantly share code, notes, and snippets.

@Sedose
Sedose / Delegate all members.scala
Last active September 14, 2024 09:14
Delegation pattern using Scala. Zero boilerplate code. Delegate all members | Delegate some of members
trait DatabaseOperations:
def createDatabase(): Unit
def manageDatabase(): Unit
def maintainDatabase(): Unit
class CloudProvider extends DatabaseOperations:
def createDatabase(): Unit =
println("Cloud Provider creates the database.")
def manageDatabase(): Unit =
println("Cloud Provider manages the database.")
@Sedose
Sedose / MacroDef.scala
Last active September 10, 2024 10:26
Counting Matching Non-Null Fields with Macro. Reduce code duplication
import scala.quoted.{quotes, Expr, Quotes, Type}
inline def compareEntity[T](a: T, b: T): Int =
${compareEntitiesImpl('a, 'b)}
def compareEntitiesImpl[T: Type](a: Expr[T], b: Expr[T])(using Quotes): Expr[Int] = {
import quotes.reflect.*
val tpe = TypeRepr.of[T]
val fields = tpe.typeSymbol.caseFields
@Sedose
Sedose / AAA. MacroDef.scala
Last active September 10, 2024 12:32
ChatGPT generated. Working. Scala macro for entities custom comparison logic + Analogs without Macro
import scala.quoted.*
inline def compareEntity[T](a: T, b: T): Boolean =
${compareEntitiesImpl('a, 'b)}
def compareEntitiesImpl[T: Type](a: Expr[T], b: Expr[T])(using Quotes): Expr[Boolean] = {
import quotes.reflect.*
val tpe = TypeRepr.of[T]
val fields = tpe.typeSymbol.caseFields
@main def main(): Unit =
println(isUgly(14))
private val primes = List(2, 3, 5)
def isUgly(n: Int): Boolean =
var result = n
for (prime <- primes)
while (result > 1 && result % prime == 0)
result /= prime
@Sedose
Sedose / Main.scala
Created September 6, 2024 09:12
Scala's implicit singleton dependency injection is implemented in the language itself, rather than implicitly in a framework like in Java, or done explicitly.
// Define a trait for a generic Database Access Object
trait UserDao {
def getUser(id: Int): String
}
// Concrete implementation of UserDao
class UserDaoImpl extends UserDao {
def getUser(id: Int): String = s"User with ID $id"
}
@Sedose
Sedose / Main.scala
Created September 6, 2024 09:03
Scala 3 implicit function parameter
@main def main(): Unit =
// Implicit parameter declaration
implicit val defaultMultiplier: Int = 2
// Function taking implicit parameter
def multiplyBy(x: Int)(implicit multiplier: Int): Int = x * multiplier
// Function call without explicitly passed parameter
val result = multiplyBy(10) // compiler injects defaultMultiplier automatically
@Sedose
Sedose / Main.scala
Created September 5, 2024 23:20
Union types. Exhaustive `match` (`switch` Java analog, `when` Kotlin analog). Without Java wrappers shit
@main def main(): Unit =
val value1 = true
val value2 = 1234L
val value3 = "Hello Scala 3!"
println(process(value1)) // "Received a Boolean: true"
println(process(value2)) // "Received a Long: 123"
println(process(value3)) // "Hello Scala 3!"
type ExternalType1 = java.lang.Boolean
type ExternalType2 = java.lang.Long
@Sedose
Sedose / Main.java
Last active September 4, 2024 13:29
Java record instances are equal by the record members
import java.util.Objects;
private record AddressIdentity(
String city,
String streetName,
String streetNumber
) {}
void main() {
var first = new AddressIdentity("Kyiv", "Babusia", "12345");
@Sedose
Sedose / Single queue using 2 stacks.java
Last active September 3, 2024 19:54
Leetcode Single queue using 2 stacks.java. Using Java implicitly named classes. Just to show that we can do Java without explicitly named classes
import java.util.ArrayList;
import java.util.List;
void main() {
System.out.println("Hello, Java 22!");
QueueOperations queue = createQueue(createStack(), createStack());
queue.push(1);
queue.push(2);
@Sedose
Sedose / Single queue using 2 stacks.kt
Last active September 3, 2024 06:35
Single queue using 2 stacks impl for Leetcode problem
package org.example
fun main() {
createQueue(createStack(), createStack()).apply {
push(1)
push(2)
push(3)
pop()
peek()
pop()