@@ -16,20 +16,20 @@ io = import("std/io")
1616strings = import (" std/strings" )
1717
1818fn main() {
19- list = [" Not " , " Yet " , " Yellow " , " World" , " But " , " Almost " ]
19+ list = [" Hi " , " World! " , " Goodbye " ]
2020
2121 text = list
22- | > slice (2 , len (list ) - 2 ) # Slice the list to get rid of extra words.
23- | > strings.join(" " ) # Create a string by joining words from the list.
24- | > strings.replace(" Yellow " , " Hello" ) # Fix the typo (Yellow > Hello).
25- | > append( " ! " ) # Finally append !, although the + operator also works.
22+ | > slice (0 , len (list ) - 1 )
23+ | > strings.join(" " )
24+ | > strings.replace(" Hi " , " Hello" )
25+ | > strings.upper()
2626
2727 io.println(text)
2828}
2929```
3030
3131```
32- Hello World !
32+ HELLO WORLD !
3333```
3434
3535## Philosophy
@@ -40,26 +40,23 @@ The core guiding principle is minimalism, but without making it boringly simple.
4040
4141## Features
4242
43- * Minimalist but ergonomic and consistent syntax
44- * Lists (immutable Python-like lists)
45- * Records (immutable Javascript-like objects)
46- * First-class functions
47- * Lambdas
48- * Closures
49- * Function pipe operator
43+ * Minimalist, ergonomic and consistent syntax
44+ * Immutable Python-like lists
45+ * Immutable JavaScript-like records
46+ * First-class functions, lambdas and closures
47+ * Function pipe operator (` |> ` )
5048* Easy collection iteration
51- * Functions that might fail return { "error": Bool, "value": Value }
52- * One single way to do things
53- * Rich built-in std library
54- * Balanced imperative/functional style
55- * Ideal for new and experienced programmers
56- * Implemented in Rust taking advantage of its ownership model (no GC)
49+ * Lightweight error propagation with ` ?= `
50+ * Rich standard library
51+ * Balanced imperative and functional style
52+ * Friendly to both new and experienced programmers
53+ * Implemented in Rust without a garbage collector
5754
5855## Examples
5956
6057### Example 1
6158
62- ``` Python
59+ ``` python
6360io = import (" std/io" )
6461
6562fn generic_operation(a, b, operation) {
@@ -89,7 +86,7 @@ Output:
8986
9087### Example 2
9188
92- ``` Python
89+ ``` python
9390io = import (" std/io" )
9491
9592fn even_odd(numbers) {
@@ -126,57 +123,14 @@ Output:
126123
127124### Example 3
128125
129- ``` Python
130- conv = import (" std/conv" )
131- io = import (" std/io" )
132-
133- # Define a function that "updates" a user (records are immutable).
134- fn birthday(user) {
135- return {
136- name: user.name,
137- age: user.age + 1
138- }
139- }
140-
141- # Create a record.
142- user = {
143- name: " Vasco" ,
144- age: 44
145- }
146-
147- io.println(" User:" )
148- io.println(" Name: " + user.name)
149- io.println(" Age: " + conv.string(user.age))
150-
151- # Create a new updated record.
152- updated = birthday(user)
153-
154- io.println()
155- io.println(" After birthday:" )
156- io.println(" Name: " + updated.name)
157- io.println(" Age: " + conv.string(updated.age))
158- ```
159-
160- ```
161- User:
162- Name: Vasco
163- Age: 44
164-
165- After birthday:
166- Name: Vasco
167- Age: 45
168- ```
169-
170- ### Example 4
171-
172- ``` Python
126+ ``` python
173127env = import (" std/env" )
174128http = import (" std/http" )
175129io = import (" std/io" )
176130strings = import (" std/strings" )
177131
178132fn get_weather(location) {
179- # Get may fail so it returns { "error": Bool, "value": Value }.
133+ # Get may fail so it returns { "error": Bool, "value": Any }.
180134 # The last expression is returned even without the return keyword.
181135 # A function returns an expression that evaluates to a value and is returned.
182136 http.get(" https://wttr.in/" + location + " ?format=3" )
@@ -198,7 +152,7 @@ fn main() {
198152
199153 result = get_weather(location)
200154
201- # By convention functions that might fail return { "error": Bool, "value": Value }.
155+ # By convention functions that might fail return { "error": Bool, "value": Any }.
202156 # Checking this record for an error is a common pattern in gluonscript.
203157 # If error is true, value shows its message as a string.
204158 # Otherwise if error is false, value shows whatever value the function returns.
@@ -216,41 +170,39 @@ Output:
216170lisbon: 🌦 +14°C
217171```
218172
219- ## Install
220-
221- ### From binaries
222-
223- - Fetch the [ latest release] ( https://github.com/vascocosta/gluonscript/releases ) for your platform from GitHub.
224-
225- - Extract the archive into a ` .gluonscript ` folder at the root of your ` HOME ` folder.
226-
227- - Add the location of the ` .gluonscript\bin ` to your ` PATH ` .
228-
229- ### From source
173+ ### Example 4
230174
231- - Fetch the source code from GitHub by cloning the repo with:
175+ ``` python
176+ http = import (" std/http" )
177+ io = import (" std/io" )
178+ json = import (" std/json" )
179+
180+ fn fetch(url) {
181+ # The ?= operator (error propagation) is similar to Rust's ? operator.
182+ # It makes it easy to handle { "error": Bool, "value": Any } return types.
183+ # If the value on the right is an error, it propagates the error to the caller.
184+ # Otherwise it unwraps value and assigns it to the variable on the left.
185+
186+ raw_json ? = http.get(url) # Get raw json from server or propagate error.
187+ parsed_json ? = json.parse(raw_json) # Parse JSON into a record or propagate error.
188+
189+ # Functions using the ?= operator should return { "error": Bool, "value": Any }.
190+ # This record is similar to Rust's Result type, but simpler and without generics.
191+ { error: false, value: parsed_json }
192+ }
232193
233- ```
234- git clone https://github.com/vascocosta/gluonscript.git
235- ```
194+ fn main() {
195+ content = fetch(" https://catfact.ninja/fact" )
236196
237- - Compile the source code (you need the ` Rust toolchain ` ):
197+ if content.error {
198+ io.println(" There was an error: " + content.value)
199+ return 1
200+ }
238201
202+ io.println(content.value.fact)
203+ }
239204```
240- cd gluonscript
241- cargo build -r
242- ```
243-
244- ### Notes
245-
246- As you can see there is no installer, the interpreter is self-contained and can be executed from anywhere. However, it is strongly recommended that you add the location of the binary to your ` PATH ` variable so that you can run ` gluonscript ` from anywhere on the command line.
247-
248- #### Running a script
249-
250- - Edit ` my_script.gs ` on your preferred IDE.
251-
252- - Run the script from the command line:
253205
254206```
255- gluonscript my_script.gs
207+ A cat can jump 5 times as high as it is tall.
256208```
0 commit comments