먼저 순서를 뒤집으세요:
let segments = list.reverse(segments)
그런 다음 4번째 항목을 가져오세요
let chosen = case list.drop(segments, 3) {
[fourth, ..] -> fourth
[] -> ""
}
제 컴퓨터에서 실행해 보지는 않았지만, 이렇게 하면 되지 않을까요?
좀 더 관용적인 방법은 다음과 같아요:
pub fn fourth_from_tail(entries: String) -> Result(String, Nil) {
case list.reverse(string.split(entries, on: "-")) {
// Matches if there are at least 4 segments, binding the 4th from the end to `picked`
[_, _, _, picked, ..] -> Ok(picked)
// Discards strings with fewer than 4 segments
_ -> Error(Nil)
}
}
영어에서 번역·원문 보기Start by reversing it:
let segments = list.reverse(segments)
then take element number 4
let chosen = case list.drop(segments, 3) {
[fourth, ..] -> fourth
[] -> ""
}
I haven't tried running this locally, but wouldn't this work?
Here's an approach that's more idiomatic:
pub fn fourth_from_tail(entries: String) -> Result(String, Nil) {
case list.reverse(string.split(entries, on: "-")) {
// Matches if there are at least 4 segments, binding the 4th from the end to `picked`
[_, _, _, picked, ..] -> Ok(picked)
// Discards strings with fewer than 4 segments
_ -> Error(Nil)
}
}