I wrote a simple example for my script. I create a switch record type
type State = | On | Off with member this.flip = match this with | On -> Off | Off -> On type Switch = { State : State }
and then I will write a function that creates a copy of the record with one element changed
let flip switch = { switch with State = switch.State.flip }
I write in flip many consecutive times
let flipMany times switch = [1 .. times] |> List.fold (fun (sw : Switch) _ -> flip sw) switch
If I want to put these two functions in the record as methods, I will write instead
type Switch = { State : State } member this.flip = { this with State = this.State.flip } member this.flipMany times = [1 .. times] |> List.fold (fun (sw : Switch) _ -> sw.flip) this
Is there anything wrong with this? Is it equally effective? Feeling a little uncomfortable by calling the sw.flip function on a different object each time.
Edit: this is just a simple example to explain my question. My question is how flipMany function compare with the flipMany method in a record. The implementation may be naive, but in both cases it is the same.
Panos source share