-
Notifications
You must be signed in to change notification settings - Fork 411
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[step2] 문자열 계산기 #958
base: bperhaps
Are you sure you want to change the base?
[step2] 문자열 계산기 #958
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
안녕하세요 민성님~
몇가지 코멘트 남겼으니 확인 부탁드립니다 :)
companion object | ||
} | ||
|
||
fun Calculator.Companion.from(input: String): Calculator { | ||
return Calculator(Inputs.from(extractInputs(input))) | ||
} | ||
|
||
private fun extractInputs(input: String): List<CalculatorInput> { | ||
return input.split(" ").asSequence() | ||
.filter { it.isNotBlank() } | ||
.map { CalculatorInput.from(it) } | ||
.toList(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
companion object | |
} | |
fun Calculator.Companion.from(input: String): Calculator { | |
return Calculator(Inputs.from(extractInputs(input))) | |
} | |
private fun extractInputs(input: String): List<CalculatorInput> { | |
return input.split(" ").asSequence() | |
.filter { it.isNotBlank() } | |
.map { CalculatorInput.from(it) } | |
.toList(); | |
} | |
companion object { | |
fun from(input: String): Calculator { | |
return Calculator(Inputs.from(extractInputs(input))) | |
} | |
private fun extractInputs(input: String): List<CalculatorInput> { | |
return input.split(" ").asSequence() | |
.filter { it.isNotBlank() } | |
.map { CalculatorInput.from(it) } | |
.toList(); | |
} | |
} |
https://pearlluck.tistory.com/722
companion object는 이렇게 사용하시면 될 것 같습니다 :)
val hasNextNumber: Boolean | ||
get() { | ||
return numbers.size > numbersPosition | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
val hasNextNumber: Boolean | |
get() { | |
return numbers.size > numbersPosition | |
} | |
val hasNextNumber: Boolean | |
get() = numbers.size > numbersPosition |
이렇게 하면 조금 더 깔끔해질 것 같아요!
fun Inputs.Companion.from(values: List<CalculatorInput>): Inputs { | ||
values.validateFormat() | ||
|
||
val groupBy = values.groupBy { it.getType() } | ||
|
||
return Inputs( | ||
groupBy[Number::class.java] as List<Number>, | ||
groupBy[Operation::class.java] as List<Operation> | ||
) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 부분도 이 코멘트 처럼 companion object 부분을 수정해주세요 :)
if (this[i] !is Number) { | ||
throw IllegalArgumentException("인풋 포멧 오류") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
kotlin 의 require
을 사용해보는건 어떨까요?
https://seosh817.tistory.com/155
또한 어떤 에러인지 알 수 있도록 에러 메시지를 조금 더 상세하게 작성해주세요!
private fun CalculatorInput.getType(): Class<out CalculatorInput> { | ||
if (this is Number) { | ||
return Number::class.java | ||
} | ||
|
||
if (this is Operation) { | ||
return Operation::class.java | ||
} | ||
|
||
throw IllegalArgumentException("잘못된 타입") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when
을 사용하면 조금 더 간결하게 표현할 수 있을 것 같아요!
fun CalculatorInput.Companion.from(input: String): CalculatorInput { | ||
return when { | ||
input.isTypeOf(Number::class.java) -> Number(input.toInt()) | ||
input.isTypeOf(Operation::class.java) -> Operation.from(input) | ||
else -> throw IllegalArgumentException("알 수 없는 입력값.") | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 부분도 변경할 수 있을 것 같습니다 :)
코틀린의 장점 중 하나가 가독성이라고 생각합니다. |
안녕하세요~ 드디어 시작합니다!!
첫 pr이다보니, 일단은 코드에 대한 평가를 먼저 받고 싶어요.
코틀린으로 처음 코딩해보니 어렵네요.. 아직까지는 자바가 너무 편하기도 하고요...
step1, step2를 함께 진행했습니다.
몇가지 궁금한 부분은...
companion을 이런식으로 사용하는게 좋은지 모르겠어요. 정적 팩터리 메소드를 구현하기 위해서 쓰기는 했는데. 이렇게 막 사용해도 되는 키워드인가요??
SAM을 사용하면 좋은점이 무엇인가요?? 가독성..? 은 크게 체감이 안되고.. 코틀린은 왜 SAM을 채택했을까요?
리뷰 부탁 드립니다! 감사합니다~