1 # spec/ysh-methods
2
3 ## our_shell: ysh
4
5 #### => operator for pure computation is allowed (may be mandatory later)
6
7 # later we may make it mandatory
8
9 if ("abc" => startsWith("a")) {
10 echo yes
11 }
12
13 var mylist = [1, 2, 3]
14
15 # This one should be ->
16 call mylist->pop()
17 echo 'ok'
18
19 ## STDOUT:
20 yes
21 ok
22 ## END
23
24 #### => can be used to chain free functions
25
26 func dictfunc() {
27 return ({k1: 'spam', k2: 'eggs'})
28 }
29
30 echo $[list(dictfunc()) => join('/') => upper()]
31
32 # This is nicer and more consistent
33 echo $[dictfunc() => list() => join('/') => upper()]
34
35 ## STDOUT:
36 K1/K2
37 K1/K2
38 ## END
39
40 #### Str => startsWith()
41 pp line ("abc" => startsWith(""))
42 pp line ("abc" => startsWith("a"))
43 pp line ("abc" => startsWith("z"))
44 ## status: 0
45 ## STDOUT:
46 (Bool) true
47 (Bool) true
48 (Bool) false
49 ## END
50
51 #### Str => startsWith(), no args
52 = "abc" => startsWith()
53 ## status: 3
54
55 #### Str => startsWith(), too many args
56 = "abc" => startsWith("extra", "arg")
57 ## status: 3
58
59 #### Missing method (Str->doesNotExist())
60 = "abc"->doesNotExist()
61 ## status: 3
62
63 #### Dict => keys()
64 var en2fr = {}
65 setvar en2fr["hello"] = "bonjour"
66 setvar en2fr["friend"] = "ami"
67 setvar en2fr["cat"] = "chat"
68 pp line (en2fr => keys())
69 ## status: 0
70 ## STDOUT:
71 (List) ["hello","friend","cat"]
72 ## END
73
74 #### Dict => values()
75 var en2fr = {}
76 setvar en2fr["hello"] = "bonjour"
77 setvar en2fr["friend"] = "ami"
78 setvar en2fr["cat"] = "chat"
79 pp line (en2fr => values())
80 ## status: 0
81 ## STDOUT:
82 (List) ["bonjour","ami","chat"]
83 ## END
84
85 #### Separation of -> attr and () calling
86 const check = "abc" => startsWith
87 pp line (check("a"))
88 ## status: 0
89 ## STDOUT:
90 (Bool) true
91 ## END
92
93 #### Bound methods, receiver value/reference semantics
94 var is_a_ref = { "foo": "bar" }
95 const f = is_a_ref => keys
96 pp line (f())
97 setvar is_a_ref["baz"] = 42
98 pp line (f())
99
100 var is_a_val = "abc"
101 const g = is_a_val => startsWith
102 pp line (g("a"))
103 setvar is_a_val = "xyz"
104 pp line (g("a"))
105 ## status: 0
106 ## STDOUT:
107 (List) ["foo"]
108 (List) ["foo","baz"]
109 (Bool) true
110 (Bool) true
111 ## END
112
113 #### List => indexOf()
114 var items = [1, '2', 3, { 'a': 5 }]
115
116 json write (items => indexOf('a'))
117 json write (items => indexOf(1))
118 json write (items => indexOf('2'))
119 json write (items => indexOf({'a': 5}))
120 ## STDOUT:
121 -1
122 0
123 1
124 3
125 ## END
126
127 #### List => join()
128 var items = [1, 2, 3]
129
130 json write (items => join()) # default separator is ''
131 json write (items => join(" ")) # explicit separator (can be any number or chars)
132 json write (items => join(", ")) # separator can be any number of chars
133
134 try {
135 json write (items => join(1)) # separator must be a string
136 }
137 echo "failed with status $_status"
138 ## STDOUT:
139 "123"
140 "1 2 3"
141 "1, 2, 3"
142 failed with status 3
143 ## END
144
145 #### List->reverse()
146
147 var empty = []
148
149 var a = [0]
150 var b = [2, 1, 3]
151 var c = :| hello world |
152
153 call empty->reverse()
154 call a->reverse()
155 call b->reverse()
156 call c->reverse()
157
158 json write --pretty=F (empty)
159 json write --pretty=F (a)
160 json write --pretty=F (b)
161 json write --pretty=F (c)
162
163 ## STDOUT:
164 []
165 [0]
166 [3,1,2]
167 ["world","hello"]
168 ## END
169
170 #### List->reverse() from iterator
171 var x = list(0 .. 3)
172 call x->reverse()
173 write @x
174 ## STDOUT:
175 2
176 1
177 0
178 ## END
179