Skip to content
Roland edited this page Oct 3, 2016 · 1 revision

guard.lua is a small module which adds Elixir-style guards toLua.

Installation

Download the latest 0.0.1 release. You can also grab it with Luarocks.

luarocks install guard

Then add guard to your code.

local guard = require 'guard'

Usage

guard returns a function which, when being called, return a small table we can refer to as a guardian. It is local to the code and does not pollute the global environment.

local guard = require 'guard'

-- creates a guard
local g = guard()

###when(filter, f) Clauses can now be added to a guard using the when(filter, f) function call. This function expects two functions arguments: the first one being a filter function, and the second one being a function f to be evaluated. The filter function should return a boolean. When it results to true, the second function f is evaluated and the guard returns itself right after.

local guard = require 'guard'

-- a filter function which checks if the input is a even number
local is_odd = function(n) return n % 2 == 0 end

-- a function which doubles the input
local double = function(n) return n * 2 end

-- creates a guard
local g = guard().when(is_odd, double)
print(g(2)) -- returns 4
print(g(4)) -- returns 8
print(g(6)) -- returns 12

Multiple when-clauses can be defined with chaining, since the guard returns itself after a when-clause. When the guard will be called with given arguments later, the clauses will be examined in first-in-first-out order. In case a filter evaluates to true, its corresponding function is executed and the functions left will be ignored. In case all when-clauses evaluates to false, an error is returned.

local guard = require 'guard'

--is the number even?
local is_odd = function(n) return n % 2 == 0 end

-- is the number a multiple of 3
local is_multiple_of_3 = function(n) return n % 3 == 0 end

-- a function which double the input
local double = function(n) return n * 2 end

-- a function which triples the input
local triple = function(n) return n * 3 end

local g = guard().when(is_odd, double).when(is_multiple_of_3, triple)
print(g(2)) -- returns 4
print(g(3)) -- returns 9
print(g(4)) -- returns 8
print(g(6)) -- returns 18
print(g(7)) -- error : No guard defined for given arguments.

###any(f)

When a clause is defined with any(f), it acts a default clause which is always executed when there are no when-clauses defined.

local guard = require 'guard'

-- a function which returns the input
local function identity(n) return n end

local g = guard().any(identity)

print(g(1)) -- returns 1
print(g(2)) -- returns 2
print(g(3)) -- returns 3

The guard will also defaults to any-clauses in case all when-clauses evaluates to false.

local guard = require 'guard'

--is the number even?
local is_odd = function(n) return n % 2 == 0 end

-- is the number a multiple of 3
local is_multiple_of_3 = function(n) return n % 3 == 0 end

-- a function which double the input
local double = function(n) return n * 2 end

-- a function which triples the input
local triple = function(n) return n * 3 end

-- a function which returns the input
local function identity(n) return n end

local g = guard().when(is_odd, double).when(is_multiple_of_3, triple).any(identity)
print(g(1)) -- returns 1 (any)
print(g(2)) -- returns 4 (double)
print(g(3)) -- returns 9 (triple)
print(g(4)) -- returns 8 (double)
print(g(5)) -- returns 5 (any)
Clone this wiki locally