#제너레이터
피보나치 수열, 소수 스트림, 직접 만든 take_while 어댑터, 재귀 트리 순회를 yield fn을 써서 지연 반복자로 만듭니다.
#코드
nabi
// An endless Fibonacci stream.
yield fn fibonacci() -> i64 {
let (a, b) = (0, 1);
loop {
yield a;
(a, b) = (b, a + b);
}
}
// Primes by trial division against the primes found so far.
yield fn primes() -> i64 {
let found: List<i64> = [];
let n = 2;
loop {
let is_prime = true;
for p in found {
if p * p > n { break; }
if n % p == 0 { is_prime = false; break; }
}
if is_prime {
found.push(n);
yield n;
}
n += 1;
}
}
// A generic adapter: pass items through until `pred` fails.
yield fn take_while<I: Iterator>(it: I, pred: fn(I::Item) -> bool) -> I::Item {
for x in it {
if !pred(x) { return; }
yield x;
}
}
// A nested list, walked depth-first by a recursive generator.
enum Tree {
Leaf(str),
Branch(List<Tree>),
}
yield fn leaves(t: Tree) -> str {
match t {
Tree::Leaf(name) => yield name,
Tree::Branch(kids) => for k in kids {
for name in leaves(k) { yield name; }
},
}
}
let first = fibonacci().take(12).to_list();
println(f"fib: {first:?}");
let small = take_while(primes(), |p| p < 50).to_list();
println(f"primes < 50: {small:?}");
let even_fib_sum = take_while(fibonacci(), |x| x < 4_000_000)
.filter(|x| x % 2 == 0)
.sum();
println(f"sum of even fib < 4M: {even_fib_sum}");
// The 1000th prime: nothing past it is ever computed.
println(f"1000th prime: {primes().nth(999) ?? -1}");
// An inline generator block.
let squares = yield {
for i in 1..=5 { yield i * i; }
};
println(f"squares: {squares.to_list():?}");
let tree = Tree::Branch([
Tree::Leaf("a"),
Tree::Branch([Tree::Leaf("b"), Tree::Branch([Tree::Leaf("c")]), Tree::Leaf("d")]),
Tree::Leaf("e"),
]);
let names = leaves(tree).to_list();
println(f"leaves: {names:?}");
fib: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
primes < 50: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
sum of even fib < 4M: 4613732
1000th prime: 7919
squares: [1, 4, 9, 16, 25]
leaves: ["a", "b", "c", "d", "e"]
#설명
yield fn을 호출해도 본체가 바로 실행되지는 않습니다. 호출은 반복자GenIter<T>를 돌려줍니다.-> i64는 반환값이 아니라yield로 내보내는 항목의 타입입니다(4.11.3 명명된 제너레이터 함수).fibonacci()와primes()는 무한 제너레이터입니다. 지역 변수(a,b,found)는yield사이에 보존됩니다. 소비하는 쪽은take,nth등으로 필요한 만큼만 항목을 꺼냅니다(4.11.9 무한 제너레이터).- 제너레이터는
Iterator를 구현하므로filter,sum,to_list같은 어댑터와 조합할 수 있습니다.take_while을 직접 쓰면(표준 반복자에도 있습니다)return;으로 반복을 끝내는 제네릭yield fn하나로 만듭니다(컬렉션과 반복자, 제네릭). yield { ... }는 표현식 자리에서 바로 반복자를 만드는 인라인 제너레이터 블록입니다(4.11.4).leaves는 자기 자신을 호출해서 각 하위 트리의 값을 다시yield로 내보냅니다. 재귀 제너레이터는 중첩된 구조를 평평하게 펼칠 때 흔히 쓰는 방법입니다(합 타입).
참고
Node(Tree, i64, Tree)처럼 자기 자신을 직접 담는 enum은 크기가 무한하므로 컴파일러가 거부합니다(recursive type `Tree` has infinite size). 그래서 이 예제는 자식을 참조 타입인 List<Tree>에 담아 같은 구조를 나타냅니다.
잘못된 곳이 있나요? GitHub에서 이 페이지 고치기. 마지막 수정