For example, i have
data ShipDesign = ShipDesign {
offense :: Offense
, defense :: Defense
, maxHealth :: Integer
, repairRate :: Integer
, stealth :: Integer
, radar :: Integer
, speed :: Integer
, shipType :: String
...
}
Now i want to change the defense. The known way to do this is:
changeDefense :: (Defense -> Defense) -> ShipDesign -> ShipDesign
changeDefense fDef sd@(ShipDesign o d m rr s r sp st ...) = ShipDesign o (fDef d) m rr s r sp st ...
which isn't elegant. Especially in games its common to change just a few values per step.
My Question is: is there a library, design pattern or other way to change a single value in a more elegant manner?
The easiest way to find and replace multiple entries in Excel is by using the SUBSTITUTE function. The formula's logic is very simple: you write a few individual functions to replace an old value with a new one.
Resizing an Object Using the Scale FunctionWith the white square layer selected, click on the Edit menu, and select Transform>Scale. A bounding box with corner and side handles will appear around the white box. To quickly scale the box, click and drag any handle and the size of the box will change proportionally.
The “Change values of multiple fields” action is used to when more than one value is from the same source. Fig. 3. The configuration of the “Change values of multiple fields” action Data source – it is used to indicate the source or data source connection from which values indicated by action will be downloaded.
Tap the number 35 in any blank cell and copy it. 2. Select the range that you want to change values and right-click, and then choose Paste special… from the menu. See screenshot:
If you have Kutools for Excel installed, you can change the values in cells quickly with Operation Tools. Kutools for Excel: with more than 300 handy Excel add-ins, free to try with no limitation in 30 days. Get it Now Please apply this utility by clicking Kutools > More > Operation Tools, see screenshot:
Click OK, and all values of the range will be added 35. By using the Paste Special command, you can change values in a range by the Add, Subtract, Multiply and Divide operation to change values in a range without using a formula.
Yes, you can use record update notation:
changeDefense :: (Defense -> Defense) -> ShipDesign -> ShipDesign
changeDefense fDef sd = sd { defense = fDef (defense sd) }
As you work with it, though, the limitations of record update notation will show themselves, and you will want something more powerful. At that point, you should start to learn about lenses.
I wonder why nobody proposed lens(es)?
I recommend this short introduction by Gabriel Gonzalez: http://www.haskellforall.com/2013/05/program-imperatively-using-haskell.html?m=1
Edit: oh ... I over read the last sentence. But the suggestion still stands.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With