Fork me on GitHub

Code Snippet

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
module Standard =
  let (@@) : ('a -> 'b option) -> ('a -> 'b option) -> 'a -> 'b option =
    fun f g x ->
      match f x with
        | None -> g x
        | some -> some
  
  let standard : string -> string option = function
    | ".txt" -> Some "text/plain"
    | ______ -> None
  
  type config =
    { mimeTypes : string -> string option }
    
  let patternMatch : string -> config -> string option =
    fun x { config.mimeTypes = f } ->
      f x
  
  let config = { mimeTypes = standard }

module Custom =
  open Standard
  
  let extended : string -> string option = function
    | ".avi" -> Some "video/avi"
    | ______ -> None
  
  let custom = standard @@ extended
  
  let config = { config with mimeTypes = custom }

Standard . config |> Standard . patternMatch ".avi"
Custom   . config |> Standard . patternMatch ".avi"

Code output:

> 
module Standard = begin
  val ( @@ ) : f:('a -> 'b option) -> g:('a -> 'b option) -> x:'a -> 'b option
  val standard : _arg1:string -> string option
  type config =
    {mimeTypes: string -> string option;}
  val patternMatch : x:string -> config -> string option
  val config : config = {mimeTypes = <fun:config@19>;}
end

> 
module Custom = begin
  val extended : _arg1:string -> string option
  val custom : (string -> string option)
  val config : Standard.config = {mimeTypes = <fun:custom@28-2>;}
end

> val it : string option = None
> val it : string option = Some "video/avi"

References:

comments powered by Disqus