시퀀스

시퀀스 (Sequences)

시퀀스는 정렬된 값들의 모임을 캡슐화합니다. 시퀀스의 요소들은 for 구문 폼, sequence-generate가 반환하는 절차, 또는 시퀀스를 스트림으로 변환해 추출할 수 있습니다. 다양한 내장 데이터 타입이 시퀀스로 사용될 수 있고, 사용자 정의 시퀀스도 만들 수 있습니다.

출처: Racket Reference

본문

Racket Guide의 Sequence Constructors에서 시퀀스를 소개합니다.

시퀀스는 정렬된 값들의 모임을 캡슐화합니다. 시퀀스의 요소들은 for 구문 폼 중 하나, sequence-generate가 반환하는 절차, 또는 시퀀스를 스트림으로 변환하여 추출할 수 있습니다.

시퀀스 데이터 타입은 많은 다른 데이터 타입과 겹칩니다. 내장 데이터 타입 중 시퀀스 데이터 타입은 다음을 포함합니다:

  • 정확한 비음 정수(아래 참고);
  • 문자열(Strings 참고);
  • 바이트 문자열(Byte Strings 참고);
  • 리스트(Pairs and Lists 참고);
  • 가변 리스트(Mutable Pairs and Lists 참고);
  • 벡터(Vectors 참고);
  • flvector(Flonum Vectors 참고);
  • fxvector(Fixnum Vectors 참고);
  • 해시 테이블(Hash Tables 참고);
  • 딕셔너리(Dictionaries 참고);
  • 집합(Sets 참고);
  • 입력 포트(Ports 참고);
  • 스트림(Streams 참고).

비음 정수인 정확한 숫자 k(in-range k)와 유사한 시퀀스로 작동합니다. 단, k 자체는 스트림이 아니라는 점이 다릅니다.

사용자 정의 시퀀스는 구조체 타입 속성을 사용해 정의할 수 있습니다. 사용자 정의 시퀀스를 정의하는 가장 쉬운 방법은 gen:stream 제네릭 인터페이스를 사용하는 것입니다. 스트림은 직접 반복 가능한 자료 구조에 적합한 추상화입니다. 예를 들어 리스트는 firstrest로 직접 반복할 수 있습니다. 반면에 벡터는 직접 반복할 수 없습니다. 반복은 인덱스를 거쳐야 합니다. 직접 반복할 수 없는 자료 구조의 경우, 자료 구조에 대한 반복자(iterator)가 스트림으로 정의될 수 있습니다(예: 벡터의 인덱스를 포함하는 구조체).

예를 들어, 펼쳐진 연결 리스트(unrolled linked list, 벡터 리스트로 표현됨) 자체는 스트림 추상화에 맞지 않지만, 스트림으로 표현될 수 있는 인덱스 기반 반복자를 갖습니다:

예시:

> (struct unrolled-list-iterator (idx lst)
    #:methods gen:stream
    [(define (stream-empty? iter)
       (define lst (unrolled-list-iterator-lst iter))
       (or (null? lst)
           (and (>= (unrolled-list-iterator-idx iter)
                    (vector-length (first lst)))
                (null? (rest lst)))))
     (define (stream-first iter)
       (vector-ref (first (unrolled-list-iterator-lst iter))
                   (unrolled-list-iterator-idx iter)))
     (define (stream-rest iter)
       (define idx (unrolled-list-iterator-idx iter))
       (define lst (unrolled-list-iterator-lst iter))
       (if (>= idx (sub1 (vector-length (first lst))))
           (unrolled-list-iterator 0 (rest lst))
           (unrolled-list-iterator (add1 idx) lst)))])

> (define (make-unrolled-list-iterator ul)
    (unrolled-list-iterator 0 (unrolled-list-lov ul)))

> (struct unrolled-list (lov)
    #:property prop:sequence
    make-unrolled-list-iterator)

