最优雅的方法是通过跳过每2个元素来提取块

时间:2017-11-17 17:56:30

标签: rebol red

假设我有

block: [a 1 b 2 c 3]

我想要

[1 2 3]

像这样的东西是笨重的,它不起作用,因为我使用单词类型(我想用单词而不是字符串):

  block: [a 1 b 2 c 3]
  block2: []

  counter: -1
  foreach 'element block [
    counter: negate counter
    if counter append block2 element
  ]

1 个答案:

答案 0 :(得分:6)

EXTRACT功能应符合以下条款:

>> extract/index [a 1 b 2 c 3] 2 2
== [1 2 3]

这种类型的东西相当通用。

>> help extract
USAGE:
     EXTRACT series width

DESCRIPTION: 
     Extracts a value from a series at regular intervals. 
     EXTRACT is a function! value.

ARGUMENTS:
     series       [series!] 
     width        [integer!] "Size of each entry (the skip)".

REFINEMENTS:
     /index       => Extract from an offset position.
        pos          [integer!] "The position".
     /into        => Provide an output series instead of creating a new one.
        output       [series!] "Output series".