Global operator overloading in F#

后端 未结 2 1869
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-30 11:20

I\'m starting down the road of defining my own operators for Cartesian products and matrix multiplication.

With matrices and vectors aliased as lists:



        
2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-30 11:56

    Here is the (non idiomatic) solution:

    type Mult = Mult with
        static member inline ($) (Mult, v1: 'a list) = fun (v2: 'b list) -> 
            v1 |> List.collect (fun x -> v2 |> List.map (fun y -> (x, y))) : list<'a * 'b>
        static member inline ($) (Mult, v1:'a      ) = fun (v2:'a) -> v1 * v2 :'a
    
    let inline (*) v1 v2 = (Mult $ v1) v2
    

提交回复
热议问题