Skip to content

Instantly share code, notes, and snippets.

View ostretsov's full-sized avatar
🏠
Working from home

Artem Ostretsov ostretsov

🏠
Working from home
View GitHub Profile
object OmitParenthesis {
def test = "test"
def test2() = "test2"
def main (args: Array[String]): Unit = {
println(test)
// println(test()) // it's impossible
println(test2)
println(test2())
}
class NewYear(y: Int) {
val MinYear = 2014
require(y > MinYear)
val year = y
def this() = this(2015)
override def toString = "Upcoming year is "+year
}
@ostretsov
ostretsov / gcd.scala
Created December 30, 2014 04:23
How to find great common divisor
def gcd(a: Int, b: Int): Int = {
if (b == 0) a else gcd(b, a % b)
}
def >:-> { println("That's awful!!!") }
def main (args: Array[String]) {
>:->
$greater$colon$minus$greater // this will also successful print awful message
}
val a = if (3 > 2) 5 else 6
val b = if (7 > 6) { 25; 4; 3 } else { 0 }
var line = ""
while ((line = file.readLine) != "") {
println(line)
}
val inputFiles = new File("/somewhere/src").listFiles
for {
file <- inputFiles
if file.isFile
line <- scala.io.Source.fromFile(file).getLines()
trimmed = line.trim
if trimmed.matches(".*scala.*")
} println(file+": "+trimmed)
val a = try {
throw new Exception
888 // it would't be result in value ever
} catch {
case ex: Exception => 777
}
println(a)
for (
file <- inputFiles
if file.isFile
) {
var ln = 0
for (line <- scala.io.Source.fromFile(file).getLines()) {
ln += 1
if (line.matches(".*scala.*")) {
println(file.getName+", "+ln+": "+line.trim)
}
for (
file <- inputFiles
if file.isFile
) {
val lines = scala.io.Source.fromFile(file).getLines().toList
def grepScalaFrom(ln: Int): Unit = {
if (ln >= lines.length) -1
else {
if (lines(ln).matches(".*scala.*")) println(file+", "+(ln+1)+": "+lines(ln).trim)