Samples

Sample Rip source code.

```language-rip # comments start with `#` and extend to the end of line

System.IO.puts("Hello world")

can also be written as

System.IO puts "Hello world"

<figcaption>Comments and very basic IO</figcaption>
</figure>

---

<figure>
```language-rip
interesting_numbers = {
	'Prime': [2, 3, 5, 7, 11, 13],
	'Fibonacci': [1, 1, 2, 3, 5, 8],
	'Square': [1, 4, 9, 16, 25],
}

interesting_numbers.fold(0, -> (largest, kind, numbers) {
	(largest >> numbers)<System.Integer>.maximum()
})
Transliteration from a Swift code example in Apple's "The Swift Programming Language". https://itun.es/us/jEUH0.l

```language-rip List = type { fold = -> (folder, initial, items) { # stuff }
filter = -> (sieve, items) {
	fold(-> (memo, item) {
		match (sieve(item)) {
			when (true) { memo << item }
			else        { memo }
		}
	}, items.type.new(), items)
}

map = -> (mapper, items) {
	fold(-> (memo, item) {
		memo << mapper(item)
	}, [], items)
}

@.fold = -> (initial, folder) {
	@.type.fold(folder, initial, @)
}

@.filter = -> (sieve) {
	@.type.filter(sieve, @)
}

@.map = -> (mapper) {
	@.type.map(mapper, @)
}

}

<figcaption>list.rip</figcaption>
</figure>


<figure>
```language-rip
List = System.require('list')

numbers = [1, 2, 3, 4, 5]

is_even = -> (n) { n % 2 == 0 }
double = -> (n) { n * 2 }
square = -> (n) { n * n }

# object-oriented
numbers.filter(is_even)             # [2, 4]
numbers.map(double)                 # [2, 4, 6, 8, 10]
numbers.map(square).filter(is_even) # [4, 16]

# procedural
List.filter(is_even, numbers)                   # [2, 4]
List.map(double, numbers)                       # [2, 4, 6, 8, 10]
List.filter(is_even, List.map(square, numbers)) # [4, 16]

# functional (combinative)
even_square = List.map(square) + List.filter(is_even)
even_square(numbers) # [4, 16]

# functional (pipeline)
numbers |> List.map(square) |> List.filter(is_even) # [4, 16]
main.rip

```language-rip # comment

_true = true
_false = false

integer = 42
decimal = 3.14
rational = 2 / 3

complex = <integer, decimal.i>

date = 1980-10-16
time = 13:59:00
datetime = 1980-10-16T13:59:00

pair = key : value
range = 1..5

list = []
map = {}

character = `c

regular_expression = /abc/

string_symbol = :rip
string_single = 'foo'
string_double = "b#{a}r"

string_heredoc = <<-RIP
hello, world!
RIP

Person = type {
@.initialize = -> (name) { @.name = name }

find = -> (id) { }

}

People = type (System.List) {
all = => {
-> { new([]) }

	-> (filter) {
		new([].where(filter))
	}
}

}

<figcaption>Sample code used to develop a grammar for the <a href="http://prismjs.com/">Prism syntax highlighter</a>.</figcaption>
</figure>

---

<figure>
```language-rip
factorial1 = -> (n, accumulator = 1) {
	match (n) {
		when (0) { accumulator }
		else     { self(n - 1, n * accumulator) }
	}
}

factorial2 = => {
	-> (n, accumulator = 1) {
		match (n) {
			when (0) { accumulator }
			else     { self(n - 1, n * accumulator) }
		}
	}
}

factorial3 = => {
	-> (n) {
		self(n, 1)
	}

	-> (n, accumulator) {
		match (n) {
			when (0) { accumulator }
			else     { self(n - 1, n * accumulator) }
		}
	}
}
Dash-rocket and optional parameters get expanded into a lambda with multiple overloads. These factorial implementations are equivalent.

```language-rip System.Math.e # Euler's number System.Math.golden # golden ratio φ System.Math.i # square root of -1 System.Math.PI # π to five decimal points (same as `System.Math.PI_to(5)`, but only calculated once) System.Math.PI_to # lambda that calculates π to `n` decimal points ```
Constants
```language-rip System.Integer #=> 1 System.Rational #=> 3.14 System.Rational #=> 2 / 3

would love to implement division in such a way that if the number can

divide evenly, it just returns the integer, otherwise a rational

<figcaption>Real Numbers</figcaption>
</figure>

<figure>
```language-rip
# access the dynamic property `i` on real numbers for their imaginary counterparts
# (same as multiplying the real number by System.Math.i)

System.Integer  #=> 1.i
System.Decimal  #=> 3.14.i
System.Rational #=> (2 / 3).i
Imaginary Numbers
# Add a real number and an imaginary number to produce a complex number. Rip doesn't support higher-dimension numbers.

System.Complex #=> 42 + 3.i
Complex Numbers