Sequence 역할
Sequence 역할 (raku-type-sequence)
시퀀스들에 공통으로 쓰이는 메서드들을 모아 둔 역할이에요. 순차적인 Seq, 순서가 정해진 병렬 HyperSeq, 순서가 없는 병렬 RaceSeq 같은 모든 시퀀스에서 이 메서드들이 동작해야 해요.
정의
role Sequence does PositionalBindFailover { }
이 역할은 시퀀스를 변환하고 그 요소에 접근하는 여러 메서드를 구현해요. 이 메서드들은 공통점이 하나 있어요. 시퀀스가 아직 캐시되지 않았다면 모두 기본 반복자(iterator)에 접근한다는 거예요. 그래서 이미 소비(consume)된 Seq에서 호출하면 X::Seq::Consumed 타입의 오류를 던져요.
메서드
method Str
multi method Str(::?CLASS:D:)
캐시된 시퀀스를 문자열로 바꿔요.
method Stringy
multi method Stringy(::?CLASS:D:)
캐시된 시퀀스에 .Stringy를 호출해요.
method Numeric
method Numeric(::?CLASS:D:)
캐시된 시퀀스의 요소 개수를 돌려줘요.
method AT-POS
multi method AT-POS(::?CLASS:D: Int:D $idx)
multi method AT-POS(::?CLASS:D: int $idx)
캐시된 시퀀스에서 $idx 위치의 요소를 돌려줘요.
method EXISTS-POS
multi method EXISTS-POS(::?CLASS:D: Int:D $idx)
multi method EXISTS-POS(::?CLASS:D: int $idx)
캐시된 시퀀스의 $idx 위치에 요소가 있는지 나타내는 Bool을 돌려줘요.
method eager
method eager(::?CLASS:D: --> List:D)
invocant 시퀀스를 바탕으로 즉시 계산(eagerly evaluated)된 List를 돌려주고 그 시퀀스를 소비된 것으로 표시해요. 이미 소비된 Seq에서 호출하면 X::Seq::Consumed 오류를 던져요.
my $s = lazy 1..5;
say $s.is-lazy; # OUTPUT: «True»
say $s.eager; # OUTPUT: «(1 2 3 4 5)»
say $s.eager;
CATCH {
when X::Seq::Consumed {
say 'Throws exception if already consumed';
}
}
# OUTPUT: «Throws exception if already consumed»
method fmt
method fmt($format = '%s', $separator = ' ' --> Str:D)
캐시된 시퀀스를 형식 문자열대로 포맷해요.
method gist
multi method gist(::?CLASS:D:)
캐시된 시퀀스의 gist(요약 표현)를 돌려줘요.