# Please play code golf with my code

**URL:** <https://forums.fsharp.org/t/please-play-code-golf-with-my-code/3398>\
**Category:** General\
**Tags:** beginners, web\
**Created:** [November 4, 2023, 2:22am UTC](https://forums.fsharp.org/t/please-play-code-golf-with-my-code/3398 "2023-11-04T02:22:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![surferjeff](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/surferjeff/32/1158_2.png) [@surferjeff](https://forums.fsharp.org/u/surferjeff)\
**Post date:** [November 4, 2023, 2:22am UTC](https://forums.fsharp.org/t/please-play-code-golf-with-my-code/3398/1 "2023-11-04T02:22:41Z")

</div>

I’m new to F#. I wrote this function, but I suspect there’s a way to use some of F#'s operators to make it terser. What do you suggest?

```auto
let incrementHandler: HttpHandler =
    getAndStoreAntiforgeryTokens (fun tokens ->
        bindForm<IncrementForm> None (fun payload -> 
                htmlNodes (Views.counter payload.Count tokens)))

```

Full context: [https://github.com/surferjeff/blazor-compared/blob/43b40f1a66794749ff961cfa9e785b9af83a25f2/GiraffeApp/Program.fs#L46](https://github.com/surferjeff/blazor-compared/blob/43b40f1a66794749ff961cfa9e785b9af83a25f2/GiraffeApp/Program.fs#L46)

---

<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 4, 2023, 5:44pm UTC](https://forums.fsharp.org/t/please-play-code-golf-with-my-code/3398/2 "2023-11-04T17:44:52Z")

</div>

Fun! I strongly encourage you to stick with the code you have instead of what I wrote, but it was fun to try to reduce it down. Sometimes functions line up just right so that using partial application and compose can shorten it and stay readable, but in this case it needs enough contortions that I think it’s much less readable when you shorten it than your original code. My final form is

```auto
open FSharpx
let (<<+) f g x y = f (g x y)
let count p = p.Count 

let incrementHandler : HttpHandler =
  getAndStoreAntiforgeryTokens 
    (bindForm<IncrementForm> None << flip (htmlNodes <<+ Views.counter << count))

```

So I needed to define an extra operator `<<+` for a compose with two arguments, which doesn’t exist in the standard library. I also needed `flip` from FSharpx.Extras. I defined a function `count`, but in a couple weeks when F#8 lands I can get rid of that and just have

```auto
open FSharpx
let (<<+) f g x y = f (g x y)

let incrementHandler : HttpHandler =
  getAndStoreAntiforgeryTokens 
    (bindForm<IncrementForm> None << flip (htmlNodes <<+ Views.counter << _.Count))

```

Here’s the steps I took to reduce it:

```auto
let incrementHandler =
  getAndStoreAntiforgeryTokens (fun tokens ->
    bindForm<IncrementForm> None (fun payload -> 
      htmlNodes (Views.counter payload.Count tokens)))

let incrementHandler' =
  getAndStoreAntiforgeryTokens (fun tokens ->
    bindForm<IncrementForm> None <| (fun tokens payload -> 
      htmlNodes (Views.counter payload.Count tokens)) tokens)

let incrementHandler'' =
  getAndStoreAntiforgeryTokens 
    (bindForm<IncrementForm> None << (fun tokens payload -> 
      htmlNodes (Views.counter payload.Count tokens)))

let incrementHandler''' =
  getAndStoreAntiforgeryTokens 
    (bindForm<IncrementForm> None << flip (fun payload tokens -> 
      htmlNodes (Views.counter payload.Count tokens)))

let incrementHandler'''' =
  getAndStoreAntiforgeryTokens 
    (bindForm<IncrementForm> None << flip (fun payload -> 
      htmlNodes << Views.counter payload.Count))

let incrementHandler''''' =
  getAndStoreAntiforgeryTokens 
    (bindForm<IncrementForm> None << flip ((fun c -> 
      htmlNodes << Views.counter c) << count))

let incrementHandler'''''' =
  getAndStoreAntiforgeryTokens 
    (bindForm<IncrementForm> None << flip (htmlNodes <<+ Views.counter << count))

```

---

<div class="post-metadata">

**Author:** ![surferjeff](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/surferjeff/32/1158_2.png) [@surferjeff](https://forums.fsharp.org/u/surferjeff)\
**Post date:** [November 8, 2023, 4:33am UTC](https://forums.fsharp.org/t/please-play-code-golf-with-my-code/3398/3 "2023-11-08T04:33:05Z")

</div>

Thank you [ntwilson](https://forums.fsharp.org/u/ntwilson). This shows me the next things I need to learn in my F# journey: compose and flip.
