扩展单词和引用范围
Stretching words and quotation scoping
在Stretch the word, I've defined the following words, to try to work at the problem via the same method as this answer播放:
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 的 all 个实例。
我觉得这在 Factor 中很容易实现,但我不太明白。我怎样才能让它做我想做的事?
嗯...不是很好的高尔夫球,但它很有效...
首先,我对 ascend-string
做了一个小改动,因此它将字符串留在堆栈上:
: ascend-string ( string -- string ascending-seqs )
dup length 1 + iota [ 0 swap pick subseq ] map
[ "" = not ] filter ;
所以stretch-word
可以这样工作:
: stretch-word ( string -- stretched )
ascend-string zip ! just zip them in the same order
[
first2 over ! first2 is the only golf I could make :/
[ = ] curry count ! same thing
swap <array> >string ! make an array of char size count and make it a string
] map concat ; ! so you have to join the pieces
编辑:
我认为问题在于使用 repeat 来完成这项工作。
: ascend-string ( string -- seqs )
"" [ suffix ] { } accumulate*-as ;
: counts ( string -- counts )
dup ascend-string [ indices length ] { } 2map-as ;
: stretch-word ( string -- stretched )
[ counts ] keep [ <string> ] { } 2map-as concat ;
"bonobo" stretch-word print
bonoobbooo
indices length
也可以是 [ = ] with count
在Stretch the word, I've defined the following words, to try to work at the problem via the same method as this answer播放:
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 的 all 个实例。
我觉得这在 Factor 中很容易实现,但我不太明白。我怎样才能让它做我想做的事?
嗯...不是很好的高尔夫球,但它很有效...
首先,我对 ascend-string
做了一个小改动,因此它将字符串留在堆栈上:
: ascend-string ( string -- string ascending-seqs )
dup length 1 + iota [ 0 swap pick subseq ] map
[ "" = not ] filter ;
所以stretch-word
可以这样工作:
: stretch-word ( string -- stretched )
ascend-string zip ! just zip them in the same order
[
first2 over ! first2 is the only golf I could make :/
[ = ] curry count ! same thing
swap <array> >string ! make an array of char size count and make it a string
] map concat ; ! so you have to join the pieces
编辑: 我认为问题在于使用 repeat 来完成这项工作。
: ascend-string ( string -- seqs )
"" [ suffix ] { } accumulate*-as ;
: counts ( string -- counts )
dup ascend-string [ indices length ] { } 2map-as ;
: stretch-word ( string -- stretched )
[ counts ] keep [ <string> ] { } 2map-as concat ;
"bonobo" stretch-word print
bonoobbooo
indices length
也可以是 [ = ] with count