arkdro avatar arkdro committed 07d4254

add subtract api

Comments (0)

Files changed (2)

erlang/src/c_server.erl

 -behaviour(gen_server).
 
 -export([
+         subtract/2,
          sum/1,
          info/1,
          get/0,
 sum(List) ->
     gen_server:call(?SERVER, {sum, List}).
 
+subtract(N1, N2) ->
+    gen_server:call(?SERVER, {subtract, N1, N2}).
+
 %-------------------------------------------------------------------
 start_link() ->
     gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
     Cmd = term_to_binary({sum_list, Term}),
     port_command(Port, Cmd),
     {noreply, State#state{from=From}};
+handle_call({subtract, N1, N2}, From,
+            #state{status=running, port=Port} = State) ->
+    Cmd = term_to_binary({subtract, N1, N2}),
+    port_command(Port, Cmd),
+    {noreply, State#state{from=From}};
 handle_call(stop, _From, St) ->
     {stop, normal, ok, St};
 handle_call(status, _From, St) ->

ocaml/src/odec_port.ml

   let res = List.fold_left f (Num.Int 0) list3 in
   create_decimal res
 
+let subtract d1 d2 =
+  let t1 = make_tuple d1 in
+  let t2 = make_tuple d2 in
+  match t1, t2 with
+  | Some _, Some _ ->
+    let (Some n1) = make_num t1 in
+    let (Some n2) = make_num t2 in
+    let res = Num.sub_num n1 n2 in
+    let dec_res = create_decimal res in
+    ET_Tuple [ET_Atom "ok"; dec_res]
+  | _ ->
+    ET_Tuple [ET_Atom "error"; ET_Atom "not_decimal"]
+
 let port_command_dispatcher old_value = function
   | ET_Tuple [ET_Atom "forget"; _] ->
     None, ET_Atom "ok"
   | ET_Tuple [ET_Atom "sum_list"; (ET_List list) as t] ->
     let res = sum_list list in
     Some t, ET_Tuple [ET_Atom "ok"; res]
+  | (ET_Tuple [ET_Atom "subtract"; t1; t2]) as t ->
+    let res = subtract t1 t2 in
+    Some t, res
   | _ -> raise (Failure "Unknown command")
   ;;
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.