如何在找到元素后增加块的元素?
How to increment element of block after found element?
给定 player-choices: ["rock" 0 "paper" 0 "scissors" 0]
如何通过搜索 "paper"
来增加此块中 "paper"
之后的值?
>> player-choices/("paper"): player-choices/("paper") + 1
== 1
您还可以保留对块中值位置的引用,以便稍后更改它们:
player-choices: ["rock" 0 "paper" 0 "scissors" 0]
rock-pos: find/tail player-choices "rock"
paper-pos: find/tail player-choices "paper"
scissors-pos: find/tail player-choices "scissors"
change paper-pos 1 + paper-pos/1
player-choices
; == ["rock" 0 "paper" 1 "scissors" 0]
另外请考虑,您可能不需要在数据块中使用字符串。
player-choices: [rock 0 paper 0 scissors 0]
player-choices/paper: player-choices/paper + 1
您还可以编写一个通用的 incr
函数,如下所示:
incr: function [
"Increments a value or series index"
value [scalar! series! any-word! any-path!] "If value is a word, it will refer to the incremented value"
/by "Change by this amount"
amount [scalar!]
][
amount: any [amount 1]
if integer? value [return add value amount] ;-- This speeds up our most common case by 4.5x
; though we are still 5x slower than just adding
; 1 to an int directly and doing nothing else.
; All this just to be smart about incrementing percents.
; The question is whether we want to do this, so the default 'incr
; call seems arguably nicer. But if we don't do this it is at
; least easy to explain.
if all [
integer? amount
1 = absolute amount
any [percent? value percent? attempt [get value]]
][amount: to percent! (1% * sign? amount)] ;-- % * int == float, so we cast.
case [
scalar? value [add value amount]
any [
any-word? value
any-path? value ;!! Check any-path before series.
][
op: either series? get value [:skip][:add]
set value op get value amount
:value ;-- Return the word for chaining calls.
]
series? value [skip value amount] ;!! Check series after any-path.
]
]
然后
incr 'player-choices/paper
Given player-choices: ["rock" 0 "paper" 0 "scissors" 0]
How could I increment the value after "paper" in this block by searching for "paper"?
poke player-choices index? next find player-choices "paper" 1 + select player-choices "paper"
细分:
>> ? poke
USAGE:
POKE series index value
DESCRIPTION:
Replaces the series value at a given index, and returns the new value.
- 系列玩家选择。
- 索引是"paper"
之后值在player-choices中的位置
- 该值为纸张加1后的当前值
查找将 return 找到值的系列,或 NONE。
>> find player-choices "paper"
== ["paper" 0 "scissors" 0]
但是你想要下一个值的索引因此:
>> index? next find player-choices "paper"
== 4
Select 将 return 系列的下一个值,如果它找到它之前的值。否则会 return none.
>> select pc "paper"
== 0
但是我们想把它加一,所以
>> 1 + select pc "paper"
== 1
给定 player-choices: ["rock" 0 "paper" 0 "scissors" 0]
如何通过搜索 "paper"
来增加此块中 "paper"
之后的值?
>> player-choices/("paper"): player-choices/("paper") + 1
== 1
您还可以保留对块中值位置的引用,以便稍后更改它们:
player-choices: ["rock" 0 "paper" 0 "scissors" 0]
rock-pos: find/tail player-choices "rock"
paper-pos: find/tail player-choices "paper"
scissors-pos: find/tail player-choices "scissors"
change paper-pos 1 + paper-pos/1
player-choices
; == ["rock" 0 "paper" 1 "scissors" 0]
另外请考虑,您可能不需要在数据块中使用字符串。
player-choices: [rock 0 paper 0 scissors 0]
player-choices/paper: player-choices/paper + 1
您还可以编写一个通用的 incr
函数,如下所示:
incr: function [
"Increments a value or series index"
value [scalar! series! any-word! any-path!] "If value is a word, it will refer to the incremented value"
/by "Change by this amount"
amount [scalar!]
][
amount: any [amount 1]
if integer? value [return add value amount] ;-- This speeds up our most common case by 4.5x
; though we are still 5x slower than just adding
; 1 to an int directly and doing nothing else.
; All this just to be smart about incrementing percents.
; The question is whether we want to do this, so the default 'incr
; call seems arguably nicer. But if we don't do this it is at
; least easy to explain.
if all [
integer? amount
1 = absolute amount
any [percent? value percent? attempt [get value]]
][amount: to percent! (1% * sign? amount)] ;-- % * int == float, so we cast.
case [
scalar? value [add value amount]
any [
any-word? value
any-path? value ;!! Check any-path before series.
][
op: either series? get value [:skip][:add]
set value op get value amount
:value ;-- Return the word for chaining calls.
]
series? value [skip value amount] ;!! Check series after any-path.
]
]
然后
incr 'player-choices/paper
Given player-choices: ["rock" 0 "paper" 0 "scissors" 0] How could I increment the value after "paper" in this block by searching for "paper"?
poke player-choices index? next find player-choices "paper" 1 + select player-choices "paper"
细分:
>> ? poke
USAGE:
POKE series index value
DESCRIPTION:
Replaces the series value at a given index, and returns the new value.
- 系列玩家选择。
- 索引是"paper" 之后值在player-choices中的位置
- 该值为纸张加1后的当前值
查找将 return 找到值的系列,或 NONE。
>> find player-choices "paper"
== ["paper" 0 "scissors" 0]
但是你想要下一个值的索引因此:
>> index? next find player-choices "paper"
== 4
Select 将 return 系列的下一个值,如果它找到它之前的值。否则会 return none.
>> select pc "paper"
== 0
但是我们想把它加一,所以
>> 1 + select pc "paper"
== 1