Functional Programming
Why doesn't FSL use traditional for and while loops?
Because a gemstone design is really a pipeline of geometric transformations: you take a list of gear teeth or angles, transform them mathematically, and turn them into facets. In FSL, arrays never mutate out from under you, and every calculation is an expression that returns a clean value. Once you get the hang of map, filter, reduce, and forEach, you can generate complex multi-tier crowns and custom preforms in just a few readable lines.
Arrow Functions & Closures
Define reusable functions with the => arrow syntax:
// Single parameter
double = (x) => x * 2
result = double(5) // 10
print("Double:", result)
// Multiple parameters
add = (a, b) => a + b
sum = add(3, 4) // 7
print("Sum:", sum)
// Block body for multi-step calculations (returns final expression)
calc = (a, b) => {
temp = a + b
temp * 2
}
value = calc(3, 4) // 14
print("Calc:", value)
-- lua script
-- Single parameter
local double = function(x) return x * 2 end
local result = double(5) -- 10
print("Double:", result)
-- Multiple parameters
local add = function(a, b) return a + b end
local sum = add(3, 4) -- 7
print("Sum:", sum)
-- Block body for multi-step calculations
local calc = function(a, b)
local temp = a + b
return temp * 2
end
local value = calc(3, 4) -- 14
print("Calc:", value)
Arrow functions capture variables from their enclosing scope (closures), making it easy to build specialized helpers:
// Closure captures 'offset' from outer scope
makeAdder = (offset) => (x) => x + offset
addFive = makeAdder(5)
result = addFive(10) // 15
print("Closure result:", result)
-- lua script
local makeAdder = function(offset)
return function(x) return x + offset end
end
local addFive = makeAdder(5)
local result = addFive(10) -- 15
print("Closure result:", result)
Default Parameters
Parameters can declare default values that apply whenever an argument is omitted:
greet = (name, greeting = "Hello") => print(greeting, name)
greet("World") // prints "Hello World"
greet("World", "Howdy") // prints "Howdy World"
-- lua script
local greet = function(name, greeting)
greeting = greeting or "Hello"
print(greeting, name)
end
greet("World") -- prints "Hello World"
greet("World", "Howdy") -- prints "Howdy World"
Conditional Logic (? : and cond)
Use the ternary operator condition ? trueVal : falseVal to select values inline:
angle = 67
side = angle > 45 ? "steep" : "shallow"
print("Side is", side)
// Chained ternary
score = 84
grade = score >= 90 ? "A"
: score >= 80 ? "B"
: score >= 70 ? "C"
: "F"
print("Grade:", grade)
-- lua script
local angle = 67
local side = angle > 45 and "steep" or "shallow"
print("Side is", side)
local score = 84
local grade = score >= 90 and "A"
or score >= 80 and "B"
or score >= 70 and "C"
or "F"
print("Grade:", grade)
When you want to conditionally execute an action (such as cutting facets or logging), use cond(condition, trueThunk, falseThunk):
gear = 96
cond(gear == 96,
() => print("Standard 96 gear"),
() => print("Non-standard gear")
)
-- lua script
local gear_val = 96
if gear_val == 96 then
print("Standard 96 gear")
else
print("Non-standard gear")
end
Functional Array Methods
All array methods in FSL are pure—they return a new array without mutating the original.
map — Transform Elements
angles = [40, 42, 44]
radians = angles.map(a => Math.toRadians(a))
print("Radians:", radians)
-- lua script
local angles = {40, 42, 44}
local radians = {}
for i, a in ipairs(angles) do radians[i] = math.rad(a) end
print("Radians:", radians)
filter — Keep Matching Elements
values = [12, 5, 8, 20, 3]
big = values.filter(v => v > 10)
print("Big values:", big)
-- lua script
local values = {12, 5, 8, 20, 3}
local big = {}
for _, v in ipairs(values) do
if v > 10 then table.insert(big, v) end
end
print("Big values:", big)
reduce — Fold into a Single Value
nums = [1, 2, 3, 4]
total = nums.reduce((acc, n) => acc + n, 0)
print("Total:", total)
largest = nums.reduce((a, b) => a > b ? a : b)
print("Largest:", largest)
-- lua script
local nums = {1, 2, 3, 4}
local total = 0
local largest = nums[1]
for _, n in ipairs(nums) do
total = total + n
if n > largest then largest = n end
end
print("Total:", total)
print("Largest:", largest)
forEach — Execute Cuts or Side Effects
points = [Point(0,0,1), Point(0,1,0), Point(1,0,0)]
points.forEach(p => show(p, "cyan"))
-- lua script
local points = {Point(0,0,1), Point(0,1,0), Point(1,0,0)}
for _, p in ipairs(points) do
show(p, "cyan")
end
average, sum, min, max
average() computes the arithmetic mean of numbers or the geometric centroid of an array of Point/Vector objects:
readings = [40.2, 41.8, 40.5]
print("Mean:", readings.average())
pts = [Point(0,0,0), Point(10,0,0), Point(5,10,0)]
print("Centroid:", pts.average())
-- lua script
local readings = {40.2, 41.8, 40.5}
local mean = (readings[1] + readings[2] + readings[3]) / 3
print("Mean:", mean)
local pts = {Point(0,0,0), Point(10,0,0), Point(5,10,0)}
local center = (pts[1]:add(pts[2]):add(pts[3])):div(3)
print("Centroid:", center)
Chaining Pipelines
Because every transformation returns a new array, you can chain operations cleanly:
indices = Math.range(0, 96, 12)
.filter(i => i != 0)
.map(i => i + 3)
print("Chained indices:", indices)
-- lua script
local indices = {}
for i = 0, 95, 12 do
if i ~= 0 then
table.insert(indices, i + 3)
end
end
print("Chained indices:", indices)
Putting It All Together: Procedural Crown Tiers
Combine forEach, concat, and TierAD / TierAP to generate multi-tier patterns programmatically:
name = "Functional Crown Demo"
gear = 96
size = 1.0
P1 0 @ 41.8 : cp() x8
G1 0 @ 90 : size x16
crownAngles = [35, 28, 22, 15]
crownAngles.forEach((angle, i) => {
tier = concat("C", i + 1)
print(tier, "at", angle, "°")
})
print("Average crown angle:", crownAngles.average())
-- lua script
name("Functional Crown Demo")
gear(96)
TierAP("P1", 0, 41.8, cp(), "x8")
TierAD("G1", 0, 90, 1.0, "x16")
local crownAngles = {35, 28, 22, 15}
local sum = 0
for i, angle in ipairs(crownAngles) do
local tier = "C" .. i
print(tier, "at", angle, "°")
sum = sum + angle
end
print("Average crown angle:", sum / #crownAngles)