2017-11-14 9 views
2

Rebol 2에서 어떻게 하나의 목록을 분할합니까? 파티션의 최종 순서는 신경 쓰지 않습니다.Rebol 2에서 목록을 분할하는 방법

이러한 붙박이 기능이없는 경우

lst: [1 5 6 5 5 2 1 3] 

probe partition lst ; Assuming identity function by default 
[[1 1] [5 5 5] [6] [2] [3]] 

probe partition/by lst even? 
[[6 2] [1 5 5 5 1 3]] 

, REBOL에 구축의 관용적 방법은 무엇입니까 :이 같은 함수 뭔가있을 것입니다 상상?

답변

3

에 대한 파티션 기능은 여기에 관용적 시도의

>> lst: [1 5 6 5 5 2 1 3] 
== [1 5 6 5 5 2 1 3] 

>> partition-unique lst 
== [[1 1] [2] [3] [5 5 5] [6]] 

>> partition-by lst :even? 
== [[6 2] [1 5 5 5 1 3]] 

그것은 하나 개의 함수로 다음을 결합하기 쉽다.

partition: func [ 
    s [block!] 
    /by f [any-function!] 
][ 
    either by [partition-by s :f] [partition-unique s] 
] 
+0

방언 내부에서 사용되는 Rebol 단어에 대한 문서를 찾기가 어렵습니다. 정렬 된 사본이 파서 드되어 같은 요소가 항상 서로 인접하여 발견되고 바깥 쪽 "any"는 입력이 끝날 때까지 반복적으로 매치하지만 안쪽 "copy"는 임의의 수의 인스턴스와 일치합니다. 같은 값을 사용하고 결과를 "유지/전용"을 사용하여 외부 "수집"에 전달되는 값으로 복사합니다. 예? – dukereg

+0

@dukereg - 수정하십시오. PARSE 사투리에서 ANY는 정규 표현식에서'*'와 같이 0 이상을 매치한다는 점을 강조하고 싶습니다. 그래서 위의 예제에서 사용되지 않은 일부는 정규 표현식에서'+'와 같이 하나 이상을 찾습니다. – draegtun

+1

@dukereg - 유용한 PARSE 링크 몇 군데 : http://www.rebol.com/docs/core23/rebolcore-15.html | https://github.com/gchiu/rebol.net/blob/master/wikipedia/Parse%20Project.wiki – draegtun

2

rebol.org/scripts/hof, 등에 일부 HOF가 있습니다.

partition-unique: func [ 
    s [block!] 
    /local got more 
][ 
    collect [ 
     parse sort copy s [ 
      any [ 
       copy got [ 
        set more skip 
        any more 
       ] (keep/only got) 
      ] 
     ] 
    ] 
] 

partition-by: func [ 
    s [block!] 
    f [any-function!] 
    /local result 
][ 
    result: reduce [make block! 0 make block! 0] 
    forall s [append pick result f s/1 s/1] 
    result 
] 

사용 예 : 두 번째 소원

partition: func [ 
    {Partition the series 'xs in two parts, 
    'success and 'failure - according to the 
    outcome of application of the predicate 'p 
    to all elements of 'xs. 
     ((a -+ logic) -+ [a] -> [[a] [a]]) } 
    p [any-function!] 
    xs [series!] 
    /local us vs result [block!] 
][ 
    us: copy [] 
    vs: copy [] 
    foreach k xs [ 
     either p :k [ 
      insert/only tail us :k 
     ][ 
      insert/only tail vs :k 
     ] 
    ] 
    result: copy [] 
    append/only result us 
    append/only result vs 
    result 
] 


>> partition func [a] [even? a] lst 
== [[6 2] [1 5 5 5 1 3]]