This file contains hidden or 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
''' | |
SingleTon class definition | |
_instance reference of single instance of the class | |
''' | |
class SingleTon: | |
''' | |
reference to the single instace | |
''' | |
_instance = None |
This file contains hidden or 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
@main | |
def main(): Unit = | |
println(2.square) | |
// Prints 4 in this example | |
extension (value: Int) | |
def square: Int = value * value |
This file contains hidden or 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
object Runner extends App { | |
// To be imported for implicit context | |
import Implicits.IntPlus | |
println(2.square) | |
// Prints 4 in this example | |
} | |
object Implicits { |
This file contains hidden or 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 scala.util.CommandLineParser.FromString | |
given FromString[List[Int]] with | |
def fromString(s: String): List[Int] = s.split(" ").map(_.toInt).toList | |
// ex: cmd args = "1 2 3 4 5 6 7 8 9" | |
@main | |
def main(numbers: List[Int]): Unit = | |
println(numbers) | |
// prints => List(1, 2, 3, 4, 5, 6, 7, 8, 9) |
This file contains hidden or 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
// unary methods such as +, -, !, ~ can be achived with defining methods with unary_ as prefix | |
class Person(name: String): | |
// definition of unary function | |
def unary_! : Person = | |
Person(name.reverse) | |
override def toString: String = s"Person($name)" | |
This file contains hidden or 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
// any method ends with : (colon) will have right assossiative. | |
// this is only applicable to methods having single argument. | |
// using scala 3 | |
class Person: | |
def >>:(any: String) = | |
println(any) | |