redshiftlang v0.5.2
Redshift - functional programming language
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, nothis - all functions must return
Getting Started
Installation
Install compiler via npm:
npm install -g redshiftlangGenerating 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>.rhWatching for changes
redshift watch <path_to_file>.rhBoth options accept optional output argument (it defaults to ./bin/bundle.js):
redshift build path/to/file.red -o dist/directory/build.jsSyntax
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
endRedshift 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
endPattern 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 endFunction 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 * 3Constants can also be used inside functions
def func() do
a = 40
b = 100
a + b
endImports
There are three types of imports.
System Imports
Load built-in libraries like Math or IO
import Math
import IONode Modules Imports
Load javascript module from node_modules directory
import "lodash" as _
import "ramda" as RInternal 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 UserString 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 <> str2List 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_namesTo 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
endModule is always 'default' exported. That is why it is only possible to have one module per file.
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago