【问题标题】:Most elegant way to extract block by skipping every 2 element通过跳过每 2 个元素来提取块的最优雅方法
【发布时间】:2017-11-17 17:56:30
【问题描述】:

假设我有

block: [a 1 b 2 c 3]

我想要

[1 2 3]

这样的东西很笨重,而且它不起作用,因为我使用的是 word 类型(我希望它是 word 而不是字符串):

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

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

【问题讨论】:

  • 除了 EXTRACT 函数(见下文),您的变通解决方案再次使用 COLLECT 看起来更优雅:collect [foreach [key value] block [keep value]]

标签: rebol red


【解决方案1】:

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".

【讨论】:

    猜你喜欢
    • 2013-05-23
    • 1970-01-01
    • 2017-06-06
    • 1970-01-01
    • 2019-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多