Created
November 26, 2024 09:59
-
-
Save OnurGumus/9a6edfae2e5ee30aeb2b2ff53a347d58 to your computer and use it in GitHub Desktop.
fable functor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Functor = class end | |
let inline map (f: 'a -> 'b) (x: ^Functor) : ^Result = | |
((^Functor or ^Result or Functor) : (static member Map : ^Functor * ('a -> 'b) -> ^Result) (x, f)) | |
type Functor with | |
static member inline Map (x: list<'a>, f: 'a -> 'b) : list<'b> = | |
List.map f x | |
static member inline Map (x: option<'a>, f: 'a -> 'b) : option<'b> = | |
Option.map f x | |
let inline replace (value: 'a) (x: ^Functor) : ^Result = | |
map (fun _ -> value) x | |
// Usage Examples | |
let resultList = replace 42 [1; 2; 3] | |
// resultList : int list = [42; 42; 42] | |
let resultOption = replace "hello" (Some 5) | |
// resultOption : string option = Some "hello" | |
printf "%A" resultOption | |
printf "%A" resultList |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment