帮助深入理解ocaml中functor 的实例
来源:互联网 发布:软件授权码后台 编辑:程序博客网 时间:2024/05/02 05:43
假设我们已经有啤酒与威士忌两个模块:
module Beer = struct
type t = BEER
let pour () =
let () = Printf.printf "... a nice head ... " in BEER
let consume t =Printf.printf "Ha! Nothing like a good beer to quench the thirst\n"
end
module Whisky = struct
type t = WHISKY
let pour () =
let () = Printf.printf "... 2 fingers of this ...." in WHISKY
let consume _ = Printf.printf "... Ha! Piss of the Gods!\n"
end
如果现在想实现一个酒鬼模块, 这个酒鬼有时喜欢啤酒,有时又喜欢威士忌. 注意: 啤酒与威士忌模块实现了一样的函数(pour/consume). 所以我们可以定义一个如下的"模块类型":
module type Beverage = sig
type t
val pour :unit -> t
val consume : t ->unit
end
然后,使用 functor建一个酒鬼模块:
module Alkie =functor (B:Beverage)-> struct
let rec another = function
| 0 -> ()
| i -> let b = B.pour () in
let () = B.consume b in
another (i-1)
end
使用:
let menu = Hashtbl.create 3
let () = Hashtbl.add menu "Beer" (module Beer : Beverage)
let () = Hashtbl.add menu "Whisky" (module Whisky : Beverage)
let () = let favourite =ref "Whisky"in
let () = Arg.parse
[("--beverage", Arg.Set_string favourite,Printf.sprintf "which beverage to use (%s)" !favourite)]
(fun s -> raise (Arg.Bad s))
("usage:")
in
let module B = (val (Hashtbl.find menu !favourite) : Beverage)in
let module AlkieB = Alkie(B) in
let () = AlkieB.another 3 in ();;
将上面代码存入文件a.ml,然后:
>ocamlc a.ml
>./a.out
... 2 fingers of this ....... Ha! Piss of the Gods!
... 2 fingers of this ....... Ha! Piss of the Gods!
... 2 fingers of this ....... Ha! Piss of the Gods!
>./a.out --beverage Beer
... a nice head ... Ha! Nothing like a good beer to quench the thirst
... a nice head ... Ha! Nothing like a good beer to quench the thirst
... a nice head ... Ha! Nothing like a good beer to quench the thirst
当然你也可以用类来实现, 只需将啤酒与威士忌对象注入酒鬼就可以了. 附代码:
classtype beverage = object
method consume : unit -> unit
end
class beer = object (self :#beverage)
method consume () = Printf.printf "...drank beer\n"
end
let make_beer () =
let () = Printf.printf "pouring beer\n" in
let b = new beer in
(b :> beverage) (*b是beverage的子对象,函数返回的类型是beverage *)
class whisky = object(self :#beverage)
method consume () = Printf.printf "...drank whisky\n"
end
let make_whisky () =
let () = Printf.printf "pouring whisky\n" in
let b = new whisky in
(b :> beverage)
class alkie p = object (self)
method another n =
if n = 0
then ()
else
let b = p () in
let () = b#consume ()in
self#another (n-1)
end
let menu = Hashtbl.create 3
let () = Hashtbl.add menu "Beer" make_beer
let () = Hashtbl.add menu "Whisky" make_whisky
let () =
let favourite = ref "Whisky" in
let () = Arg.parse
[("--beverage", Arg.Set_string favourite,Printf.sprintf "which beverage to use (%s)" !favourite)]
(fun s -> raise (Arg.Bad s))
("usage:")
in
let p = Hashtbl.find menu !favouritein
let a = new alkie pin (* 注入对象 *)
a#another 3 ;;
引用网址:http://blog.csdn.net/sw2wolf/article/details/7006930
- 帮助深入理解ocaml中functor 的实例
- 使用OCaml的Functor与First class module
- OCaml 中 module 和 Signature 的关系
- 深入理解仿函数(functor或function object)
- C++ 中 Bool functor 的优化
- 自己为了理解Functor写的一个例子
- PHP递归算法的一个实例 帮助理解
- 关于二分法的深入理解及实例
- ocaml
- OCaml
- React 实例-深入理解
- STL,Loki和Boost中Functor的比较(-)
- STL,Loki和Boost中Functor的比较(二)
- Ocaml 的入门整理 1
- 通过实例帮助理解闭包
- 帮助理解c#中委托+事件的一个例子
- STL中伪函数、函数对象(functor)初步理解(上)
- STL中伪函数、函数对象(functor)初步理解(下)
- NYOJ 202 红黑树 数组模拟中序遍历
- OpenCV2.4.0正式发布了
- NDK工程的创建、编译、打包
- 云计算服务层次 PAAS IAAS SAAS
- 揭秘刷机产业链:每部利润上百元 厂商涌入
- 帮助深入理解ocaml中functor 的实例
- 雷军谈互联网七字诀:天下武功唯快不破
- 993 - Product of digits
- 谁是中国移动互联网的王者?360发布定制手机消息随感
- poj1323(Game Prediction)
- AndroidManifest.xml文件详解(activity)(二)
- C# CHECKEDLISTBOX控件用法总结
- 高内聚、低耦合
- AndroidManifest.xml文件详解(activity)(三)