0.5.2 • Published 5 years ago

redshiftlang v0.5.2

Weekly downloads
4
License
ISC
Repository
github
Last release
5 years ago

Redshift - functional programming language

CircleCI Redshift is a functional programming language compiled to Javascript. It implements syntax similiar to Elixir. It is created as a part of learning how do compilers work, and it's mostly just proof of concept.

Key concepts

  • no mutations
  • no classes, no this
  • all functions must return

Getting Started

Installation

Install compiler via npm:

npm install -g redshiftlang

Generating project files

Redshift provides CLI that helps with scaffolding code.

redshift create <project_name>
cd <project_name>

Once generated you can yse npm scripts to run your application.

One time build

redshift build <path_to_file>.rh

Watching for changes

redshift watch <path_to_file>.rh

Both options accept optional output argument (it defaults to ./bin/bundle.js):

redshift build path/to/file.red -o dist/directory/build.js

Syntax

Function declaration

Functions are defined by use of def/do/end keywords. The last expression will be returned.

def sum(a, b) do
  a + b
end

Redshift takes note of the arity of the function (number of arguments), so it is possible to declare functions with same name, but different arity. For example following code is valid

def sum(a) do
  a + a
end

def sum(a, b) do
  a + b
end

Pattern matching

Redshift lets you use pattern matching in function declarations.

def error("syntax") do
  IO.puts("You used wrong syntax!")
end

def error("type") do
  IO.puts("Trying to assign different type")
end

def error("range") do
  IO.puts("Given value is out of range")
end

def error(type) do
  IO.puts(type <> " error occured!")
end

error("syntax") # You used wrong syntax!
error("Unknown") # Unknown error occured!

Function expressions

Redshift lets you define function expressions by using fn/->/end syntax.

double = fn(a) -> a * 2 end

Function expressions do not support pattern matching. To pass them as an argument to another function, they must be first stored in a variable. This is by design and improves code readability.

say_hello = fn(name) -> "Hello " <> name end

List.map(names, say_hello)

Constants

Because of lack of mutations, redshift supports constants only. Constants are block scoped.

test_constant = 2
result = 2 * 3

Constants can also be used inside functions

def func() do
  a = 40
  b = 100

  a + b
end

Imports

There are three types of imports.

System Imports

Load built-in libraries like Math or IO

import Math
import IO

Node Modules Imports

Load javascript module from node_modules directory

import "lodash" as _
import "ramda" as R

Internal file imports

Load modules from another files (either .rh or .js) File extension is required.

import "./lib/Auth.rh" as Auth
import "./js/User.js" as User

String literals

String literals are declared using double quotes ".

hello_world = "Hello world!"

To concat two strings use concatenation operator <>.

str1 = "Hello "
str2 = "world!"

result = str1 <> str2

List Literals

Lists are declared using square parentheses [].

names = ["tom", "mark", "andrew"]

To concat two lists use concatenation operator ++.

names = ["tom", "mark", "adam"]
other_names = ["anna", "kasia", "ewelina"]

all = names ++ other_names

To perform more advanced operations like mapping, reducing, filtering and searching use List module

import List

numbers = [1, 2, 3]
double = fn(num) -> num * 2 end

doubled = List.map(numbers, double)

## Head | Tail
head = List.head(numbers) # 1
tail = List.tail(numbers) # [2, 3]

Modules

Module is a namespace for the functions serving similiar purpose. Examples of built-in modules are:

Math - contianing functions for arythmetic operations

IO - containig functions for managind input and output

To decalre custom module defmodule / end notation is used.

defmodule User do

  def get_user() do
    # some logic
  end

  def delete_user() do
    # some logic
  end
end

Module is always 'default' exported. That is why it is only possible to have one module per file.

0.5.2

5 years ago

0.5.1

5 years ago

0.5.0

5 years ago

0.4.9

6 years ago

0.4.8

6 years ago

0.4.7

6 years ago

0.4.6

6 years ago

0.4.5

6 years ago

0.4.4

6 years ago

0.4.3

6 years ago

0.4.2

6 years ago

0.4.1

6 years ago

0.4.0

6 years ago

0.3.6

6 years ago

0.3.5

6 years ago

0.3.4

6 years ago

0.3.3

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.1

6 years ago