-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtuple_demo.tea
275 lines (233 loc) · 7.48 KB
/
tuple_demo.tea
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/opt/TeaAgeSolutions/TeaScript/bin/TeaScript
/*
* SPDX-FileCopyrightText: Copyright (C) 2023 Florian Thake, <contact |at| tea-age.solutions>.
* SPDX-License-Identifier: MIT
* see included file MIT_LICENSE.txt
*/
// This is a demo script for showing the use of named tuple
// You can show, search, delete and add persons to/from a given data set.
// NOTE: For this demo the first and last name of one data entry must be unique.
// factory function for birthday
func create_birth( year, month, day )
{
def birth := _tuple_create()
def birth.year := year
def birth.month := month
def birth.day := day
birth
}
// factory function for one person
func create_person( firstname, lastname, birth, email )
{
def person := _tuple_create()
def person.first_name := firstname
def person.last_name := lastname
def person.birth := birth
def person.email := email
person
}
// our main data set we are working with.
def data_set := _tuple_create()
// add some data to start with
_tuple_named_append( data_set, "Doe, Jon", create_person( "Jon", "Doe", create_birth( 1976, 7, 26 ), "[email protected]" ) )
_tuple_named_append( data_set, "Meyer, Peter", create_person( "Peter", "Meyer", create_birth( 1964, 10, 5 ), "[email protected]" ) )
_tuple_named_append( data_set, "Moon, Liz", create_person( "Liz", "Moon", create_birth( 1987, 3, 17 ), "[email protected]" ) )
// shows the entry by given index or name
func show( index_or_name )
{
if( not tuple_contains( data_set, index_or_name ) ) {
println( "%(index_or_name) is not in data set." )
} else {
const index := if( index_or_name is String ) { _tuple_index_of( data_set, index_or_name ) } else { index_or_name }
println( "%(index): %(_tuple_val(data_set, index))" )
}
}
// adds a new entry, just do some very basic checks on the input.
func add_entry()
{
println( "\nAdd new entry (type nothing and press enter will cancel!)" )
print( "\nFirstname: ")
const firstname := readline()
if( not firstname ) {
println( "canceled!" )
return void
}
print( "\nLastname: ")
const lastname := readline()
if( not lastname ) {
println( "canceled!" )
return void
}
const key := lastname % ", " % firstname
if( tuple_contains( data_set, key ) ) {
println( "%(key) exists already!" )
return void
}
print( "\nBirth year: ")
const year_str := readline()
if( not year_str ) {
println( "canceled!" )
return void
}
const year := _strtonum( year_str )
if( not year is i64 ) {
println( "must be number!" )
return void
}
print( "\nBirth month: ")
const month_str := readline()
if( not month_str ) {
println( "canceled!" )
return void
}
const month := _strtonum( month_str )
if( not month is i64 ) {
println( "must be number!" )
return void
}
print( "\nBirth day: ")
const day_str := readline()
if( not day_str ) {
println( "canceled!" )
return void
}
const day := _strtonum( day_str )
if( not day is i64 ) {
println( "must be number!" )
return void
}
print( "\nEmail: ")
const email := readline()
if( not email ) {
println( "canceled!" )
return void
}
if( not strfind( email, "@" ) ) {
println( "must be email!" )
return void
}
// and finally add the entry
_tuple_named_append( data_set, key, create_person( firstname, lastname, create_birth( year, month, day ), email ) )
}
func delete_entry()
{
println( "\nDelete an entry (type nothing and press enter will cancel!)" )
print( "Type index or key: " )
def s := readline()
def index := _strtonum( s )
if( index is i64 ) {
if( not _tuple_remove( data_set, index ) ) {
println( "%(index) is not in data set." )
}
} else {
if( not _tuple_named_remove( data_set, s ) ) {
println( "%(s) is not in data set." )
}
}
}
func search_entry()
{
println( "\nSearch an entry (type nothing and press enter will cancel!)" )
println( "Choose: 1 = Firstname, 2 = Lastname, 3 = birth year, 4 = birth month, 5 = birth day" )
def choice := readline()
if( not choice ) {
println( "canceled!" )
return void
}
def n := _strtonum( choice )
if( not n is i64 or n > 5 or n < 1 ) {
println( "must be number between 1 and 5.")
return void
}
println( "Enter the data to search for: " )
def s := readline()
if( choice > 2 ) {
def test := _strtonum(s)
if( not test is i64 ) {
println( "must be number!" )
return void
}
}
// well, there are 1000 of other options to perform the search ...
// ... but for this demo we picked to build a predicate on the fly ...
// ... (could also use a map (tuple) with predefined predicates, ...
// ... or use the key name and _tuple_named_val() ... or ... or .... )
def predicate := func( entry @= ) { false }
if( choice == 1 ) {
predicate := func( entry @= ) { entry.first_name == s }
} else if( choice == 2 ) {
predicate := func( entry @= ) { entry.last_name == s }
} else if( choice == 3 ) {
predicate := func( entry @= ) { entry.birth.year == s }
} else if( choice == 4 ) {
predicate := func( entry @= ) { entry.birth.month == s }
} else if( choice == 5 ) {
predicate := func( entry @= ) { entry.birth.day == s }
}
println("Searching...")
def idx := 0
const size := _tuple_size( data_set )
repeat {
if( idx == size ) { return true }
if( predicate( _tuple_val( data_set, idx ) ) ) {
show( idx )
}
idx := idx + 1
}
}
func main()
{
println( "%(_tuple_size(data_set)) entries in the data set.\n" )
println( "Enter number for list the entry at index or \"all\" for all (or \"dump\" for dump structure)." )
println( "Enter \"search\" for start search" )
println( "Enter \"delete\" for start delete" )
println( "Enter \"add\" for start add" )
println( "Enter \"q\", \"quit\", \"exit\" or \"end\" for end" )
def command := readline()
if( command == "q" or command == "quit" or command == "exit" or command == "end" ) {
return false
}
if( command == "search" ) {
search_entry()
return true
}
if( command == "delete" ) {
delete_entry()
return true
}
if( command == "add" ) {
add_entry()
return true
}
if( command == "all" ) {
println("\nShowing all:")
def idx := 0
const size := _tuple_size( data_set )
repeat {
if( idx == size ) { return true }
show( idx )
idx := idx + 1
}
}
if( command == "dump" ) {
println("\nDump:")
tuple_print( data_set, "data", 10 )
return true
}
// is it a number?
def index := _strtonum( command )
if( index is i64 ) {
println( "\nShowing:" )
show( index )
return true
}
println( "\nUNKNOWN COMMAND! Please, try again.\n" )
true
}
println( "This is a TeaScript demo script for showing the use of named tuples." )
println( "You can show, search, delete and add persons to/from a given data set." )
repeat {
if( not main() ) { stop }
println("\n")
}
println( "END!" )