Skip to content
/ jet Public

Jet is a simple OOP, dynamically typed, functional language that runs on the Erlang virtual machine (BEAM). Jet's syntax is Ruby-like syntax.

License

Notifications You must be signed in to change notification settings

i2y/jet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

75 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

"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.

Language features

Builtin Types

### 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

Class definition

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

Module definition

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

Mixing in Modules

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

Trailing closures (Trailing blocks)

sample_list = SampleList::SampleList.new([1, 2, 3])
sample_list.select {|item| item > 1}
           .map {|item| item * 2}
           # => [4, 6]

Other supported features

  • Tail recursion optimization
  • Pattern matching

Currently unsupported features

  • Class inheritance
  • Macro definition

Requirements

  • Erlang/OTP >= 18.0
  • Elixir >= 1.1

Installation

$ git clone https://github.com/i2y/jet.git
$ cd jet
$ mix archive.build
$ mix archive.install
$ mix escript.build
$ cp jet <any path>

Usage

Command

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

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.

About

Jet is a simple OOP, dynamically typed, functional language that runs on the Erlang virtual machine (BEAM). Jet's syntax is Ruby-like syntax.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published