Current dronesgroup total life

I am storing a

List (Maybe Hitpoints)

in botmemory from

droneGroupInLocalSpace

How can I add all of them using their Int form? I would like to take my List (Maybe Hitpoints) and sum up all their parts to reach a total. And when this numbers goes down between snapshot of memory I want a bool flag of True.

I’m having a hard time reading the Maybe hitpoints value inside the list.

What can I use?

currentDroneLife =
            case currentReading.dronesWindow of
                Nothing ->
                    []

                Just dronesWindow ->
                    case dronesWindow.droneGroupInLocalSpace of
                        Nothing ->
                            []

                        Just droneGroupInLocalSpace ->
                            case droneGroupInLocalSpace.drones |> List.map .hitpointsPercent of
                                [] ->
                                    []

                                listHitpoints ->
                                    listHitpoints -- List (Maybe Hitpoints)
droneDamaged =
            if currentDroneLife >= botMemoryBefore.currentDroneLife || then
                False

            else
                True

I think I found it. Did not test yet but I got to find the Int.

currentDroneLife =
            case currentReading.dronesWindow of
                Nothing ->
                    0

                Just dronesWindow ->
                    case dronesWindow.droneGroupInLocalSpace of
                        Nothing ->
                            0

                        Just droneGroupInLocalSpace ->
                            case droneGroupInLocalSpace.drones |> List.map .hitpointsPercent of
                                [] ->
                                    0

                                listhitpointsmaybe ->
                                    case Maybe.Extra.combine listhitpointsmaybe of
                                        Nothing ->
                                            0

                                        Just totaldroneshitpoints ->
                                            let
                                                shield =
                                                    (totaldroneshitpoints |> List.map .shield) |> List.sum

                                                struct =
                                                    (totaldroneshitpoints |> List.map .structure) |> List.sum

                                                armor =
                                                    (totaldroneshitpoints |> List.map .armor) |> List.sum
                                            in
                                            shield + struct + armor -- All integers

It works! When drones start taking damage they get pulled back in and back out to reset the enemy target.

Added this to my drone management function

 else if context.memory.droneDamaged then
                            Just
                                (describeBranch "Drones are taking damage. Recalling."
                                    (useContextMenuCascade
                                        ( "drones group", droneGroupInLocalSpace.header.uiNode )
                                        (useMenuEntryWithTextContaining "Return to drone bay" menuCascadeCompleted)
                                        readingFromGameClient
                                    )
                                )

This is 1 spot where I’m sure I could benefit from understand how to use >> in elm.

I still dont understand when or how to use >>

1 Like

Thanks for sharing your experience!
As far as I understand, the summing problem is already solved, so I will focus on the >> operator.

Thanks for sharing your experience!
As far as I understand, the summing problem is already solved, so I will focus on the >> operator.

Most importantly, you don’t need this operator when writing code because you can express any function without it.

To quote the guide on |> and >>:

[…]
These operators don’t do anything that we couldn’t achieve with other language elements. They only allow us to write a function differently. We can rewrite any program without them. But, if these are not even strictly necessary, why do we even use them? The reason is that they can help improve the readability of the program code. Just like abbreviations in natural language, they act as shortcuts to consolidate common patterns in our code.
[…]

There are several ways to learn how this operator works.
One way is to look at its definition in the Elm core library here:

When we use it in the form of >>, the first argument goes to the operator’s left and the second to the right. I think this arrangement is the meaning of ‘infix’ in the term ‘infix operator’.

Another way to learn is an interactive exploration in the Elm REPL. Here we get fast feedback to see what happens when we use this operator:

Here is a guide on the |> and >> operators: https://to.botengine.org/guide/elm-programming-language-forward-pipe-and-function-composition-operators: bots/guide/elm-programming-language-forward-pipe-and-function-composition-operators.md at main · Viir/bots · GitHub

If something is missing in the guide, I will update it.