Skip to content

Added doc for more string-dict methods #27

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

Open
wants to merge 2 commits into
base: horizon
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
207 changes: 206 additions & 1 deletion src/trove/string-dict.scrbl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
(method-spec (name "has-key"))
(method-spec (name "count"))
(method-spec (name "unfreeze"))
(method-spec (name "merge"))
(method-spec (name "keys-list"))
(method-spec (name "map-keys"))
(method-spec (name "fold-keys"))
(method-spec (name "each-key"))
)))
(data-spec
(name "MutableStringDict")
Expand All @@ -44,7 +49,13 @@
(method-spec (name "has-key-now"))
(method-spec (name "count-now"))
(method-spec (name "freeze"))
(method-spec (name "seal")))))
(method-spec (name "seal"))
(method-spec (name "merge-now"))
(method-spec (name "keys-list-now"))
(method-spec (name "map-keys-now"))
(method-spec (name "fold-keys-now"))
(method-spec (name "each-key-now"))
)))
))

@docmodule["string-dict"]{
Expand Down Expand Up @@ -225,6 +236,102 @@ check:
end
}

@sd-method["merge"
#:contract (a-arrow (SD-of "a") (SD-of "a") (SD-of "a"))
#:args (list (list "self" #f) (list "other" #f))
#:return (SD-of "a")
]

Returns a new immutable string-dict that has the keys of both
the original string-dict and the @pyret{other} string-dict. Choose a key's
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't use the verb "choose" here, since that sounds to me like the programmer has a choice. I'd reword this to "In cases where both input dictionaries have the same key, the resulting value comes from the @pyret{other} string-dict."

value to be the one it has in the @pyret{other} string-dict, if it has one;
otherwise it has the value from the original string-dict.

