@redshift/core v0.7.0
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 @redshift/cli
Generating project files
Redshift provides CLI that helps with scaffolding code.
redshift create <project_name>
cd <project_name>
Once generated you can use npm scripts to run your application.
REPL mode
redshift repl
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
Function expressions
Redshift lets you define function expressions by using ->
syntax.
Expression after arrow will be returned.
To call anonymous function, dot syntax is used. This indicates that called function is an anonymous function and pattern matching should not be performed.
let double = (a) -> a * 2
double.(2) # 4
let greeted = List.map(names, (name) -> "Hello " <> name <> "!")
Constants
Because of lack of mutations, redshift supports constants only. Constants are block scoped.
let test_constant = 2
let result = 2 * 3
Constants can also be used inside functions
let a = 5
def func() do
let a = 40
let b = 100
a + b
end
a # 5
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 "
.
let hello_world = "Hello world!"
To concat two strings use concatenation operator <>
.
let str1 = "Hello "
let str2 = "world!"
let result = str1 <> str2
List Literals
Lists are declared using square parentheses []
.
let names = ["tom", "mark", "andrew"]
To concat two lists use concatenation operator ++
.
let names = ["tom", "mark", "adam"]
let other_names = ["anna", "kasia", "ewelina"]
let all = names ++ other_names
To perform more advanced operations like mapping, reducing, filtering and searching use List module
import List
let numbers = [1, 2, 3]
let double = (num) -> num * 2
let doubled = List.map(numbers, double)
## Head | Tail
let head = List.head(numbers) # 1
let tail = List.tail(numbers) # [2, 3]