这是一个惯用的尝试:
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
]
使用示例:
>> 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]
]