为了玩Stretch the word,我定义了以下词,以尝试通过与此答案相同的方法解决问题:
USING: kernel math sequences sequences.repeating ;
IN: stretch-words
! "bonobo" -> { "b" "bo" "bon" "bono" "bonob" "bonobo" }
: ascend-string ( string -- ascending-seqs )
dup length 1 + iota [ 0 swap pick subseq ] map
[ "" = not ] filter nip ;
! expected: "bonobo" -> "bonoobbooo"
! actual: "bonobo" -> "bbbooonnnooobbbooo"
: stretch-word ( string -- stretched )
dup ascend-string swap zip
[
dup first swap last
[ = ] curry [ dup ] dip count
repeat
] map last ;
stretch-word
应该按照它在字符串中出现的次数重复字符串中的字符。但是,我的实现是重复它获得的 1string 的所有实例。
我觉得这在 Factor 中很容易实现,但我不太明白。我如何让它做我想要的?