What functional programming is responsible for overriding?

I have updated both with functional programming and with F # coming from OOP and C # background, and I noticed that in functional programming methods are most often static in modules by type (and I can understand why).

For example, to work with, listyou can use the functions found in the module list, to work with optionthere option, etc.

However, what happens when I accept a base class parameter, and the derived class has a more appropriate implementation for it?

In OOP, the corresponding implementation, which is a derived implementation, will be called, although the object is statically a base class, since the element has been redefined .

How would it be in functional programming?

If you have a basic type as a parameter (as it should be), you can only work with functions for it, just as you can only call basic elements in OOP.
The only difference is that in OOP, the called member is suitable.

+4
source share
2 answers

, , Polymorphism. , - IEnumerable. , FP, : Ad-Hoc Parametric.

Type Classes, Haskell T , T C, > "" , , T, , , , -time.

, fmap Monad bind return, fmap list / option, , .

100%, , # Linq, SelectMany, bind.

, F # , (. FsControl), F # , .NET( ), SubTyping.

, , , -- , , SubTyping. , FP , " ", , , " " match (, | _ -> ) .

UPDATE

:

type MyType = 
    static member Nth (x:seq<_>) = fun n -> printfn "General case"; Seq.nth n x 
    static member Nth (x:_ [])   = fun n -> printfn "Override for []"; x.[n]

let bigSeq = seq {1..10000000}
let bigLst = [ 1..10000000 ]
let bigArr = [|1..10000000|]

MyType.Nth bigSeq 9000000
// General case
// Real: 00:00:00.217, CPU: 00:00:00.218, GC gen0: 0, gen1: 0, gen2: 0

MyType.Nth bigArr 9000000
// Override for []
// Real: 00:00:00.001, CPU: 00:00:00.000, GC gen0: 0, gen1: 0, gen2: 0

MyType.Nth bigLst 9000000
// General case
// Real: 00:00:00.080, CPU: 00:00:00.078, GC gen0: 0, gen1: 0, gen2: 0

[]:

MyType.Nth bigArr 9000000
// General case
// Real: 00:00:00.052, CPU: 00:00:00.046, GC gen0: 0, gen1: 0, gen2: 0

, ad-hoc- ().

.NET , , , , .

, Type Class Collection, , seq, nth, F #, - :

// General implementation relaying in IEnumerable<_>
let nth n (x:Collection<_>) = Seq.nth n

// Specific implementation for arrays
let nth n (x:_ []) = x.[n]

, , F # , , , F #, , , : FsControl , .

, , , Seq Array? - .

+5

. ; , . , , , , . , , .

, .

, FP , , . - () , .

, F # generics , #, , , , , , . , , 'a.

: , n- . , , ,

type 'a Collection =
    | Array of 'a array
    | List of 'a list
    | Seq of 'a seq

, , - , , List Array, . . , , . nth , , - . , .

( , " , " ), (.. ) ad-hoc ().

:

[1..10] |> List.sumBy (+)
[1.0..10.0] |> List.sumBy (+)

+ . .

, , , , FP . , , say, List.map , "", , , .

, , , , .

+5

Source: https://habr.com/ru/post/1534469/


All Articles