Created
October 27, 2016 06:29
-
-
Save bertzzie/c917c576fb9800e0396595278839281a to your computer and use it in GitHub Desktop.
Scala Hello World and Basics
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 com.example | |
object Hello { | |
// 0 - Functions | |
def printHelloWorld = println("Hello, world!") | |
def add5(num: Int): Int = num + 5 | |
def add(a: Int, b: Int): Int = a + b | |
def sub(a: Int, b: Int) = a - b | |
// 1 - Looping | |
def multipleOf5(count: Int) = 1 to count map { i => i * 5 } | |
def add5(nums: Seq[Int]): Seq[Int] = nums map { n => add5(n) } | |
val add2 = (nums: Seq[Int]) => nums.map(_ + 2) | |
def printNumbers(nums: Seq[Int]): Unit = nums foreach { n => print(n) } | |
def printNumbers2(nums: Seq[Int]): Unit = nums foreach(print) | |
def printStrings(strs: Seq[String]): Unit = strs foreach println | |
def total = (nums: Seq[Int]) => nums.fold(0)((total, curr) => total + curr) | |
//def add2Total(nums: Seq[Int]): Int = total(add2(nums)) | |
val add2Total = total compose add2 | |
// 2 - conditionals | |
def isEven(num: Int): Boolean = if (num % 2 == 0) true else false | |
def isOdd(num: Int): Boolean = !isEven(num) | |
def isOddStr(num: Int): String = if (num % 2 != 0) "odd" else "even" | |
def count(num: Int): String = num match { | |
case n if n < 0 => "Negatif" | |
case 0 => "Nol" | |
case 1 => "Satu" | |
case 2 => "Dua" | |
case _ => "Banyak" | |
} | |
// 3 - Loop + conditionals | |
/* | |
def fizzBuzz(num: Int): Seq[String] = 1 to num map { n => | |
n match { | |
case x if x % 3 == 0 && x % 5 == 0 => "FizzBuzz" | |
case x if x % 3 == 0 => "Fizz" | |
case x if x % 5 == 0 => "Buzz" | |
case x => x.toString | |
} | |
} | |
*/ | |
def fizzBuzz(num: Int): Seq[String] = 1 to num map { | |
case x if x % 3 == 0 && x % 5 == 0 => "FizzBuzz" | |
case x if x % 3 == 0 => "Fizz" | |
case x if x % 5 == 0 => "Buzz" | |
case x => x.toString | |
} | |
def main(args: Array[String]): Unit = { | |
printHelloWorld | |
println(add5(15)) | |
println(add(10, 20)) | |
println(sub(10, 20)) | |
println(multipleOf5(5)) | |
println(add5(Seq(1, 2, 3, 4, 5))) | |
printNumbers(Seq(2, 3, 4, 5, 6)) | |
println | |
printNumbers2(Seq(4, 5, 6, 7, 8, 9)) | |
println | |
println(total(Seq(1, 2, 3, 4))) | |
printNumbers2(add2(Seq(1, 2, 3, 4))) | |
println | |
println(add2Total(Seq(1, 2, 3, 4))) | |
println(isEven(2)) | |
println(isEven(3)) | |
println(isEven(4)) | |
println(isEven(5)) | |
println | |
println(isOdd(2)) | |
println(isOdd(3)) | |
println(isOdd(4)) | |
println(isOdd(5)) | |
println | |
println(isOddStr(2)) | |
println(isOddStr(3)) | |
println(isOddStr(4)) | |
println(isOddStr(5)) | |
println | |
println(count(0)) | |
println(count(-1)) | |
println(count(1)) | |
println(count(2)) | |
println(count(10)) | |
println(count(100)) | |
println | |
printStrings(fizzBuzz(100)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
yang bikin sedikit terkejut itu variable yang tiba2 muncul di dalam { } nya map dan variable _ nya plus yang fold sepertinya perlu dijelasin dikit.
kalau audiencenya dari yang terbiasa coding imperative bisa dikasi perbandingan contoh codenya tuh, yang imperatif sama versi deklaratifnya.