# Generalize Similar Functions

**URL:** <https://forums.fsharp.org/t/generalize-similar-functions/3396>\
**Category:** General\
**Tags:** beginners\
**Created:** [October 30, 2023, 10:27am UTC](https://forums.fsharp.org/t/generalize-similar-functions/3396 "2023-10-30T10:27:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![FabBadDog](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/fabbaddog/32/1260_2.png) [@FabBadDog](https://forums.fsharp.org/u/FabBadDog)\
**Post date:** [October 30, 2023, 10:27am UTC](https://forums.fsharp.org/t/generalize-similar-functions/3396/1 "2023-10-30T10:27:50Z")

</div>

Hey there,

I have a lot of functions that are very similar:

```auto
    let addCustomer customerNumber firstName data =
        let customer = Customer.create customerNumber firstName
        { data with Customers = data.Customers @ [customer] }

    let addOrder orderNumber name data =
        let order = Order.create orderNumber name
        { data with Orders = data.Orders @ [order] }
    
    let addInvoice id amount name data =
        let invoice = Invoice.create id amount name
        { data with Invoices = data.Invoices @ [invoice] }

```

I feel like there should be a way to extract the common behavior, but I am struggling to find a way.

Any ideas?

Thx

---

<div class="post-metadata">

**Author:** ![altbodhi](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/altbodhi/32/1253_2.png) [@altbodhi](https://forums.fsharp.org/u/altbodhi)\
**Post date:** [October 30, 2023, 1:52pm UTC](https://forums.fsharp.org/t/generalize-similar-functions/3396/2 "2023-10-30T13:52:07Z")

</div>

All functions as different as an apple and an orange. Outside similar, but inside no.

---

<div class="post-metadata">

**Author:** ![ntwilson](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/ntwilson/32/984_2.png) [@ntwilson](https://forums.fsharp.org/u/ntwilson)\
**Post date:** [November 2, 2023, 9:46pm UTC](https://forums.fsharp.org/t/generalize-similar-functions/3396/3 "2023-11-02T21:46:58Z")

</div>

Yeah I don’t think there’s a lot of opportunity. If you wrote setters for each field of your data record like

```auto
module Data = 
  let updateCustomers fn x = { x with Customers = fn x.Customers }
  let updateOrders fn x = { x with Orders = fn x.Orders }
  ...

```

then you could kinda shorten it with

```auto
let snoc x xs = xs @ [x]

let addCustomer customerNumber firstName = Data.updateCustomers (snoc (Customer.create customerNumber firstName))
let addOrder orderNumber name = Data.updateOrders (snoc (Order.create orderNumber name))
let addInvoice id amount name = Data.updateInvoices (snoc (Invoice.create id amount name))

```

which cuts a little bit of boilerplate I guess? You could slightly update that to use [lenses from F#+](https://fsprojects.github.io/FSharpPlus/lens.html):

```auto
module Data = 
  let _customers f d = f d.Customers <&> fun x -> { d with Customers = x }
  let _orders f d = f d.Orders <&> fun x -> { d with Orders = x }

...

let snoc x xs = xs @ [x]

let addCustomer customerNumber firstName = over Data._customers (snoc (Customer.create customerNumber firstName))
let addOrder orderNumber name = over Data._orders (snoc (Order.create orderNumber name))
let addInvoice id amount name = over Data._invoices (snoc (Invoice.create id amount name))

```

which is at least a bit more standardized of a pattern than the first one, though requires anyone working with it to have a basic familiarity with lenses.

I can’t think of any other ways to shorten it.

---

<div class="post-metadata">

**Author:** ![FabBadDog](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/fabbaddog/32/1260_2.png) [@FabBadDog](https://forums.fsharp.org/u/FabBadDog)\
**Post date:** [November 3, 2023, 1:22pm UTC](https://forums.fsharp.org/t/generalize-similar-functions/3396/4 "2023-11-03T13:22:51Z")

</div>

Thank you for taking the time to provide some ideas.
