-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlasso9.lasso
111 lines (92 loc) · 3.2 KB
/
lasso9.lasso
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
<?lasso
content_type('text/plain')
// Simple timer
define timethis(what::string) => {
local(gb) = givenblock
return '\n' + #what + '\t' + ((timer(1) => {#gb()})->get(2)->value * 0.001) +'\tmilliseconds\n\n'
}
protect => {
handle_error => {
// Execute capture x many times
define do(count::integer,i::integer=0) => {
local(gb) = givenblock
not #count ? returnhome
{ #gb->invoke(++#i)
#i < #count ? currentcapture->restart()
}()
}
// Fibonacci capture x many times
define fibonacci(n::integer) => {
#n < 1 ? return 0
local(
swap = 0,
n1 = 0,
n2 = 1
)
do(#n) => {
#swap = #n1 + #n2
#n2 = #n1
#n1 = #swap
}
return #n1
}
// Count instances of a pattern
define string->count(p::string) => {
local(i=0,offset = 0)
{
#offset = .find(#p,#offset != 0 ? #offset + #p->size | 1 )
#offset == 0 ? returnhome
#i++
currentcapture->restart()
}()
return #i
}
}
do(1)
''->count('')
}
timethis('Fibonacci Numbers') => {^
local(out) = ''
do(40) => {
#out->append(fibonacci(#1-1));
}
#out
^}
timethis('1,000,000') => {
local(i) = 0
{++#i != 1000000 ? currentcapture->restart}()
}
timethis('JSON') => {^
local(j) = json_deserialize('[{"batters": {"batter": [{"id": "1001", "type": "Regular"}, {"id": "1002", "type": "Chocolate"}, {"id": "1003", "type": "Blueberry"}, {"id": "1004", "type": "Devil\'s Food"}]}, "id": "0001", "name": "Cake", "ppu": 0.550000, "topping": [{"id": "5001", "type": "None"}, {"id": "5002", "type": "Glazed"}, {"id": "5005", "type": "Sugar"}, {"id": "5007", "type": "Powdered Sugar"}, {"id": "5006", "type": "Chocolate with Sprinkles"}, {"id": "5003", "type": "Chocolate"}, {"id": "5004", "type": "Maple"}], "type": "donut"}, {"batters": {"batter": [{"id": "1001", "type": "Regular"}]}, "id": "0002", "name": "Raised", "ppu": 0.550000, "topping": [{"id": "5001", "type": "None"}, {"id": "5002", "type": "Glazed"}, {"id": "5005", "type": "Sugar"}, {"id": "5003", "type": "Chocolate"}, {"id": "5004", "type": "Maple"}], "type": "donut"}, {"batters": {"batter": [{"id": "1001", "type": "Regular"}, {"id": "1002", "type": "Chocolate"}]}, "id": "0003", "name": "Old Fashioned", "ppu": 0.550000, "topping": [{"id": "5001", "type": "None"}, {"id": "5002", "type": "Glazed"}, {"id": "5003", "type": "Chocolate"}, {"id": "5004", "type": "Maple"}], "type": "donut"}]')
json_serialize(#j)
^}
timethis('Count String') => {
'the three truths'->count('th')
'ababababab'->count('abab')
}
// Using mysql.help_topic table
timethis('MySQL Inline') => {
local(out) = ''
inline(
-database = 'mysql',
-sql = 'SELECT name, example, url FROM help_topic LIMIT 0,100'
) => {
rows => {
#out->append(
column('name')+', '+column('example')+' is here ' + column('url')
)
}
}
}
// Download here: http://www.github.com/zeroloop/ds
timethis('MySQL Datasource') => {
local(out) = ''
with row in ds(::mysqlds,'127.0.0.1',::mysql,'root','')->sql(
'SELECT name, example, url FROM help_topic LIMIT 0,100'
)->rows do {
#out->append(
#row(::name) + ', ' + #row(::example)+' is here ' + #row(::url) + '\n'
)
}
}
?>