Skip to content

Instantly share code, notes, and snippets.

@OnurGumus
Created November 26, 2024 09:59
Show Gist options
  • Save OnurGumus/9a6edfae2e5ee30aeb2b2ff53a347d58 to your computer and use it in GitHub Desktop.
Save OnurGumus/9a6edfae2e5ee30aeb2b2ff53a347d58 to your computer and use it in GitHub Desktop.
fable functor
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