"I thought of objects being like biological cells and/or individual computers on a network, only able to communicate with messages" --Alan Kay, creator of Smalltalk, on the meaning of "object oriented programming"
Jet is a simple OOP, dynamically typed, functional language that runs on the Erlang virtual machine (BEAM). Jet's syntax is Ruby-like syntax. Jet was inspired by Reia and Celluloid.
### Numbers
49 # integer
4.9 # float
### Booleans
true
false
### Atoms
:foo
### Lists
list = [2, 3, 4]
list2 = [1, *list] # => [1, 2, 3, 4]
[1, 2, 3, *rest] = list2
rest # => [4]
list.append(5) # => [2, 3, 4, 5]
list # => [2, 3, 4]
list.select {|item| item > 2}
.map {|item| item * 2} # => [6, 8]
list # => [2, 3, 4]
# list comprehensions
[n * 2 for n in list] # => [4, 6, 8]
### Tuples
tuple = {1, 2, 3}
tuple.select {|item| item > 1}
.map {|item| item * 2} # => [4, 6]
tuple.to_list # => [1, 2, 3]
### Maps
dict = {foo: 1, bar: 2}
dict2 = dict.put(:baz, 3) # => {foo: 1, bar: 2, baz: 3}
dict # => {foo: 1, bar: 2}
dict.get(:baz, 100) # => 100
### Strings (Lists)
"Abc"
### Anonymous functions (Blocks)
add = {|x, y| x + y}
add(40, 9) # => 49
multiply = do |x, y|
x * y
end
multiply(7, 7) # => 49
### Binaries
<<1, 2, 3>>
<<"abc">>
<<1 , 2, x>> = <<1, 2, 3>>
x # => 3
Car.jet
Module Car
class Car
# On jet, state of an instance is immutable.
# The initialize method returns initial state of an instance.
def initialize()
{name: "foo",
speed: 100}
end
def display()
@name.display()
@speed.display()
end
end
end
Enumerable.jet
module Enumerable
def select(func)
reduce([]) {|item, acc|
if func.(item)
acc ++ [item]
else
acc
end
}
end
def filter(func)
reduce([]) {|item, acc|
if func.(item)
acc ++ [item]
else
acc
end
}
end
def reject(func)
reduce([]) {|item, acc|
if func.(item)
acc
else
acc ++ [item]
end
}
end
def map(func)
reduce([]) {|item, acc|
acc ++ [func.(item)]
}
end
def collect(func)
reduce([]) {|item, acc|
acc ++ [func.(item)]
}
end
def min(func)
reduce(:infinity) {|item, acc|
match func.(acc, item)
case -1
0
case 0
0
case 1
item
end
}
end
def min()
reduce(:infinity) {|item, acc|
if acc <= item
acc
else
item
end
}
end
def unique()
reduce([]) {|item, acc|
if acc.index_of(item)
acc
else
acc ++ [item]
end
}
end
def each(func)
reduce([]) {|item, acc|
func.(item)
}
end
end
SampleList.jet
module SampleList
class SampleList
include Enumerable
def initialize(items)
{items: items}
end
def reduce(acc, func)
lists::foldl(func, acc, @items)
end
end
end
sample_list = SampleList::SampleList.new([1, 2, 3])
sample_list.select {|item| item > 1}
.map {|item| item * 2}
# => [4, 6]
- Tail recursion optimization
- Pattern matching
- Class inheritance
- Macro definition
- Erlang/OTP >= 18.0
- Elixir >= 1.1
$ git clone https://github.com/i2y/jet.git
$ cd jet
$ mix archive.build
$ mix archive.install
$ mix escript.build
$ cp jet <any path>
Compiling:
$ ls
Foo.jet
$ jet Foo.jet
$ ls
Foo.beam Foo.jet
Compiling and Executing:
$ cat Foo.jet
module Foo
def self.bar()
123.display()
end
end
$ jet -r Foo::bar Foo.jet
123
mix.exs file example:
defmodule MyApp.Mixfile do
use Mix.Project
def project do
[app: :my_app,
version: "1.0.0",
compilers: [:jet|Mix.compilers],
deps: [{:jet, git: "https://github.com/i2y/jet.git"}]]
end
end
".jet" files in source directory(src) is automatically compiled by mix command.