-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7a265ed
commit b59b06a
Showing
14 changed files
with
790 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class A { | ||
var x = 1 | ||
|
||
@to_string() { | ||
return 'Some random string ${self.x}' | ||
} | ||
} | ||
|
||
@{ | ||
echo to_string(A()) | ||
}() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
class A { | ||
var a = 5 | ||
|
||
test2() { | ||
echo 'Test 2 called...' | ||
} | ||
|
||
test() { | ||
var b = 20 | ||
catch { | ||
echo [][10] | ||
} as e | ||
|
||
echo 'Finally...' | ||
self.test2() | ||
if self.a { | ||
echo self.a | ||
} | ||
echo b | ||
|
||
if e { | ||
echo 'Error occurred:' | ||
echo e.message | ||
echo e.stacktrace | ||
} | ||
} | ||
} | ||
|
||
A().test() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
def generate_fibonacci(n) { | ||
var fib = [1] * n | ||
for i in 2..n { | ||
fib[i] = fib[i - 2] + fib[i - 1] | ||
} | ||
return fib | ||
} | ||
|
||
var fib = generate_fibonacci(35) | ||
print(' '.join(fib)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
def test(a, i) { | ||
return to_string(a) + ' ' + i | ||
} | ||
|
||
var text = 'all is well' | ||
for i in 0..100 { | ||
echo text.replace_with('/([a-z]+)/', @(match, val) { | ||
if val == 'is' return 'is not' | ||
return test(val, i) | ||
}) | ||
} | ||
|
||
[10, 20, 30, 40].each(@(x, y, z) { | ||
echo 'It works! ${y} -> ${x}' | ||
}) | ||
|
||
{name: 'Richard', age: 40}.each(@(x, y, z) { | ||
echo '${y} -> ${x}' | ||
}) | ||
|
||
bytes([13,219,79]).each(@(b) { | ||
echo b | ||
}) | ||
|
||
echo [1,2,3].map(@(v) { | ||
return v * 2 | ||
}) | ||
|
||
echo [1,2,3,4,5,6].filter(@(x) { return x % 2 != 0 }) | ||
|
||
echo [1,2,3,4,5].some(@(x) { return x % 2 != 0 }) | ||
echo [1,2,3,4,5].every(@(x) { return x % 2 != 0 }) | ||
|
||
var people = { | ||
john: { | ||
name: 'John', | ||
age: 40, | ||
address: 'London, England', | ||
}, | ||
daniel: { | ||
name: 'Daniel', | ||
age: 31, | ||
address: 'Lagos, Nigeria', | ||
} | ||
} | ||
|
||
echo people.filter(@(x) { | ||
return x.address.index_of('Lagos') != -1 | ||
}) | ||
|
||
echo people.some(@(x){ return x.age > 30 }) | ||
echo people.every(@(x){ return x.age > 40 }) | ||
|
||
echo [1, 100].reduce(@(i, x) { return max(i, x) }, 50) | ||
echo [1, 100].reduce(@(i, x) { return max(i, x) }) | ||
echo [].reduce(@(i, x) { return max(i, x) }) | ||
|
||
echo [15, 16, 17, 18, 19].reduce( | ||
@(accumulator, currentValue) { return accumulator + currentValue }, | ||
10 | ||
) | ||
|
||
def reducer(accumulator, currentValue, index) { | ||
var returns = accumulator + currentValue | ||
echo 'accumulator: ${accumulator}, currentValue: ${currentValue}, index: ${index}, returns: ${returns}' | ||
return returns | ||
} | ||
|
||
echo [15, 16, 17, 18, 19].reduce(reducer) | ||
|
||
var objects = [{ x: 1 }, { x: 2 }, { x: 3 }] | ||
echo objects.reduce( | ||
@(accumulator, currentValue) { return accumulator + currentValue.x }, | ||
0 | ||
) | ||
|
||
# A LITTLE COMPLEX EXAMPLE | ||
|
||
var pipe = @(...) { | ||
var functions = __args__ | ||
return @(initialValue) { | ||
return functions.reduce(@(acc, fn) { return fn(acc) }, initialValue) | ||
} | ||
} | ||
|
||
# Building blocks to use for composition | ||
var double = @(x) { return 2 * x } | ||
var triple = @(x) { return 3 * x } | ||
var quadruple = @(x) { return 4 * x } | ||
|
||
# Composed functions for multiplication of specific values | ||
var multiply6 = pipe(double, triple); | ||
var multiply9 = pipe(triple, triple); | ||
var multiply16 = pipe(quadruple, quadruple); | ||
var multiply24 = pipe(double, triple, quadruple); | ||
|
||
# Usage | ||
echo multiply6(6); # 36 | ||
echo multiply9(9); # 81 | ||
echo multiply16(16); # 256 | ||
echo multiply24(10); # 240 | ||
|
||
|
||
echo [1, 2, nil, 4].reduce(@(a, b) { return a + b }) | ||
|
||
echo {name: 'Richard', age: 40}.reduce(@(x, y, z){ return x += z + ' => ' + y + '\n' }, '') | ||
|
||
'name'.replace_with('/m/', @(match, offset) { | ||
echo offset | ||
return match | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import imagine {*} | ||
import os | ||
|
||
catch { | ||
if !os.dir_exists('./tmp') | ||
os.create_dir('./tmp') | ||
|
||
Image.new(130, 20, true).use(@(im) { | ||
im.save_alpha() | ||
|
||
var bg_color = im.allocate_color(0, 0, 0, 127) | ||
var fore_color = im.allocate_color(233, 14, 91) | ||
|
||
im.fill(0, 0, bg_color) | ||
im.string(5, 2, 'A simple text string', FONT_REGULAR, fore_color) | ||
|
||
im.export_png('./tmp/image1.png') | ||
}) | ||
|
||
Image.new(100, 100).use(@(im) { | ||
var black = im.allocate_color(0, 0, 0) | ||
var white = im.allocate_color(255, 255, 255) | ||
|
||
im.fill(0, 0, white) | ||
im.filled_arc(50, 50, 98, 98, 0, 204, black) | ||
|
||
im.export_webp('./tmp/image2.webp') | ||
}) | ||
|
||
Image.new(640, 640, true).use(@(im) { | ||
var bg_color = im.allocate_color(0, 0, 0, 127) | ||
im.fill(0, 0, bg_color) | ||
|
||
Image.from_webp('./tmp/image2.webp').use(@(im2) { | ||
var meta = im2.meta() | ||
|
||
im.copy_resized(im2, 0, 0, 0, 0, 640, 640, meta.width, meta.height) | ||
}) | ||
|
||
im.export_png('./tmp/image3.png') | ||
}) | ||
} as e | ||
|
||
if e { | ||
echo '' | ||
echo 'IMAGINE TEST ERROR:' | ||
echo '======================================================' | ||
echo e.message | ||
echo e.stacktrace | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import json | ||
|
||
echo json.encode('️Hell"s not\'s here') | ||
echo json.encode(1) | ||
echo json.encode(true) | ||
echo json.encode(nil) | ||
echo json.encode(-2.555551688) | ||
echo json.encode({}) | ||
echo json.encode({}, false) | ||
echo json.encode([]) | ||
echo json.encode([], false) | ||
echo json.encode({name: 'Richard'}) | ||
echo json.encode({name: 'Richard'}, false) | ||
echo json.encode([1, 2, 3, 4, 5]) | ||
echo json.encode([1, 2, 3, 4, 5], false) | ||
echo json.encode(['apple', 'mango', 'oranges']) | ||
echo json.encode(['apple', 'mango', 'oranges'], false) | ||
echo json.encode([{name: 'Richard'}]) | ||
echo json.encode([{name: 'Richard'}], false) # non compact | ||
|
||
echo json.encode([{"precision": "zip", | ||
"Latitude": 37.7668, | ||
"Longitude": -122.3959, "Address": "", | ||
"City": "SAN FRANCISCO", | ||
"State": "CA", "Zip": "94107", | ||
"Country": "US" | ||
}, { | ||
"precision": "zip", | ||
"Latitude": 37.371991, "Longitude": -122.026020, | ||
"Address": "", | ||
"City": "SUNNYVALE", | ||
"State": "CA", "Zip": "94085", | ||
"Country": "US" | ||
}], false) |
Oops, something went wrong.