Udemy-akka-essential # 01 Settings, GeneralRecap

Ada·2023년 9월 20일

Akka

목록 보기
21/32

유데미에 아카 관련 기초 강의가 있어서 수강하고있다.

그 강의로 공부한 내용을 정리하고자 한다.


초기 설정

먼저 프로젝트 설정을 해준다.

난 강의의 설정값을 그대로 따라했다.

ThisBuild / version := "0.1.0"

ThisBuild / scalaVersion := "2.12.7"

lazy val root = (project in file("."))
  .settings(
    name := "udemy-akk-essentials"
  )


val akkaVersion = "2.5.13"

libraryDependencies ++= Seq (
  "com.typesafe.akka" %% "akka-actor" % akkaVersion,
  "com.typesafe.akka" %% "akka-testkit" % akkaVersion,
  "org.scalatest" %% "scalatest" % "3.0.5"
)

그 다음 아카시스템이 제대로 동작하는지 확인하기 위해 간단한 코드를 작성했다.

HelloAkka 라는 문구가 잘 출력되었다.

이는 액터 시스템이 올바르게 인스턴스화 되었고, 라이브러리가 설정되었음을 보여준다.


스칼라 기초 문법 복습

다른 스칼라 문법 강의에서 다뤘던 내용을 간략하게 요약해주셨는데
정말 간단히 스칼라를 훑어보기에 좋았다.

package part1recap

import scala.util.Try

object GeneralRecap extends App {

  val aCondition: Boolean = false

  var aVariable = 42
  aVariable += 1 // aVariable = 43

  // expressions
  val aConditionedVal = if (aCondition) 42 else 65

  // code block
  val aCodeBlock = {
    if (aCondition) 74
    56
  }

  // types
  // Unit
  val theUnit = println("Hello, Scala")

  def aFunction(x: Int): Int = x + 1
  // recursion - TAIL recursion
  def factorial(n: Int, acc: Int): Int =
    if (n <= 0) acc
    else factorial(n - 1, acc * n)

  // OOP

  class Animal
  class Dog extends Animal
  val aDog: Animal = new Dog

  trait Carnivore {
    def eat(a: Animal): Unit
  }

  class Crocodile extends Animal with Carnivore {
    override def eat(a: Animal): Unit = println("crunch!")
  }

  // method notations
  val aCroc = new Crocodile
  aCroc.eat(aDog)
  aCroc eat aDog

  // anonymous classes
  val aCarnivore = new Carnivore {
    override def eat(a: Animal): Unit = println("roar")
  }

  aCarnivore eat aDog

  // generics
  abstract class MyList[+A] // + 는 공변적임을 나타냄, 확장

  // companion objects
  object MyList // 클래스와 동반객체 공부

  // case classes
  case class Person(name: String, age: Int) // a LOT in this course!

  // Exceptions
  val aPotentialFailure = try {
    throw new RuntimeException("I'm innocent, I swear!") // Nothing
  } catch {
    case e: Exception => "I caught an exception!"
  } finally  {
    // side effects
    println("some logs")
  }

  // Functional programming
  // 모든 객체가 함수처럼 호출 가능한 것이 아니라, 여러 매개변수를 사용하여 적용 메소드를 정의했을때만 가능하다.

  val incrementer = new Function1[Int, Int] {
    override def apply(v1: Int): Int = v1 + 1
  }

  val incremented = incrementer(42) // 43
  // incrementer.apply(42)

  val anonymousIncrementer = (x: Int) => x + 1 // 람다 구문
  // Int => Int === Function1[Int, Int]

  // FP is all about working with functions as first-class
  List(1,2,3).map(incrementer)
  // map = HOF , map 메소드는 함수를 매개변수로 받거나 반환하므로 고차함수임

  // for comprehensions
  val pairs = for {
    num <- List(1,2,3,4)
    char <- List('a', 'b', 'c', 'd')
  } yield num + "-" + char

  // List(1,2,3,4).flatMap(num => List('a', 'b', 'c', 'd').map(char => num + "-" + char))

  // Seq, Array, List, Vector, Map, Tuples, Sets

  // "collections"
  // Option and Try
  val anOption = Some(2)
  val aTry = Try {
    throw new RuntimeException
  }

  // pattern matching
  val unknown = 2
  val order = unknown match {
    case 1 => "first"
    case 2 => "second"
    case _ => "unknown"
  }

  val bob = Person("Bob", 22)
  val greeting = bob match {
    case Person(n, _) => s"Hi, my name is $n"
    case _ => "I don't know my name"
  }

}

참고

유데미 강좌

profile
백엔드 프로그래머

0개의 댓글