rip-lang 3.7.3 → 3.8.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Binary file
@@ -0,0 +1,180 @@
1
+ # Shared playground examples
2
+ # Loaded by all playground variants via <script type="text/rip" src="examples.rip">
3
+
4
+ globalThis.ripExamples =
5
+ demo: '''
6
+ # Rip code - edit me!
7
+ def fibonacci(n)
8
+ if n <= 1 then n
9
+ else fibonacci(n - 1) + fibonacci(n - 2)
10
+
11
+ # Regex features
12
+ email = "user@example.com"
13
+ domain = email[/@(.+)$/, 1]
14
+
15
+ # Reactive state
16
+ count := 0
17
+ doubled ~= count * 2
18
+ ~> console.log "Count: #{count}, Doubled: #{doubled}"
19
+
20
+ # Classes
21
+ class Animal
22
+ constructor: (@name, @sound) ->
23
+ speak: -> "#{@name} says #{@sound}!"
24
+
25
+ dog = Animal.new "Rex", "woof"
26
+ console.log "Fib(10):", fibonacci(10)
27
+ console.log "Domain:", domain
28
+ console.log dog.speak()
29
+ '''
30
+
31
+ basics: '''
32
+ # Variables and expressions
33
+ name = "World"
34
+ greeting = "Hello, #{name}!"
35
+ console.log greeting
36
+
37
+ # Functions
38
+ def fibonacci(n)
39
+ if n <= 1 then n
40
+ else fibonacci(n - 1) + fibonacci(n - 2)
41
+
42
+ # Control flow
43
+ for i in [1..10]
44
+ if i %% 2 is 0
45
+ console.log "#{i} is even"
46
+
47
+ # Array comprehensions
48
+ squares = (x * x for x in [1..5])
49
+ console.log "Squares:", squares
50
+
51
+ # Destructuring
52
+ [first, ...rest] = [1, 2, 3, 4, 5]
53
+ console.log "First:", first, "Rest:", rest
54
+
55
+ console.log "Fib(10):", fibonacci(10)
56
+ '''
57
+
58
+ reactive: '''
59
+ # Reactive state (:=) creates a signal
60
+ count := 0
61
+ name := "Rip"
62
+
63
+ # Computed values (~=) auto-update when dependencies change
64
+ doubled ~= count * 2
65
+ greeting ~= "Hello, #{name}! Count is #{count}"
66
+
67
+ # Effects (~>) run when dependencies change
68
+ ~> console.log "Effect:", greeting
69
+
70
+ # Modify state — effects and computed values update automatically
71
+ count = 5
72
+ name = "World"
73
+ console.log "Doubled:", doubled
74
+
75
+ # Readonly constants
76
+ MAX =! 100
77
+ console.log "Max:", MAX
78
+ '''
79
+
80
+ classes: '''
81
+ class Animal
82
+ constructor: (@name, @sound) ->
83
+ speak: -> "#{@name} says #{@sound}!"
84
+
85
+ class Dog extends Animal
86
+ constructor: (@name) -> super @name, "woof"
87
+ fetch: (item) -> "#{@name} fetches the #{item}!"
88
+
89
+ # Ruby-style .new() constructor
90
+ rex = Dog.new "Rex"
91
+ console.log rex.speak()
92
+ console.log rex.fetch("ball")
93
+
94
+ # Class with methods
95
+ class Counter
96
+ constructor: (@count = 0) ->
97
+ increment: -> @count++
98
+ decrement: -> @count--
99
+ toString: -> "Counter(#{@count})"
100
+
101
+ c = Counter.new 10
102
+ c.increment()
103
+ c.increment()
104
+ console.log "#{c}"
105
+
106
+ # Chained comparisons
107
+ x = 5
108
+ console.log "1 < x < 10:", 1 < x < 10
109
+ '''
110
+
111
+ regex: '''
112
+ # String interpolation
113
+ name = "Rip"
114
+ console.log "Hello, #{name}!"
115
+
116
+ # Regex match with capture groups
117
+ email = "user@example.com"
118
+ domain = email[/@(.+)$/, 1]
119
+ console.log "Domain:", domain
120
+
121
+ # Ruby-style =~ match operator
122
+ if "hello world" =~ /world/
123
+ console.log "Match found!"
124
+
125
+ # Heregex (multiline regex with comments)
126
+ urlPattern = ///
127
+ ^(https?://) # protocol
128
+ ([^/]+) # hostname
129
+ (/.*)?$ # path
130
+ ///
131
+
132
+ url = "https://rip-lang.org/docs"
133
+ [_, protocol, host, path] = url.match(urlPattern)
134
+ console.log "Host:", host
135
+
136
+ # Heredocs (triple-quoted strings)
137
+ html = """
138
+ <div class="greeting">
139
+ <h1>Hello!</h1>
140
+ </div>
141
+ """
142
+ console.log html
143
+
144
+ # String repetition
145
+ console.log "-" * 40
146
+ '''
147
+
148
+ async: '''
149
+ # The ! (dammit) operator calls AND awaits
150
+ # fetchData! is equivalent to: await fetchData()
151
+
152
+ def fetchJSON(url)
153
+ response = fetch!(url)
154
+ response.json!
155
+
156
+ # Fetch data with !
157
+ data = fetchJSON!("https://jsonplaceholder.typicode.com/todos/1")
158
+ console.log "Todo:", data.title
159
+
160
+ # Error handling
161
+ def safeFetch(url)
162
+ try
163
+ response = fetch!(url)
164
+ unless response.ok
165
+ throw new Error "HTTP #{response.status}"
166
+ response.json!
167
+ catch err
168
+ console.error "Failed:", err.message
169
+ null
170
+
171
+ # Multiple async operations
172
+ urls = [
173
+ "https://jsonplaceholder.typicode.com/todos/1"
174
+ "https://jsonplaceholder.typicode.com/todos/2"
175
+ ]
176
+
177
+ results = Promise.all!(urls.map (url) -> fetch!(url).json!)
178
+ for item in results
179
+ console.log "- #{item.title}"
180
+ '''