@examples{
check:
sd1 = [string-dict: "a", 5, "c", 4]
sd2 = [string-dict: "a", 10, "b", 6]
sd3 = sd1.merge(sd2)
sd3 is [string-dict: "a", 10, "b", 6, "c", 4]
sd4 = sd2.merge(sd1)
sd4 is [string-dict: "a", 5, "b", 6, "c", 4]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# NOTE: sd3 is-not sd4

sd2.merge(sd1).merge(sd1) is sd2.merge(sd1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# merging the same dictionary twice has no additional effect

end
}

@sd-method["keys-list"
#:contract (a-arrow (SD-of "a") (L-of S))
#:args (list (list "self" #f))
#:return (L-of S)
]

Returns the list of keys in the immutable string-dict, in some order.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"in some unspecified order"


@examples{
check:
sd2 = [string-dict: "a", 10, "b", 6]
sd2.keys-list() is [list: "a", "b"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the order is unspecified, then this test might fail. Either add a .sort() or make it into a set or something...

end
}

@sd-method["map-keys"
#:contract (a-arrow (SD-of "a") (a-arrow "a" "b") (L-of "b"))
#:args (list (list "self" #f) (list "f" #f))
#:return (L-of "b")
]

Applies the function @pyret{f} to each key in the immutable string-dict, and
returns a list of the returned values in some order.

@examples{
fun one-of(ans, elts):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably easier to say elts.member(ans), no?

is-some(for find(elt from elts):
ans == elt
end)
end

check:
sd2 = [string-dict: "a", 10, "b", 6]
sd2.map-keys(lam(x): string-append(x, x) end) is%(one-of)
[list: [list: "aa", "bb"], [list: "bb", "aa"]]
end
}

@sd-method["fold-keys"
#:contract (a-arrow (SD-of "a") (a-arrow S "b") "b")
#:args (list (list "self" #f) (list "f" #f) (list "init" #f))
#:return "b"
]

Returns the result of folding the function @pyret{f} across the keys in the
immutable string-dict, in some order, starting with @pyret{init} as the base value.

@examples{
check:
sd2 = [string-dict: "a", 10, "b", 6]
sd2.fold-keys(lam(x, acc): acc + string-length(x) end, 0) is 2
end
}

@sd-method["each-key"
#:contract (a-arrow (SD-of "a") (a-arrow S No) No)
#:args (list (list "self" #f) (list "f" #f))
#:return No
]

Calls the function @pyret{f} on each key in the immutable string-dict,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The point of this is the side effects of f, right? Probably worth mentioning.

returning nothing.

@examples{
var numkeys = 0

check:
sd2 = [string-dict: "a", 10, "b", 6]
sd2.each-key(lam(x): numkeys := numkeys + 1 end) is nothing
numkeys is 2
end
}

@section{The MutableStringDict Type}

@type-spec["MutableStringDict" (list "a")]
Expand Down Expand Up @@ -423,4 +530,102 @@ check:
end
}

@msd-method["merge-now"
#:contract (a-arrow (MSD-of "a") (MSD-of "a") No)
#:args (list (list "self" #f) (list "other" #f))
#:return No
]

Modifies the mutable string-dict to include the keys from the @pyret{other}
mutable-string-dict. Choose a key's value to be the one it has in the @pyret{other}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

string-dict, if it has one; otherwise it keeps the value it originally had.
Returns nothing.

@examples{
check:
msd1 = [mutable-string-dict: "a", 5, "c", 4]
msd2 = [mutable-string-dict: "a", 10, "b", 6]
msd1.get-value-now("a") is 5
msd1.get-value-now("b") raises "Key b not found"
msd1.get-value-now("c") is 4
msd1.merge-now(msd2) is nothing
msd1.get-value-now("a") is 10
msd1.get-value-now("b") is 6
msd1.get-value-now("c") is 4
end
}

@msd-method["keys-list-now"
#:contract (a-arrow (MSD-of "a") (L-of S))
#:args (list (list "self" #f))
#:return (L-of S)
]

Returns the list of keys in the mutable string-dict, in some order.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto


@examples{
check:
msd2 = [mutable-string-dict: "a", 10, "b", 6]
msd2.keys-list-now() is [list: "a", "b"]
end
}

@msd-method["map-keys-now"
#:contract (a-arrow (MSD-of "a") (a-arrow "a" "b") (L-of "b"))
#:args (list (list "self" #f) (list "f" #f))
#:return (L-of "b")
]

Modify the mutable string-dict so that each key is now
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No -- this returns a List<b>, and the description sounds like it returns a MSD.

associated with the result of applying
the function @pyret{f} to its original value.

@examples{
fun one-of(ans, elts):
is-some(for find(elt from elts):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

ans == elt
end)
end

check:
msd2 = [mutable-string-dict: "a", 10, "b", 6]
msd2.map-keys-now(lam(x): string-append(x, x) end) is%(one-of)
[list: [list: "aa", "bb"], [list: "bb", "aa"]]
end
}

@msd-method["fold-keys-now"
#:contract (a-arrow (MSD-of "a") (a-arrow S "b") "b")
#:args (list (list "self" #f) (list "f" #f) (list "init" #f))
#:return "b"
]

Returns the result of folding the function @pyret{f} across the keys in the
mutable string-dict, in some order, starting with @pyret{init} as the base value.

@examples{
check:
msd2 = [mutable-string-dict: "a", 10, "b", 6]
msd2.fold-keys-now(lam(x, acc): acc + string-length(x) end, 0) is 2
end
}

@msd-method["each-key-now"
#:contract (a-arrow (MSD-of "a") (a-arrow S No) No)
#:args (list (list "self" #f) (list "f" #f))
#:return No
]

Calls the function @pyret{f} on each key in the mutable string-dict,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks -- made your suggested changes. When having the description of each-key refer to map-keys, I tried using @pyret-id{map-keys} rather than @pyret{map-keys} to make it a hyperlink, but it doesn't seem to work. Is there a Pyret scribble quickref that will show how to ensure that @pyret-id{...} does link.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use @pyret-method["StringDict"]{each-key} to link to the each-key method of the StringDict data type.

returning nothing.

@examples{
var numkeys = 0

check:
msd2 = [mutable-string-dict: "a", 10, "b", 6]
msd2.each-key-now(lam(x): numkeys := numkeys + 1 end) is nothing
numkeys is 2
end
}
}