Skip to content

enshahar/ScalarTurtle

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ScalarTurtle

스칼라를 사용한 터틀 그래픽

이 프로젝트는 거북이를 살펴보는 13가지 방법에서 힌트를 얻어서 시작한 것입니다. 물론 재미로.

oo turtle

객체 지향으로 구현한 거북이는 객체 내부에 저장한 상태를 기반으로 움직입니다. 이렇게 만든 거북이를 조정하는 경우에는 다음과 같이 움직임을 순서대로 적어줘야 합니다.

turtle.On()
(1  to  360).foreach  {  _  =>
    (1  to  4).foreach  {  _  =>
        turtle.move(300)
        turtle.turn(90  deg)
    }
    turtle.setColor(Random.nextInt(256),  Random.nextInt(256),  Random.nextInt(256))
    turtle.turn(1  deg)
}

원한다면 moveturn 등의 명령어가 this를 반환해서 메서드 연쇄(chaining) 기법을 사용할 수도 있었을 것입니다. 그랬다면 코드가 다음과 비슷해졌겠지요.

turtle.On()
(1  to  360).foreach  {  _  =>
    (1  to  4).foreach  {  _  =>
        turtle.move(300)
              .turn(90  deg)
    }
    turtle.setColor(Random.nextInt(256),  Random.nextInt(256),  Random.nextInt(256))
          .turn(1  deg)
}

실행결과

functional turtle

함수형으로 구현한 거북이는 상태를 별도의 케이스클래스로 만들어서 불변 객체에 담습니다. 따라서, 클라이언트는 매번 move등의 명령을 실행할 때마다 새로 생기는 상태 객체를 반환받아서 활용해야 합니다. 이를 편리하게 하기 위해 |>라는 파이프 연산자를 지원하게 만들었습니다. 이를 활용하고, 고차함수를 사용해 repeat등의 함수를 구현하면 다음과 같이 거북이를 조종할 수 있습니다.

turtle |> on |> repeat(360){ (s:TurtleState) => s |> changeColorAndDrawRect |> turn(1 deg) } |> flush

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published