Lil*_*a 5 7 ocaml ocamlbuild opam
我正在尝试制作这段代码:
open Lwt;;
open Cohttp;;
(* a simple function to access the content of the response *)
let content = function
| Some (_, body) -> Cohttp_lwt_unix.Body.string_of_body body
(* launch both requests in parallel *)
let t = Lwt_list.map_p Cohttp_lwt_unix.Client.get
(List.map Uri.of_string
[ "http://example.org/";
"http://example2.org/" ])
(* maps the result through the content function *)
let t2 = t >>= Lwt_list.map_p content
(* launch the event loop *)
let v = Lwt_main.run t2
Run Code Online (Sandbox Code Playgroud)
但是,当我跑
Ocamlbuild file.native
Run Code Online (Sandbox Code Playgroud)
我得到了未绑定的模块错误.
这些模块是通过opam安装的,当我运行时
ocamlfind query lwt
/home/chris/.opam/system/lib/lwt
ocamlfind query cohttp
/home/chris/.opam/system/lib/cohttp
Run Code Online (Sandbox Code Playgroud)
如何让Ocamlbuild找到这两个包?
我试过了
Ocamlbuild -pkgs cohttp,lwt file.native
Run Code Online (Sandbox Code Playgroud)
它不起作用.它说了一些可能的扩展.我不认为这是问题所在.
如果有人能给我正确的代码来做到这一点,将不胜感激.谢谢!
Cohttp已更新,所以我已更正您的代码以使用最新版本:
open Lwt;;
open Cohttp;;
(* a simple function to access the content of the response *)
let content = function
| Some (_, body) -> Cohttp_lwt_body.string_of_body body
| None -> assert false
(* launch both requests in parallel *)
let t = Lwt_list.map_p Cohttp_lwt_unix.Client.get
(List.map Uri.of_string
[ "http://google.com";
"http://yahoo.com" ])
(* maps the result through the content function *)
let t2 = t >>= Lwt_list.map_p content
(* launch the event loop *)
let v = Lwt_main.run t2
Run Code Online (Sandbox Code Playgroud)
你可以用
ocamlbuild -use-ocamlfind -pkgs cohttp.lwt file.native
Run Code Online (Sandbox Code Playgroud)
几条评论:
1)您应该使用-use-ocamlfindwith ocamlbuild来使用opam(或任何其他已安装的ocaml库)
2)要使用cowt与lwt,你应该使用cohttp.lwt包.添加lwt也不是绝对必要的.