코세라 강의 중 괜찮은 예제가 있어서 소개한다.



https://www.coursera.org/learn/progfun1/lecture/JIPKx/lecture-6-2-combinatorial-search-and-for-expressions


scala에서 소수를 구하는 isPrime() 

def isPrime(n: Int): scala.Boolean = (2 until n) forall (n % _ != 0)

println(isPrime(2)) // true
println(isPrime(3)) // true
println(isPrime(4)) // false
println(isPrime(5)) // true
println(isPrime(6)) // false
println(isPrime(7)) // true
println(isPrime(8)) // false
println(isPrime(9)) // false



합산이 소수일때만 더하는 집합 예제이다. 


def isPrime(n: Int): scala.Boolean = (2 until n) forall (n % _ != 0)

val result = (1 until 7) flatMap (i =>
(1 until i) map (j => (i, j))) filter (pair =>
isPrime(pair._1 + pair._2)
)

println(result)


결과는 다음과 같다. 


Vector((2,1), (3,2), (4,1), (4,3), (5,2), (6,1), (6,5))




scalaProduct 예제이다. 

def scalarProduct(left: List[Double], right: List[Double]): Double = (for ((x, y) <- left zip right) yield x * y).sum


결과는 다음과 같다. 


scala> scalarProduct(List(3,4,5), List(2,2,2))

res39: Double = 24.0


Posted by '김용환'
,

[scala] forall 표현식

scala 2017. 6. 30. 19:33



스칼라의 forall 표현식 예제이다.


콜렉션에 대해서 forall을 사용하면 모든 Range에 대해서 forall 표현식이 참이라면 true를 리턴한다.


scala> Range(2,3) forall (x => x == 2 || x == 3)

res34: Boolean = true






그러나, Range() 에다가 forall을 사용하면 결과 값은 무엇이 될까? 


scala> (2 until -1) forall (_ % 2== 0)

res37: Boolean = true


scala> (2 until 2) forall (_ % 2== 0)

res37: Boolean = true



true이다!!!

Posted by '김용환'
,

scala의 filter류 예제이다. 



filterNot은 filter의 expression의 !과 동일하다.

partition은 filter 와 filterNot을 합친 결과와 동일하다.


  val list = List(1,2,3,4,5)

  println(list.filter(x => x > 3))  // List(4, 5)


  println(list.filterNot(x => x > 3))  // List(1, 2, 3)

  println(list.filter(x => !(x > 3)))  // List(1, 2, 3)


  println(list.partition(x => x > 3)) // (List(4, 5),List(1, 2, 3))

  println(list.filter(x => x > 3), list.filterNot(x => x > 3)) // (List(4, 5),List(1, 2, 3))



결과는 다음과 같다. 


List(4, 5)

List(1, 2, 3)

List(1, 2, 3)

(List(4, 5),List(1, 2, 3))

(List(4, 5),List(1, 2, 3))





takeWhile과 dropWhile은 처음봤을 때 기억해도 잘 나지 않는데. 다시 해봐야 겨우 기억나는 익숙치 않은(?) api이다.


take는 처음 엘리먼트를 얻는다. 


takeWhile은 take를 기반으로 생각하면 좋다.


takeWhile은 술어함수가 list의 엘리먼트가 참에서 시작해서 거짓이 되는 시점의 엘리먼트를 얻는다. 



  val list = List(19, 5, -1, 0, 4, 10, 30)


  println(list.take(1)) // List(19)


  println(list.takeWhile(x => x == 0)) // List()

  println(list.takeWhile(x => x > 0)) // List(19, 5)

  println(list.takeWhile(x => x == 19)) // List(19)




drop(t)는 t번째 엘리먼트부터의 엘리먼트만 리턴한다. 

dropWhile은 drop을 기반으로 이해하는 것이 좋다. 

dropWhile은 술어 함수가 처음으로 거짓이 되는 엘리먼트부터를 얻는다. 

  val list = List(19, 5, -1, 0, 4, 10, 30)
  println(list.drop(3)) // List(0, 4, 10, 30)
  println(list.dropWhile(x => x > 0)) // List(-1, 0, 4, 10, 30)
  println(list.dropWhile(x => x > 5)) // List(5, -1, 0, 4, 10, 30)





span은 takeWhile과 dropWhile를 하나로 결합한 함수이다. 


  val list = List(19, 5, -1, 0, 4, 10, 30)

  println(list.span(x => x > 5))   // (List(19),List(5, -1, 0, 4, 10, 30))

  println(list.takeWhile(x => x > 5), list.dropWhile(x => x > 5)) // (List(19),List(5, -1, 0, 4, 10, 30))




'scala' 카테고리의 다른 글

[scala] forall, for 예제 코드  (0) 2017.06.30
[scala] forall 표현식  (0) 2017.06.30
[scala] mergesort(match) 예제  (0) 2017.06.29
[scala] expansion of function value  (0) 2017.06.28
[scala] Boolean-ifThenElse (coursera 강의 발췌)  (0) 2017.06.28
Posted by '김용환'
,