 3 people like it.

# Type-inference friendly division and multiplication

F# necessarily forces you to explicitly cast between int and float for operations such as division. This is necessary because implicit conversion would make type inference much harder. However having to cast all the time in your code can be a pain. These operators reduce the overhead to one or at most two characters of code.

 ``` 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: ``` ``````module Operators // Division and multiplication operators which cast ints // to floats for themselves. '.' on the side(s) which // need(s) to be cast. /// Floating point division given int and double args. let (./) x y = (x |> double) / y /// Floating point division given double and int args. let (/.) x y = x / (y |> double) /// Floating point division given int and int args. let (./.) x y = (x |> double) / (y |> double) /// Floating point multiplication given int and double args. let (.*) x y = (x |> double) * y /// Floating point multiplication given double and int args. let ( *. ) x y = x * (y |> double) /// Floating point multiplication given int and int args. let (.*.) x y = (x |> double) * (y |> double) /// Examples: let average = let numbers = [0..100] (numbers |> List.sum) ./. (numbers |> List.length) let average' = let numbers = [0. .. 100.] (numbers |> List.sum) /. (numbers |> List.length) let crazyTotal = [0. .. 100.] |> List.mapi (fun i x -> i .* x) |> List.sum ``````
Multiple items
module Operators

--------------------
module Operators

from Microsoft.FSharp.Core
val x : int
val y : float
Multiple items
val double : value:'T -> float (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.double

--------------------
type double = System.Double

Full name: Microsoft.FSharp.Core.double
val x : float
val y : int
val average : float

Full name: Operators.average

Examples:
val numbers : int list
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
| ( [] )
| ( :: ) of Head: 'T * Tail: 'T list
interface IEnumerable
interface IEnumerable<'T>
member IsEmpty : bool
member Item : index:int -> 'T with get
member Length : int
member Tail : 'T list
static member Cons : head:'T * tail:'T list -> 'T list
static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val sum : list:'T list -> 'T (requires member ( + ) and member get_Zero)

Full name: Microsoft.FSharp.Collections.List.sum
val length : list:'T list -> int

Full name: Microsoft.FSharp.Collections.List.length
val average' : float

Full name: Operators.average'
val numbers : float list
val crazyTotal : float

Full name: Operators.crazyTotal
val mapi : mapping:(int -> 'T -> 'U) -> list:'T list -> 'U list

Full name: Microsoft.FSharp.Collections.List.mapi
val i : int