-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Benchmark Dictionary implementation in Swarm #2191
base: main
Are you sure you want to change the base?
Conversation
@byorgey without tydef Maybe a = Unit + a end
tydef IDict d k v =
[ empty: d
, insert: k -> v -> d -> d
, delete: k -> d -> d
, get: k -> d -> Maybe v
, contains: k -> d -> Bool
, pretty: d -> Text
]
end
def undefined_dict : any -> IDict any k v =\empty.
[empty=empty, insert=\x.undefined, delete=\x.undefined, get=\x.undefined, contains=\x.undefined, pretty=\x.undefined]
end
tydef RBTree k = rec b. Maybe [red: Bool, k: k, l: b, r: b] end
tydef Dict k v = RBTree (k * v) end
def tree_dict : IDict (Dict k v) k v = undefined_dict (inl ()) end
run "recursive-containers.sw";
log $ tree_dict.pretty $ tree_dict.insert 1 "b" $ tree_dict.insert 0 "a" $ tree_dict.insert 4 "e" tree_dict.empty I hoped the records would make this shorter, but they are values, so I can not just declare them EDIT: I wonder if |
end | ||
|
||
tydef FlatSet a = List a end | ||
def flat_set : ISet (FlatSet a) a = undefined_set (inl ()) end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the purpose of defining flat_set
et al as records with lots of undefined
at first, and then later redefining them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's an overview, as it can be hard to spot between the Red-Black Tree implementation and tests.
It also works as a "header" of sorts, if you wanted to copy paste part of it in a scenario definition/other Swarm script and then run this file.
def benchmark: Int -> s -> (s -> s) -> Cmd (Int * (Int * Int)) = \times.\s.\act. | ||
let min = \x.\y. if (x > y) {y} {x} in | ||
let max = \x.\y. if (x > y) {x} {y} in | ||
let runM: (Int * Maybe (Int * Int)) -> s -> Int -> Cmd (Int * Maybe (Int * Int)) = \acc.\s.\n. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Scoped type variables! 😍
You can test it with:
run "example/recursive-containers.sw"; benchmark_tree