> (define ul1 (unrolled-list '(#(cracker biscuit) #(cookie scone))))
> (for/list ([x ul1]) x)

'(cracker biscuit cookie scone)

prop:sequence 속성은, 예를 들어 데이터를 반복에 대비해 준비하는 전처리 단계가 필요할 때와 같이, 반복을 지정하는 데 더 유연성을 제공합니다. make-do-sequence 함수는 시퀀스를 구현할 절차들을 반환하는 썽크가 주어지면 시퀀스를 만들고, prop:sequence 속성은 구조체 타입과 연관되어 그 구조체의 시퀀스로의 암시적 변환을 구현할 수 있습니다.

대부분의 시퀀스 타입에서, 시퀀스에서 요소를 추출하는 것은 원래 시퀀스 값에 부수 효과가 없습니다. 예를 들어 리스트에서 요소들의 시퀀스를 추출해도 리스트는 변하지 않습니다. 다른 시퀀스 타입에서는 각 추출이 부수 효과를 함의합니다. 예를 들어 포트에서 바이트들의 시퀀스를 추출하면 그 바이트들이 포트에서 읽힙니다. 시퀀스의 상태는 포트에서처럼 시퀀스의 모든 사용에 걸쳐 있을 수도 있고, for 폼, sequence->stream, sequence-generate, sequence-generate*가 시퀀스를 시작하는 매번 별개의 시간으로 국한될 수도 있습니다. 구체적으로, make-do-sequence에 전달된 썽크는 시퀀스가 사용될 때마다 시퀀스를 시작하도록 호출됩니다. 따라서 서로 다른 시퀀스들은 여러 번 시작될 때 다르게 동작합니다.

> (define (double-initiate s1)
    ; initiate the sequence twice
    (define-values (more?.1 next.1) (sequence-generate s1))
    (define-values (more?.2 next.2) (sequence-generate s1))
    ; alternate fetching from sequence via the two initiations
    (list (next.1) (next.2) (next.1) (next.2)))

> (double-initiate (open-input-string "abcdef"))

'(97 98 99 100)
> (double-initiate (list 97 98 99 100))

'(97 97 98 98)
> (double-initiate (in-naturals 97))

'(97 97 98 98)

또한, 시퀀스의 이후 요소들은 sequence-generate의 첫 번째 결과를 호출하는 것만으로 "소비"될 수 있습니다(두 번째 결과가 결코 호출되지 않아도).

> (define (double-initiate-and-use-more? s1)
    ; initiate the sequence twice
    (define-values (more?.1 next.1) (sequence-generate s1))
    (define-values (more?.2 next.2) (sequence-generate s1))
    ; alternate fetching from sequence via the two initiations
    ; but this time call `more?` in between
    (list (next.1) (more?.1) (next.2) (more?.2)
          (next.1) (more?.1) (next.2) (more?.2)))

> (double-initiate-and-use-more? (open-input-string "abcdef"))

'(97 #t 99 #t 98 #t 100 #t)

이 예시에서, sequence-generate에 대한 첫 번째 호출에 포함된 상태는 more?.1의 호출만으로도 98을 "가져갑니다".

시퀀스의 개별 요소는 보통 단일 값에 해당하지만, 요소가 여러 값에 해당할 수도 있습니다. 예를 들어 해시 테이블은 시퀀스의 각 요소에 대해 두 값을 만듭니다—키와 그 값.

Sequence Predicate and Constructors

procedure

(sequence? v) → boolean?
  v : any/c

v를 시퀀스로 사용할 수 있으면 #t를, 그렇지 않으면 #f를 반환합니다.

예시:

> (sequence? 42)

#t
> (sequence? '(a b c))

#t
> (sequence? "word")

#t
> (sequence? #\x)

#f

procedure

(in-range end) → stream?
  end : real?
(in-range start end [step]) → stream?
  start : real?
  end : real?
  step : real? = 1

요소들이 숫자인 시퀀스(또한 스트림)를 반환합니다. 인자 하나짜리 (in-range end)(in-range 0 end 1)과 동등합니다. 시퀀스의 첫 번째 숫자는 start이고, 각 연속 요소는 이전 요소에 step을 더해 생성됩니다. step이 음이 아니면 end보다 크거나 같을 요소 앞에서, step이 음수이면 end보다 작거나 같을 요소 앞에서 시퀀스가 멈춥니다.

in-range 호출은 for 절에 직접 나타나면 숫자 반복에 대해 더 나은 성능을 제공할 수 있습니다.

예시: 가우스 합

> (for/sum ([x (in-range 10)]) x)

45

예시: 짝수들의 합

> (for/sum ([x (in-range 0 100 2)]) x)

2450

step으로 0이 주어지면 in-range는 무한 시퀀스를 반환합니다. step이 매우 작은 숫자이고 step 또는 시퀀스 요소가 부동소수점 숫자일 때도 무한 시퀀스를 반환할 수 있습니다.

procedure

(in-inclusive-range start end [step]) → stream?
  start : real?
  end : real?
  step : real? = 1

in-range와 비슷하지만, 마지막 요소가 end와 같도록 허용되도록 시퀀스 중지 조건이 변경되었습니다.

in-inclusive-range 호출은 for 절에 직접 나타나면 숫자 반복에 대해 더 나은 성능을 제공할 수 있습니다.

예시:

> (sequence->list (in-inclusive-range 7 11))

'(7 8 9 10 11)
> (sequence->list (in-inclusive-range 7 11 2))

'(7 9 11)
> (sequence->list (in-inclusive-range 7 10 2))

'(7 9)

base 패키지의 8.0.0.13 버전에서 추가되었습니다.

procedure

(in-naturals [start]) → stream?
  start : exact-nonnegative-integer? = 0

start에서 시작하는(각 요소가 앞선 요소보다 1 더 큰) 정확한 정수들의 무한 시퀀스(또한 스트림)를 반환합니다.

in-naturals 호출은 for 절에 직접 나타나면 정수 반복에 대해 더 나은 성능을 제공할 수 있습니다.

예시:

> (for/list ([k (in-naturals)]
             [x (in-range 10)])
    (list k x))

'((0 0) (1 1) (2 2) (3 3) (4 4) (5 5) (6 6) (7 7) (8 8) (9 9))

procedure

(in-list lst) → stream?
  lst : list?

lst를 직접 시퀀스로 사용하는 것과 동등한 시퀀스(또한 스트림)를 반환합니다.

리스트를 시퀀스로 사용하는 것에 대한 정보는 Pairs and Lists를 참고하세요.

in-list 호출은 for 절에 직접 나타나면 리스트 반복에 대해 더 나은 성능을 제공할 수 있습니다.

반복 중 리스트 요소의 도달 가능성에 대한 정보는 for를 참고하세요.

예시:

> (for/list ([x (in-list '(3 1 4))])
    `(,x ,(* x x)))

'((3 9) (1 1) (4 16))

base 패키지의 6.7.0.4 버전에서 변경됨: for에서 리스트의 요소 도달 가능성 보장을 개선함.

procedure

(in-mlist mlst) → sequence?
  mlst : mlist?

mlst와 동등한 시퀀스를 반환합니다. mlst가 가변 리스트일 것으로 기대되지만, in-mlist는 처음에 mlst가 가변 쌍인지 null인지만 검사합니다. 반복 중에 바뀔 수 있기 때문입니다.

가변 리스트를 시퀀스로 사용하는 것에 대한 정보는 Mutable Pairs and Lists를 참고하세요.

in-mlist 호출은 for 절에 직접 나타나면 가변 리스트 반복에 대해 더 나은 성능을 제공할 수 있습니다.

예시:

> (for/list ([x (in-mlist (mcons "RACKET" (mcons "LANG" '())))])
    (string-length x))

'(6 4)

procedure

(in-vector vec [start stop step]) → sequence?
  vec : vector?
  start : exact-nonnegative-integer? = 0
  stop : (or/c exact-integer? #f) = #f
  step : (and/c exact-integer? (not/c zero?)) = 1

선택적 인자가 제공되지 않으면 vec와 동등한 시퀀스를 반환합니다.

벡터를 시퀀스로 사용하는 것에 대한 정보는 Vectors를 참고하세요.

선택적 인자 start, stop, stepin-range와 유사하지만, stop#f 값이 (vector-length vec)와 동등하다는 점이 다릅니다. 즉 시퀀스의 첫 요소는 (vector-ref vec start)이고, 각 연속 요소는 이전 요소의 인덱스에 step을 더해 생성됩니다. step이 음이 아니면 end보다 크거나 같을 인덱스 앞에서, step이 음수이면 end보다 작거나 같을 인덱스 앞에서 시퀀스가 멈춥니다.

start가 유효한 인덱스가 아니면 exn:fail:contract 예외가 발생합니다. 단 start, stop, (vector-length vec)가 같으면 결과는 빈 시퀀스입니다.

예시:

> (for ([x (in-vector (vector 1) 1)]) x)
> (for ([x (in-vector (vector 1) 2)]) x)

in-vector: starting index is out of range

  starting index: 2

  valid range: [0, 0]

  vector: '#(1)
> (for ([x (in-vector (vector) 0 0)]) x)
> (for ([x (in-vector (vector 1) 1 1)]) x)

stop[-1, (vector-length vec)]에 없으면 exn:fail:contract 예외가 발생합니다.

startstop보다 작고 step이 음수이면 exn:fail:contract 예외가 발생합니다. 마찬가지로 startstop보다 크고 step이 양수이면 exn:fail:contract 예외가 발생합니다.

in-vector 호출은 for 절에 직접 나타나면 벡터 반복에 대해 더 나은 성능을 제공할 수 있습니다.

예시:

> (define (histogram vector-of-words)
    (define a-hash (make-hash))
    (for ([word (in-vector vector-of-words)])
      (hash-set! a-hash word (add1 (hash-ref a-hash word 0))))
    a-hash)

> (histogram #("hello" "world" "hello" "sunshine"))

'#hash(("hello" . 2) ("sunshine" . 1) ("world" . 1))

procedure

(in-string str [start stop step]) → sequence?
  str : string?
  start : exact-nonnegative-integer? = 0
  stop : (or/c exact-integer? #f) = #f
  step : (and/c exact-integer? (not/c zero?)) = 1

선택적 인자가 제공되지 않으면 str와 동등한 시퀀스를 반환합니다.

문자열을 시퀀스로 사용하는 것에 대한 정보는 Strings를 참고하세요.

선택적 인자 start, stop, stepin-vector와 같습니다.

in-string 호출은 for 절에 직접 나타나면 문자열 반복에 대해 더 나은 성능을 제공할 수 있습니다.

예시:

> (define (line-count str)
    (for/sum ([ch (in-string str)])
      (if (char=? #\newline ch) 1 0)))

> (line-count "this string\nhas\nthree \nnewlines")

3

procedure

(in-bytes bstr [start stop step]) → sequence?
  bstr : bytes?
  start : exact-nonnegative-integer? = 0
  stop : (or/c exact-integer? #f) = #f
  step : (and/c exact-integer? (not/c zero?)) = 1

선택적 인자가 제공되지 않으면 bstr와 동등한 시퀀스를 반환합니다.

바이트 문자열을 시퀀스로 사용하는 것에 대한 정보는 Byte Strings를 참고하세요.

선택적 인자 start, stop, stepin-vector와 같습니다.

in-bytes 호출은 for 절에 직접 나타나면 바이트 문자열 반복에 대해 더 나은 성능을 제공할 수 있습니다.

예시:

> (define (has-eof? bs)
    (for/or ([ch (in-bytes bs)])
      (= ch 0)))

> (has-eof? #"this byte string has an \0embedded zero byte")

#t
> (has-eof? #"this byte string does not")

#f

procedure

(in-port [r in]) → sequence?
  r : (input-port? . -> . any/c) = read
  in : input-port? = (current-input-port)

in에 대해 r을 호출해 eof를 만들어낼 때까지 만들어진 값들을 요소로 하는 시퀀스를 반환합니다.

procedure

(in-input-port-bytes in) → sequence?
  in : input-port?

(in-port read-byte in)와 동등한 시퀀스를 반환합니다.

procedure

(in-input-port-chars in) → sequence?
  in : input-port?

요소들이 in에서 문자로 읽히는 시퀀스((in-port read-char in)과 동등)를 반환합니다.

procedure

(in-lines [in mode]) → sequence?
  in : input-port? = (current-input-port)
  mode : (or/c 'linefeed 'return 'return-linefeed 'any 'any-one)
   = 'any

(in-port (lambda (p) (read-line p mode)) in)과 동등한 시퀀스를 반환합니다. 기본 mode'any인 반면 read-line의 기본 mode'linefeed라는 점에 주의하세요.

procedure

(in-bytes-lines [in mode]) → sequence?
  in : input-port? = (current-input-port)
  mode : (or/c 'linefeed 'return 'return-linefeed 'any 'any-one)
   = 'any

(in-port (lambda (p) (read-bytes-line p mode)) in)과 동등한 시퀀스를 반환합니다. 기본 mode'any인 반면 read-bytes-line의 기본 mode'linefeed라는 점에 주의하세요.

procedure

(in-hash hash) → sequence?
  hash : hash?
(in-hash hash bad-index-v) → sequence?
  hash : hash?
  bad-index-v : any/c

bad-index-v가 제공되지 않으면 hash와 동등한 시퀀스를 반환합니다.

hash-map처럼, in-hash를 통한 반복은 가변 해시 테이블에 대한 특정 수정에 순회(traversal)가 진행되는 동안 적응할 수 있습니다. 순회하는 스레드가 제거하거나 다시 매핑한 키들은 즉각적인 역효과가 없습니다. 키를 이미 봤다면 그 변경은 순회에 영향을 주지 않고, 그렇지 않으면 순회는 삭제된 키를 건너뛰거나 다시 매핑된 키의 새 값을 사용합니다.

다른 스레드에 의한 키 제거를 포함한 다른 동시 수정은, 기대한 항목 키가 그 키나 값을 가져오기 전에 제거되었다면 건너뛴 항목이나 예외를 초래할 수 있습니다. bad-index-v가 제공되면, 해시가 동시에 수정되어 반복이 유효한 해시 인덱스를 갖지 못하는 경우 bad-index-v가 키와 값 둘 다로 반환됩니다. bad-index-v를 제공하는 것은 키가 약하게 보유된 해시 테이블을 반복할 때 특히 유용한데, 항목들이 비동기적으로 제거될 수 있기 때문입니다(즉, in-hash가 다른 반복에 착수한 후, 다음 반복을 위해 항목에 접근하기 전에).

예시:

> (define table (hash 'a 1 'b 2))
> (for ([(key value) (in-hash table)])
    (printf "key: ~a value: ~a\n" key value))

key: b value: 2

key: a value: 1

해시 테이블을 시퀀스로 사용하는 것에 대한 정보는 Hash Tables를 참고하세요.

base 패키지의 7.0.0.10 버전에서 변경됨: 선택적 bad-index-v 인자 추가. 8.18.0.11 버전에서 변경됨: 가변 해시 테이블에 대한 같은-스레드 수정으로 순회에 대한 보장을 강화함.

procedure

(in-hash-keys hash) → sequence?
  hash : hash?
(in-hash-keys hash bad-index-v) → sequence?
  hash : hash?
  bad-index-v : any/c

요소들이 hash의 키들인 시퀀스를 반환합니다. bad-index-vin-hash와 같은 방식으로 사용하고, in-hash와 유사한 동시 수정 보장을 갖습니다.

예시:

> (define table (hash 'a 1 'b 2))
> (for ([key (in-hash-keys table)])
    (printf "key: ~a\n" key))

key: b

key: a

base 패키지의 7.0.0.10 버전에서 변경됨: 선택적 bad-index-v 인자 추가. 8.18.0.11 버전에서 변경됨: 가변 해시 테이블에 대한 같은-스레드 수정으로 순회에 대한 보장을 강화함.

procedure

(in-hash-values hash) → sequence?
  hash : hash?
(in-hash-values hash bad-index-v) → sequence?
  hash : hash?
  bad-index-v : any/c

요소들이 hash의 값들인 시퀀스를 반환합니다. bad-index-vin-hash와 같은 방식으로 사용하고, in-hash와 유사한 동시 수정 보장을 갖습니다.

예시:

> (define table (hash 'a 1 'b 2))
> (for ([value (in-hash-values table)])
    (printf "value: ~a\n" value))

value: 2

value: 1

base 패키지의 7.0.0.10 버전에서 변경됨: 선택적 bad-index-v 인자 추가. 8.18.0.11 버전에서 변경됨: 가변 해시 테이블에 대한 같은-스레드 수정으로 순회에 대한 보장을 강화함.

procedure

(in-hash-pairs hash) → sequence?
  hash : hash?
(in-hash-pairs hash bad-index-v) → sequence?
  hash : hash?
  bad-index-v : any/c

요소들이 쌍들인 시퀀스를 반환합니다. 각 쌍은 hash에서 온 키와 그 값을 포함합니다(hash를 직접 시퀀스로 사용해 각 요소에 대해 키와 값을 별개의 값으로 얻는 것과는 대조적).

bad-index-v 인자는 제공되면 in-hash가 사용하는 것과 같은 방식으로 사용됩니다. 유효하지 않은 인덱스를 만나면, 시퀀스의 쌍은 bad-index-vcarcdr 둘 다로 갖습니다. in-hash-pairs의 동시 수정 보장은 in-hash의 것과 유사합니다.

예시:

> (define table (hash 'a 1 'b 2))
> (for ([key+value (in-hash-pairs table)])
    (printf "key and value: ~a\n" key+value))

key and value: (b . 2)

key and value: (a . 1)

base 패키지의 7.0.0.10 버전에서 변경됨: 선택적 bad-index-v 인자 추가. 8.18.0.11 버전에서 변경됨: 가변 해시 테이블에 대한 같은-스레드 수정으로 순회에 대한 보장을 강화함.

procedure

(in-mutable-hash hash) → sequence?
  hash : (and/c hash? (not/c immutable?) hash-strong?)

procedure

(in-mutable-hash hash bad-index-v) → sequence?
  hash : (and/c hash? (not/c immutable?) hash-strong?)
  bad-index-v : any/c

procedure

(in-mutable-hash-keys hash) → sequence?
  hash : (and/c hash? (not/c immutable?) hash-strong?)

procedure

(in-mutable-hash-keys hash bad-index-v) → sequence?
  hash : (and/c hash? (not/c immutable?) hash-strong?)
  bad-index-v : any/c

procedure

(in-mutable-hash-values hash) → sequence?
  hash : (and/c hash? (not/c immutable?) hash-strong?)

procedure

(in-mutable-hash-values hash bad-index-v) → sequence?
  hash : (and/c hash? (not/c immutable?) hash-strong?)
  bad-index-v : any/c

procedure

(in-mutable-hash-pairs hash) → sequence?
  hash : (and/c hash? (not/c immutable?) hash-strong?)

procedure

(in-mutable-hash-pairs hash bad-index-v) → sequence?
  hash : (and/c hash? (not/c immutable?) hash-strong?)
  bad-index-v : any/c

procedure

(in-immutable-hash hash) → sequence?
  hash : (and/c hash? immutable?)

procedure

(in-immutable-hash hash bad-index-v) → sequence?
  hash : (and/c hash? immutable?)
  bad-index-v : any/c

procedure

(in-immutable-hash-keys hash) → sequence?
  hash : (and/c hash? immutable?)

procedure

(in-immutable-hash-keys hash bad-index-v) → sequence?
  hash : (and/c hash? immutable?)
  bad-index-v : any/c

procedure

(in-immutable-hash-values hash) → sequence?
  hash : (and/c hash? immutable?)

procedure

(in-immutable-hash-values hash bad-index-v) → sequence?
  hash : (and/c hash? immutable?)
  bad-index-v : any/c

procedure

(in-immutable-hash-pairs hash) → sequence?
  hash : (and/c hash? immutable?)

procedure

(in-immutable-hash-pairs hash bad-index-v) → sequence?
  hash : (and/c hash? immutable?)
  bad-index-v : any/c

procedure

(in-weak-hash hash) → sequence?
  hash : (and/c hash? hash-weak?)

procedure

(in-weak-hash hash bad-index-v) → sequence?
  hash : (and/c hash? hash-weak?)
  bad-index-v : any/c

procedure

(in-weak-hash-keys hash) → sequence?
  hash : (and/c hash? hash-weak?)

procedure

(in-weak-hash-keys hash bad-index-v) → sequence?
  hash : (and/c hash? hash-weak?)
  bad-index-v : any/c

procedure

(in-weak-hash-values hash) → sequence?
  hash : (and/c hash? hash-weak?)

procedure

(in-weak-hash-keys hash bad-index-v) → sequence?
  hash : (and/c hash? hash-weak?)
  bad-index-v : any/c

procedure

(in-weak-hash-pairs hash) → sequence?
  hash : (and/c hash? hash-weak?)

procedure

(in-weak-hash-pairs hash bad-index-v) → sequence?
  hash : (and/c hash? hash-weak?)
  bad-index-v : any/c

procedure

(in-ephemeron-hash hash) → sequence?
  hash : (and/c hash? hash-ephemeron?)

procedure

(in-ephemeron-hash hash bad-index-v) → sequence?
  hash : (and/c hash? hash-ephemeron?)
  bad-index-v : any/c

procedure

(in-ephemeron-hash-keys hash) → sequence?
  hash : (and/c hash? hash-ephemeron?)

procedure

(in-ephemeron-hash-keys hash bad-index-v) → sequence?
  hash : (and/c hash? hash-ephemeron?)
  bad-index-v : any/c

procedure

(in-ephemeron-hash-values hash) → sequence?
  hash : (and/c hash? hash-ephemeron?)

procedure

(in-ephemeron-hash-keys hash bad-index-v) → sequence?
  hash : (and/c hash? hash-ephemeron?)
  bad-index-v : any/c

procedure

(in-ephemeron-hash-pairs hash) → sequence?
  hash : (and/c hash? hash-ephemeron?)

procedure

(in-ephemeron-hash-pairs hash bad-index-v) → sequence?
  hash : (and/c hash? hash-ephemeron?)
  bad-index-v : any/c

특정 종류의 해시 테이블에 대한 시퀀스 생성자들입니다. 이것들은 유사한 in-hash 폼보다 더 나은 성능을 낼 수 있습니다.

base 패키지의 6.4.0.6 버전에서 추가됨. 7.0.0.10 버전에서 변경됨: 선택적 bad-index-v 인자 추가. 8.0.0.10 버전에서 변경됨: ephemeron 변형 추가.

procedure

(in-directory [dir use-dir?]) → (sequence/c path?)
  dir : (or/c #f path-string?) = #f
  use-dir? : ((and/c path? complete-path?) . -> . any/c)
   = (lambda (dir-path) #t)

dir 안의 파일, 디렉터리, 링크에 대한 모든 경로들을 만들어내는 시퀀스(use-dir?#f를 반환하는 어떤 디렉터리의 내용은 제외)를 반환합니다. dir#f가 아니면 만들어지는 모든 경로는 dir을 접두어로 시작합니다. dir#f이면 현재 디렉터리 안의 그리고 그에 상대적인 경로들이 만들어집니다.

in-directory 시퀀스는 중첩된 하위 디렉터리들을 재귀적으로 순회합니다(use-dir?로 거름). 디렉터리의 즉시 내용만 포함하는 시퀀스를 만드려면 directory-list의 결과를 시퀀스로 사용하세요.

각 디렉터리의 즉시 내용은 path<?로 정렬된 것으로 보고되고, 하위 디렉터리의 내용은 디렉터리 안의 이후 경로들보다 먼저 보고됩니다.

예시:

> (current-directory (path-only (collection-file-path "main.rkt" "info")))
> (for/list ([f (in-directory)])
     f)

'(#<path:compiled>

  #<path:compiled/main_rkt.dep>

  #<path:compiled/main_rkt.zo>

  #<path:main.rkt>)

> (for/list ([f (in-directory "compiled")])
    f)

'(#<path:compiled/main_rkt.dep> #<path:compiled/main_rkt.zo>)
> (for/list ([f (in-directory #f (lambda (p)
                                   (not (regexp-match? #rx"compiled" p))))])
     f)

'(#<path:compiled> #<path:main.rkt>)

base 패키지의 6.0.0.1 버전에서 변경됨: use-dir? 인자 추가. 6.6.0.4 버전에서 변경됨: 정렬된 결과 보장 추가.

procedure

(in-producer producer) → sequence?
  producer : procedure?
(in-producer producer stop arg ...) → sequence?
  producer : procedure?
  stop : any/c
  arg : any/c

보통 어떤 상태를 사용해 작업을 수행하는 producer에 대한 연속 호출들로부터 온 값들을 포함하는 시퀀스를 반환합니다.

stop 값이 주어지지 않으면 시퀀스는 무한히 계속되며, 따라서 유한 시퀀스나 #:break 등과 함께 사용하는 것이 일반적입니다. stop 값이 주어지면, 시퀀스의 끝을 표시하는 값을 식별하는 데 사용됩니다(그리고 stop 값은 시퀀스에 포함되지 않습니다). stopproducer의 결과에 적용되는 술어일 수도 있고, producer의 결과에 대해 eq?로 검사되는 값일 수도 있습니다. (stop 값 자체가 함수이거나 producer가 여러 값을 반환하면 stop 인자는 술어여야 합니다.)

추가 arg들이 지정되면, 그것들은 producer에 대한 모든 호출에 전달됩니다.

예시:

> (define (counter)
    (define n 0)
    (lambda ([d 1]) (set! n (+ d n)) n))

> (for/list ([x (in-producer (counter))] [y (in-range 4)]) x)

'(1 2 3 4)
> (for/list ([x (in-producer (counter))] #:break (= x 5)) x)

'(1 2 3 4)
> (for/list ([x (in-producer (counter) 5)]) x)

'(1 2 3 4)
> (for/list ([x (in-producer (counter) 5 1/2)]) x)

'(1/2 1 3/2 2 5/2 3 7/2 4 9/2)
> (for/list ([x (in-producer read eof (open-input-string "1 2 3"))]) x)

'(1 2 3)

procedure

(in-value v) → sequence?
  v : any/c

단일 값 v를 만들어내는 시퀀스를 반환합니다.

이 폼은 for*/list 같은 폼에서 let-형 바인딩에 주로 유용합니다. 하지만 더 최근에 추가된 #:do 절 폼이 같은 용도의 상당수를 다룹니다.

procedure

(in-indexed seq) → sequence?
  seq : sequence?

각 요소가 두 값을 갖는 시퀀스를 반환합니다: seq가 만들어내는 값과, 0에서 시작하는 비음 정확한 정수. seq의 요소들은 단일 값이어야 합니다.

예시:

> (for ([(ch i) (in-indexed "hello")])
    (printf "The char at position ~a is: ~a\n" i ch))

The char at position 0 is: h

The char at position 1 is: e

The char at position 2 is: l

The char at position 3 is: l

The char at position 4 is: o

procedure

(in-sequences seq ...) → sequence?
  seq : sequence?

모든 입력 시퀀스로 이루어진 하나씩 차례로 이어붙인 시퀀스를 반환합니다. 각 seq는 앞선 seq가 소진된 후에만 시작됩니다. 단일 seq가 제공되면 seq가 반환됩니다. 그렇지 않으면 각 seq의 요소들은 모두 같은 수의 값을 가져야 합니다.

procedure

(in-cycle seq ...) → sequence?
  seq : sequence?

in-sequences와 비슷하지만, 시퀀스들이 무한 순환으로 반복되며, 각 seq는 각 반복에서 새로 시작됩니다. seq가 제공되지 않거나 모든 seq가 비어 있게 되면, in-cycle이 만든 시퀀스는 요소가 요구될 때—심지어 모든 seq가 처음에 비어 있으면 시퀀스가 시작될 때조차—결코 반환하지 않는다는 점에 주의하세요.

procedure

(in-parallel seq ...) → sequence?
  seq : sequence?

각 요소가 제공된 seq의 수만큼의 값을 갖는 시퀀스를 반환합니다. 값들은 순서대로 각 seq의 값입니다. 각 seq의 요소들은 단일 값이어야 합니다.

procedure

(in-parallel-values n seq ... ...) → sequence?
  n : exact-nonnegative-integer?
  seq : sequence?

각 요소가 제공된 seq들이 만들어내는 값 수의 합만큼의 값을 갖는 시퀀스를 반환합니다. 각 seq 앞에는 그것이 만들어내는 값 수 n이 옵니다(그래서 결과 값 수는 n들의 합입니다). 새 시퀀스의 값들은 순서대로 각 seq의 값들입니다.

base 패키지의 9.0.0.2 버전에서 추가되었습니다.

procedure

(in-values-sequence seq) → sequence?
  seq : sequence?

seq와 같지만, seq의 각 요소에 대한 여러 값들을 요소들의 리스트로 결합하는 시퀀스를 반환합니다.

procedure

(in-values*-sequence seq) → sequence?
  seq : sequence?

seq와 같지만, seq의 요소가 여러 값 또는 단일 리스트 값을 가질 때 값들이 리스트로 결합되는 시퀀스를 반환합니다. 다시 말해, in-values*-sequence는 리스트가 아닌 단일 값 요소를 리스트로 감싸지 않는다는 점만 빼면 in-values-sequence와 같습니다.

procedure

(stop-before seq pred) → sequence?
  seq : sequence?
  pred : (any/c . -> . any)

pred를 요소에 적용한 결과가 #t가 되는 마지막 요소까지의 seq(단일 값이어야 함)의 요소들을 포함하고, 그 후에 끝나는 시퀀스를 반환합니다.

procedure

(stop-after seq pred) → sequence?
  seq : sequence?
  pred : (any/c . -> . any)

pred를 요소에 적용한 결과가 #t가 되는 요소(포함)까지의 seq(단일 값이어야 함)의 요소들을 포함하고, 그 후에 끝나는 시퀀스를 반환합니다.

procedure

(make-do-sequence thunk) → sequence?
  thunk : (or/c (-> (values (any/c . -> . any)
                  (any/c . -> . any/c)
                  any/c
                  (or/c (any/c . -> . any/c) #f)
                  (or/c (any/c ... . -> . any/c) #f)
                  (or/c (any/c any/c ... . -> . any/c) #f)))
      (-> (values (any/c . -> . any)
                  (or/c (any/c . -> . any/c) #f)
                  (any/c . -> . any/c)
                  any/c
                  (or/c (any/c . -> . any/c) #f)
                  (or/c (any/c ... . -> . any/c) #f)
                  (or/c (any/c any/c ... . -> . any/c) #f))))

요소들이 thunk에 따라 생성되는 시퀀스를 반환합니다.

thunk가 호출되면 시퀀스가 시작됩니다. 시작된 시퀀스는 위치(position)에 따라 정의되는데, 위치는 init-pos로 초기화되고, 요소(여러 값으로 구성될 수 있음)가 그 뒤를 따릅니다.

thunk 절차는 여섯 또는 일곱 개의 값을 반환해야 합니다. 다만 이 여러 값을 직접 나열하는 대신 initiate-sequence를 사용해 반환하세요.

thunk가 여섯 개의 값을 반환하면:

  • 첫 번째 결과는 현재 위치를 받아 현재 요소의 값(들)을 반환하는 pos->element 절차입니다.
  • 두 번째 결과는 현재 위치를 받아 다음 위치를 반환하는 next-pos 절차입니다.
  • 세 번째 결과는 초기 위치인 init-pos 값입니다.
  • 네 번째 결과는 현재 위치를 받아, 시퀀스가 현재 위치의 값(들)을 포함하면 참 결과를, 값(들)을 포함하는 대신 시퀀스가 끝나야 하면 거짓을 반환하는 continue-with-pos? 함수입니다. 또는 continue-with-pos?#f일 수 있는데, 이는 시퀀스가 항상 현재 값(들)을 포함해야 함을 나타냅니다. 이 함수는 pos->element가 사용되기 전에 각 위치에서 검사됩니다.
  • 다섯 번째 결과는 continue-with-pos?와 같지만 현재 위치 대신 현재 요소 값(들)을 인자로 받는 continue-with-val? 함수입니다. 또는 continue-with-val?#f일 수 있는데, 이는 시퀀스가 항상 현재 위치의 값(들)을 포함해야 함을 나타냅니다.
  • 여섯 번째 결과는 현재 위치와 현재 요소 값(들)을 둘 다 받아, 현재 요소가 이미 시퀀스에 포함된 후에 시퀀스가 끝나는지 결정하는 continue-after-pos+val? 절차입니다. 또는 continue-after-pos+val?#f일 수 있는데, 이는 시퀀스가 현재 값(들) 뒤에서 항상 계속될 수 있음을 나타냅니다.

thunk가 일곱 개의 값을 반환하면, 첫 번째 결과는 여전히 pos->element 절차입니다. 그러나 두 번째 결과는 이제 아래에서 더 설명되는 early-next-pos 절차입니다. 또는 early-next-pos#f일 수 있는데, 이는 항등 함수와 동등합니다. 다른 결과들의 위치는 하나씩 이동하므로, 세 번째 결과는 이제 next-pos이고, 네 번째 결과는 이제 init-pos가 됩니다.

early-next-pos 절차는 현재 위치를 받아 갱신된 위치를 반환합니다. 이 갱신된 위치는 next-poscontinue-after-pos+val?에 사용되지만, continue-with-pos?에는 사용되지 않습니다(그것은 원래의 현재 위치를 사용합니다). early-next-pos의 의도는 루프가 시퀀스 값을 처리하는 동안 값이 도달 가능하게 유지되는 것을 피하려고 위치를 증가시켜야 하는 시퀀스를 지원하는 것입니다. 그래서 early-next-pospos->element 직후에 적용됩니다. continue-after-pos+val? 함수는 그 함수에 공급할 값을 보유하지 않도록 #f여야 합니다.

위에 나열된 각 절차는 위치당 한 번만 호출됩니다. continue-with-pos?, continue-with-val?, continue-after-pos+val? 중 하나가 #f를 반환하는 즉시 시퀀스는 끝나고, 어떤 것도 다시 호출되지 않습니다. 보통 함수들 중 하나가 끝 조건을 결정하고, 다른 두 함수 자리에는 #f가 사용됩니다.

base 패키지의 6.7.0.4 버전에서 변경됨: 선택적 두 번째 결과에 대한 지원 추가.

value

prop:sequence : struct-type-property?

구조체 인스턴스를 받아 시퀀스를 반환하는 절차를 구조체 타입에 연관시킵니다. v가 이 속성을 가진 구조체 타입의 인스턴스이면 (sequence? v)#t를 만들어냅니다.

기존 시퀀스 사용하기:

예시:

> (struct my-set (table)
    #:property prop:sequence
    (lambda (s)
      (in-hash-keys (my-set-table s))))

> (define (make-set . xs)
    (my-set (for/hash ([x (in-list xs)])
              (values x #t))))

> (for/list ([c (make-set 'celeriac 'carrot 'potato)])
    c)

'(potato celeriac carrot)

make-do-sequence 사용하기:

예시:

> (require racket/sequence)
> (struct train (car next)
    #:property prop:sequence
    (lambda (t)
      (make-do-sequence
       (lambda ()
         (initiate-sequence
          #:pos->element train-car
          #:next-pos train-next
          #:init-pos t
          #:continue-with-pos? (lambda (t) t))))))

> (for/list ([c (train 'engine
                       (train 'boxcar
                              (train 'caboose
                                     #f)))])
    c)

'(engine boxcar caboose)

Sequence Conversion

procedure

(sequence->stream seq) → stream?
  seq : sequence?

시퀀스를 stream-firststream-rest 연산을 지원하는 스트림으로 변환합니다. 스트림의 생성은 시퀀스를 적극적으로(eagerly) 시작하지만, 스트림은 시퀀스에서 요소를 지연적으로(lazily) 끌어내며, stream-first가 스트림에 적용될 때마다 같은 결과를 만들도록 각 요소를 캐시합니다.

seq에서 요소를 추출하는 것이 부수 효과와 관련되어 있다면, 그 효과는 stream-first 또는 stream-rest가 요소에 접근하거나 건너뛰기 위해 처음 사용될 때마다 수행됩니다.

시퀀스 자체가 상태를 가질 수 있으므로, 같은 seq에 대한 sequence->stream의 여러 호출이 반드시 독립적이지는 않다는 점에 주의하세요.

예시:

> (define inport (open-input-bytes (bytes 1 2 3 4 5)))
> (define strm (sequence->stream inport))
> (stream-first strm)

1
> (stream-first (stream-rest strm))

2
> (stream-first strm)

1
> (define strm2 (sequence->stream inport))
> (stream-first strm2)

3
> (stream-first (stream-rest strm2))

4

procedure

(sequence-generate seq) → (-> boolean?) (-> any)

  seq : sequence?

시퀀스를 시작하고 시퀀스에서 요소를 추출할 두 개의 썽크를 반환합니다. 첫 번째는 시퀀스에 더 많은 값이 사용 가능하면 #t를 반환합니다. 두 번째는 시퀀스에서 다음 요소(여러 값일 수 있음)를 반환합니다. 더 이상 요소가 없으면 exn:fail:contract 예외가 발생합니다.

시퀀스 자체가 상태를 가질 수 있으므로, 같은 seq에 대한 sequence-generate의 여러 호출이 반드시 독립적이지는 않다는 점에 주의하세요.

예시:

> (define inport (open-input-bytes (bytes 1 2 3 4 5)))
> (define-values (more? get) (sequence-generate inport))
> (more?)

#t
> (get)

1
> (get)

2
> (define-values (more2? get2) (sequence-generate inport))
> (list (get2) (get2) (get2))

'(3 4 5)
> (more2?)

#f

procedure

(sequence-generate* seq)
→ (or/c list? #f)
(-> (values (or/c list? #f) procedure?))

  seq : sequence?

sequence-generate와 같지만, 시퀀스의 첫 번째 요소에 대한 값들의 리스트(시퀀스가 비어 있으면 #f)와 시퀀스를 계속하기 위한 썽크를 반환함으로써 (시퀀스 내재된 것 외에는) 상태를 피합니다. 썽크의 결과는 sequence-generate*의 결과와 같지만 시퀀스의 두 번째 요소에 대한 것이며, 이렇게 계속됩니다. 요소 결과가 #f(시퀀스에 더 이상 값이 없음을 나타냄)일 때 썽크가 호출되면 exn:fail:contract 예외가 발생합니다.

Additional Sequence Operations

(require racket/sequence) ; package: base

이 섹션에 문서화된 바인딩은 racket/sequenceracket 라이브러리가 제공하지만, racket/base는 제공하지 않습니다.

value

empty-sequence : sequence?

요소가 없는 시퀀스입니다.

procedure

(sequence->list s) → list?
  s : sequence?

요소들이 s의 요소들인 리스트를 반환합니다. 각 요소는 단일 값이어야 합니다. s가 무한하면 이 함수는 종료하지 않습니다.

procedure

(sequence-length s) → exact-nonnegative-integer?
  s : sequence?

모든 요소를 추출하고 버려서 s의 요소 수를 반환합니다. s가 무한하면 이 함수는 종료하지 않습니다.

procedure

(sequence-ref s i) → any
  s : sequence?
  i : exact-nonnegative-integer?

s의 i번째 요소를 반환합니다(여러 값일 수 있음).

procedure

(sequence-tail s i) → sequence?
  s : sequence?
  i : exact-nonnegative-integer?

i개 요소가 생략된 s와 동등한 시퀀스를 반환합니다.

s를 시작하는 것이 부수 효과와 관련되어 있다면, 결과 시퀀스가 시작될 때까지 s는 시작되지 않으며, 그 시점에 첫 i개 요소가 시퀀스에서 추출됩니다.

procedure

(sequence-append s ...) → sequence?
  s : sequence?

각 시퀀스의 모든 요소를 원래 시퀀스들에서 나타나는 순서대로 포함하는 시퀀스를 반환합니다. 새 시퀀스는 지연적으로 구성됩니다.

주어진 모든 s가 스트림이면 결과도 스트림입니다.

procedure

(sequence-map f s) → sequence?
  f : procedure?
  s : sequence?

s의 각 요소에 f를 적용한 결과를 포함하는 시퀀스를 반환합니다. 새 시퀀스는 지연적으로 구성됩니다.

s가 스트림이면 결과도 스트림입니다.

procedure

(sequence-andmap f s) → boolean?
  f : (-> any/c ... boolean?)
  s : sequence?

s의 모든 요소에 대해 f가 참 결과를 반환하면 #t를 반환합니다. s가 무한하고 f가 결코 거짓 결과를 반환하지 않으면 이 함수는 종료하지 않습니다.

procedure

(sequence-ormap f s) → boolean?
  f : (-> any/c ... boolean?)
  s : sequence?

s의 어떤 요소에 대해 f가 참 결과를 반환하면 #t를 반환합니다. s가 무한하고 f가 결코 참 결과를 반환하지 않으면 이 함수는 종료하지 않습니다.

procedure

(sequence-for-each f s) → void?
  f : (-> any/c ... any)
  s : sequence?

s의 각 요소에 f를 적용합니다. s가 무한하면 이 함수는 종료하지 않습니다.

procedure

(sequence-fold f i s) → any/c
  f : (-> any/c any/c ... any/c)
  i : any/c
  s : sequence?

s의 각 요소에 대해 i를 초기 누산기로 하여 f를 접습니다. s가 무한하면 이 함수는 종료하지 않습니다. f 함수는 누산기를 첫 번째 인자로, 다음 시퀀스 요소를 두 번째 인자로 받습니다.

procedure

(sequence-count f s) → exact-nonnegative-integer?
  f : procedure?
  s : sequence?

s에서 f가 참 결과를 반환하는 요소의 수를 반환합니다. s가 무한하면 이 함수는 종료하지 않습니다.

procedure

(sequence-filter f s) → sequence?
  f : (-> any/c ... boolean?)
  s : sequence?

s에서 f가 참 결과를 반환하는 요소들로 이루어진 시퀀스를 반환합니다. 새 시퀀스는 지연적으로 구성되지만, sf가 참 결과를 반환하는 두 요소 사이에 f가 거짓 결과를 반환하는 요소를 무한히 갖고 있으면, 이 시퀀스에 대한 연산은 무한 하위 시퀀스 구간에서 종료하지 않습니다.

s가 스트림이면 결과도 스트림입니다.

procedure

(sequence-add-between s e) → sequence?
  s : sequence?
  e : any/c

s의 요소들로 이루어지되 s의 각 요소 쌍 사이에 e가 들어가는 시퀀스를 반환합니다. 새 시퀀스는 지연적으로 구성됩니다.

s가 스트림이면 결과도 스트림입니다.

예시:

> (let* ([all-reds (in-cycle '("red"))]
         [red-and-blues (sequence-add-between all-reds "blue")])
    (for/list ([n (in-range 10)]
               [elt red-and-blues])
      elt))

'("red" "blue" "red" "blue" "red" "blue" "red" "blue" "red" "blue")
> (for ([text (sequence-add-between '("veni" "vidi" "duci") ", ")])
    (display text))

veni, vidi, duci

procedure

(sequence/c [#:min-count min-count]
  elem/c ...) → contract?

  min-count : (or/c #f exact-nonnegative-integer?) = #f
  elem/c : contract?

시퀀스를 감싸서, elem/c 계약 수만큼의 값들을 갖는 요소들을 만들어내고 각 값이 대응하는 elem/c를 만족하도록 의무지웁니다. 결과가 원래 값과 같은 종류의 시퀀스일 것이라고는 보장되지 않습니다. 예를 들어 감싸진 리스트가 list?를 만족할 것이라고는 보장되지 않습니다.

min-count가 숫자이면 스트림은 적어도 그만큼의 요소를 가져야 합니다.

예시:

> (define/contract predicates
    (sequence/c (-> any/c boolean?))
    (in-list (list integer?
                   string->symbol)))

> (for ([P predicates])
    (printf "~s\n" (P "cat")))

#f

predicates: broke its own contract

  promised: boolean?

  produced: 'cat

  in: an element of

      (sequence/c (-> any/c boolean?))

  contract from: (definition predicates)

  blaming: (definition predicates)

   (assuming the contract is correct)

  at: eval:55:0
> (define/contract numbers&strings
    (sequence/c number? string?)
    (in-dict (list (cons 1 "one")
                   (cons 2 "two")
                   (cons 3 'three))))

> (for ([(N S) numbers&strings])
    (printf "~s: ~a\n" N S))

1: one

2: two

numbers&strings: broke its own contract

  promised: string?

  produced: 'three

  in: an element of

      (sequence/c number? string?)

  contract from: (definition numbers&strings)

  blaming: (definition numbers&strings)

   (assuming the contract is correct)

  at: eval:57:0
> (define/contract a-sequence
    (sequence/c #:min-count 2 char?)
    "x")

> (for ([x a-sequence]
        [i (in-naturals)])
    (printf "~a is ~a\n" i x))

0 is x

a-sequence: broke its own contract

  promised: a sequence that contains at least 2 values

  produced: "x"

  in: (sequence/c #:min-count 2 char?)

  contract from: (definition a-sequence)

  blaming: (definition a-sequence)

   (assuming the contract is correct)

  at: eval:59:0

Additional Sequence Constructors and Functions

procedure

(in-syntax stx) → sequence?
  stx : syntax?

stx의 연속되는 하위 부분들(subparts)을 요소로 하는 시퀀스를 만들어냅니다. (stx->list lst)와 동등합니다.

in-syntax 호출은 for 절에 직접 나타나면 구문 반복에 대해 더 나은 성능을 제공할 수 있습니다.

예시:

> (for/list ([x (in-syntax #'(1 2 3))])
    x)

'(#<syntax:eval:61:0 1> #<syntax:eval:61:0 2> #<syntax:eval:61:0 3>)

base 패키지의 6.3 버전에서 추가되었습니다.

procedure

(in-slice length seq) → sequence?
  length : exact-positive-integer?
  seq : sequence?

요소들이 seq의 첫 length개 요소로 이루어진 리스트들을 갖고, 그 다음 length개, 이런 식으로 계속되는 시퀀스를 반환합니다.

예시:

> (for/list ([e (in-slice 3 (in-range 8))]) e)

'((0 1 2) (3 4 5) (6 7))

base 패키지의 6.3 버전에서 추가되었습니다.

procedure

(initiate-sequence
  #:pos->element pos->element
  [#:early-next-pos early-next-pos]
  #:next-pos next-pos
  #:init-pos init-pos
  [#:continue-with-pos? continue-with-pos?
  #:continue-with-val? continue-with-val?
  #:continue-after-pos+val? continue-after-pos+val?])

→ (any/c . -> . any)
(or/c (any/c . -> . any) #f)
(any/c . -> . any/c)
any/c
(or/c (any/c . -> . any/c) #f)
(or/c (any/c ... . -> . any/c) #f)
(or/c (any/c any/c ... . -> . any/c) #f)

  pos->element : (any/c . -> . any)
  early-next-pos : (or/c (any/c . -> . any) #f) = #f
  next-pos : (any/c . -> . any/c)
  init-pos : any/c
  continue-with-pos? : (or/c (any/c . -> . any/c) #f) = #f
  continue-with-val? : (or/c (any/c ... . -> . any/c) #f) = #f
  continue-after-pos+val? : (or/c (any/c any/c ... . -> . any/c) #f)
   = #f

make-do-sequence의 썽크 인자에 적합한 값들을 반환합니다. 각 인자의 의미는 make-do-sequence를 참고하세요.

예시:

> (define (in-alt-list xs)
    (make-do-sequence
     (λ ()
       (initiate-sequence
        #:pos->element car
        #:next-pos (λ (xs) (cdr (cdr xs)))
        #:init-pos xs
        #:continue-with-pos? pair?
        #:continue-after-pos+val? (λ (xs _) (pair? (cdr xs)))))))

> (sequence->list (in-alt-list '(1 2 3 4 5 6)))

'(1 3 5)
> (sequence->list (in-alt-list '(1 2 3 4 5 6 7)))

'(1 3 5 7)

base 패키지의 8.10.0.5 버전에서 추가되었습니다.

더 알아보기

  • 시퀀스 생성자에 대한 Racket Guide 문서
  • 스트림(Streams)과 집합(Sets) 관련 문서
  • for 구문 폼 관련 문서