Modifying the series using map or any custom functions

I have a function which takes the 1st value from a masurement every 5mins.

ever5Mins1st = from(bucket: "Historian/oneday")
  |> range(start: dashboardTime)
  |> filter(fn: (r) => 
    r._measurement == "InventoryStock" and
    r._field =="Value"  and
    r.Location=="NYC"
  )
 |>window(every:5m)
 |>first()
 |>window(every:inf)

Now If I want to do a difference of two consecutive points , I can do by using difference function

 ever5Mins1st
 |>difference()

But what If I do I want a sum of every consecutive points or every 5 points.

I know I can write custom functions which recieves piped data. But do I need to write a for loop? Are there any examples of for loops and conditional statements?

// Function definition
add = (tables=<-) =>
  tables
    |> map(fn: (r) => //What should I do here, for loop?)

Or this has to be a custom function created in Golang and then contribute to the open source?

@suvvenndu To create a custom aggregate function, you wouldn’t use map(), you’d use reduce() (introduced in Flux 0.23). Here’s a guide on it: https://v2.docs.influxdata.com/v2.0/query-data/guides/custom-functions/custom-aggregate/

But I’m not sure you need to create a custom function to do this. Is there a reason you can’t just use the sum() function?