Skip to content
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

[builtins/dict] add => values() method #1777

Merged
merged 4 commits into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 16 additions & 0 deletions builtin/method_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,19 @@ def Call(self, rd):

keys = [value.Str(k) for k in dictionary.keys()] # type: List[value_t]
return value.List(keys)


class Values(vm._Callable):

def __init__(self):
# type: () -> None
pass

def Call(self, rd):
# type: (typed_args.Reader) -> value_t

dictionary = rd.PosDict()
rd.Done()

values = dictionary.values() # type: List[value_t]
return value.List(values)
2 changes: 1 addition & 1 deletion core/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ def Main(
'get': None, # doesn't raise an error
'erase': None, # ensures it doesn't exist
'keys': method_dict.Keys(),
'values': None, # TODO
'values': method_dict.Values(),

# I think items() isn't as necessary because dicts are ordered?
# YSH code shouldn't use the List of Lists representation.
Expand Down
51 changes: 50 additions & 1 deletion doc/ref/chap-type-method.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,27 +138,76 @@ rather than its value.

### append()

Add an element to a list.

var fruits = :|apple banana pear|
call fruits->append("orange")
echo @fruits # => apple banana pear orange

### pop()

remove an element from a list and return it.

var fruits = :|apple banana pear orange|
var last = fruits->pop() # "orange" is removed AND returned
echo $last # => orange
echo @fruits # => apple banana pear

### extend()

Extend an existing list with the elements of another list.

var foods = :|cheese chocolate|
var fruits = :|apple banana|
call foods->extend(fruits)
echo @foods # => cheese chocolate apple banana pear

### indexOf()

Returns the first index of the element in the List, or -1 if it's not present.
Returns the first index of the element in the list, or -1 if it's not present.

var names = :| Jane Peter Joana Sam |
echo $[names => indexOf("Sam")] # => 3
echo $[names => indexOf("Simon")] # => -1

### insert()

### remove()

### reverse()

Reverses a list in place.

var fruits = :|apple banana pear|
call fruits->reverse()
echo @fruits # => pear banana apple

## Dict

### keys()

Returns all existing keys from a dict as a list of strings.

var en2fr = {
hello: "bonjour",
friend: "ami",
cat: "chat"
}
= en2fr => keys()
# => (List 0x4689) ["hello","friend","cat"]

### values()

Similar to `keys()`, but returns the values of the dictionary.

var person = {
name: "Foo",
age: 25,
hobbies: :|walking reading|
}
= en2fr => values()]
# => (List 0x4689) ["Foo",25,["walking","reading"]]

### get()

### erase()
Expand Down
11 changes: 11 additions & 0 deletions spec/ysh-methods.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ pp line (en2fr => keys())
(List) ["hello","friend","cat"]
## END

#### Dict => values()
var en2fr = {}
setvar en2fr["hello"] = "bonjour"
setvar en2fr["friend"] = "ami"
setvar en2fr["cat"] = "chat"
pp line (en2fr => values())
## status: 0
## STDOUT:
(List) ["bonjour","ami","chat"]
## END

#### Separation of -> attr and () calling
const check = "abc" => startsWith
pp line (check("a"))
Expand Down
Loading