package minicaml

  1. Overview
  2. Docs

Source file dictp.ml

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
open Types
open Typecheck
open Util

(** Insert a key-value pair in a dictionary *)
let insert_dict args =
  let (k, v, d) = (match args with
      | [k; v; d] -> (k, v, unpack_dict d)
      | _ -> raise WrongPrimitiveArgs) in
  EvtDict (isvalidkey (k, v) :: (Dict.delete k d))


(** Remove a key-value pair from a dictionary *)
let delete_dict args =
  let (key, ed) = (match args with
      | [key; d] -> (key, unpack_dict d)
      | _ -> raise WrongPrimitiveArgs) in
  if not (Dict.exists key ed) then raise (DictError "key not found") else
    EvtDict (Dict.delete key ed)

(** Check if a key-value pair is in a dictionary *)
let haskey args =
  let (key, ed) = (match args with
      | [key; d] -> (key, unpack_dict d)
      | _ -> raise WrongPrimitiveArgs) in
  EvtBool(Dict.exists key ed)

(** Check if a dict contains a key *)
let getkey args =
  let (key, ed) = (match args with
      | [key; d] -> (key, unpack_dict d)
      | _ -> raise WrongPrimitiveArgs) in
  if not (Dict.exists key ed) then raise (DictError "key not found") else
    Dict.get key ed


(** Check if a dict contains a key *)
let filterkeys args =
  let (kll, ed) = (match args with
      | [kl; d] -> (unpack_list kl, unpack_dict d)
      | _ -> raise WrongPrimitiveArgs) in
  EvtDict(Dict.filter kll ed)

let table = [
  ("insert", (insert_dict, 3));
  ("remove", (delete_dict, 2));
  ("haskey", (haskey, 2));
  ("getkey", (getkey, 2));
  ("filterkeys", (filterkeys, 2))
]

let js = {|
const insert = R.assoc;
const remove = R.dissoc;
const haskey = R.has;
const getkey = R.prop;
const filterkeys = R.pick;
|}