Own-Programming-Language-Tu.../README.md

158 lines
3.2 KiB
Markdown
Raw Permalink Normal View History

# OwnLang
2019-01-06 16:46:38 +02:00
OwnLang - dynamic functional programming language inspired by Scala and Python. Available for PC, Android and Java ME devices.
## Installing
2016-07-16 11:54:21 +03:00
| Free | Pro | Desktop |
| :--: | :-: | :-----: |
2024-05-01 21:33:00 +03:00
| [![Free](https://developer.android.com/images/brand/en_generic_rgb_wo_45.png)](https://play.google.com/store/apps/details?id=com.annimon.ownlang.free) | [![Pro](https://developer.android.com/images/brand/en_generic_rgb_wo_45.png)](https://play.google.com/store/apps/details?id=com.annimon.ownlang) | [v2.0.0](https://github.com/aNNiMON/Own-Programming-Language-Tutorial/releases/tag/v2.0.0) |
2016-07-16 11:54:21 +03:00
2019-01-06 16:46:38 +02:00
Also available as AUR package:
```
yay -S ownlang
```
2016-06-30 14:26:53 +03:00
## Key features
2016-09-24 16:21:13 +03:00
#### Higher-order functions
2016-06-30 14:26:53 +03:00
Functions are values, so we can store them to variables for operating.
```scala
operations = {
"+" : def(a,b) = a+b,
"-" : def(a,b) = a-b,
"*" : def(a,b) = a*b,
"/" : ::division
}
2016-06-30 14:26:53 +03:00
def division(v1, v2) {
if (v2 == 0) return "error: division by zero"
2016-06-30 14:26:53 +03:00
return v1 / v2
}
for name, operation : operations {
println "2 " + name + " 3 = " + operation(2, 3)
2016-06-30 14:26:53 +03:00
}
```
#### Pattern Matching
Pattern matching with value pattern, tuple pattern, list pattern and optional condition.
```scala
def factorial(n) = match n {
case 0: 1
case n if n < 0: 0
case _: n * factorial(n - 1)
}
def fizzbuzz(limit = 100) {
for i = 1, i <= limit, i++ {
println match [i % 3 == 0, i % 5 == 0] {
case (true, false): "Fizz"
case (false, true): "Buzz"
case (true, true): "FizzBuzz"
case _: i
}
}
}
// run
fizzbuzz()
2016-06-30 14:26:53 +03:00
```
#### Functional data operations
Operate data in functional style.
```scala
2023-09-09 15:52:32 +03:00
use std, functional
2016-06-30 14:26:53 +03:00
nums = [1,2,3,4,5,6,7,8,9,10]
nums = filter(nums, def(x) = x % 2 == 0)
// Squares of even numbers
squares = map(nums, def(x) = x * x)
foreach(squares, ::echo)
// Sum of squares
sum = reduce(squares, 0, def(x, y) = x + y)
println "Sum: " + sum
2019-01-06 16:46:38 +02:00
// Same using stream
println "Sum: " + stream(range(1, 11))
.filter(def(x) = x % 2 == 0)
.map(def(x) = x * x)
.reduce(0, def(x, y) = x + y)
2016-06-30 14:26:53 +03:00
```
#### Operator overloading
Why not?
```scala
2023-09-09 15:52:32 +03:00
use std, types, math
2016-06-30 14:26:53 +03:00
2019-01-06 16:46:38 +02:00
def `..`(a, b) = range(a, b)
2016-06-30 14:26:53 +03:00
def `**`(a, b) = int(pow(a, b))
for y : 1 .. 10 {
println sprintf("2 ^ %d = %d", y, 2 ** y)
}
```
#### Network module
Easy async HTTP requests with `http` module.
```scala
2024-04-28 22:39:30 +03:00
use std, http, functional, json
2016-06-30 14:26:53 +03:00
// GET request
http("https://api.github.com/events", def(r) {
events = jsondecode(r)
println events[0]
2016-06-30 14:26:53 +03:00
})
// POST request
http("http://jsonplaceholder.typicode.com/users", "POST", {
"name": "OwnLang",
"versionCode": 10
}, ::echo)
// PATCH request
http("http://jsonplaceholder.typicode.com/users/2", "PATCH", {"name": "Patched Name"}, ::patch_callback)
def patch_callback(v) {
println v
}
```
## Language specification
[English](https://annimon.com/docs/ownlang/en/) and [Russian](https://annimon.com/docs/ownlang/ru/)
2016-06-30 14:26:53 +03:00
[Examples](examples/)
## Build
2023-11-20 21:20:11 +02:00
Build using Gradle `./gradlew shadowJar`
or take a look to [latest release](https://github.com/aNNiMON/Own-Programming-Language-Tutorial/releases/latest) for binaries.
## Run
To run script use command
`java -jar OwnLang.jar -f input.own`
or
`java -jar OwnLang.jar < input.own`
## License
2024-01-12 22:04:31 +02:00
MIT - see [MIT license information](LICENSE)