Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "change" single values in large objects in elegant way?

Tags:

haskell

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?

like image 443
Vektorweg Avatar asked Jul 08 '13 03:07

Vektorweg


People also ask

How do I replace multiple values in Excel with one value?

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.

How do I make a selected object smaller?

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.

What is the “change values of multiple fields” action?

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.

How to change value of 35 in a range in Excel?

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:

How to change the values in cells quickly with Operation tools?

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:

How to change values in a range without using a formula?

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.


2 Answers

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.

like image 126
luqui Avatar answered Sep 26 '22 02:09

luqui


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.

like image 30
fho Avatar answered Sep 24 '22 02:09

fho