Functional Programming Examples

Functional Programming Examples

This post is not to teach functional programming, but rather serve as an example of how you can approach it with luau. The data used for the examples can be listed below and they might not be fully correct as they’re just examples of the basics to each function.

Hopefully this can help anyone who is interested in Functional Programming and please reply on how I can improve these examples and functions.

Inspired by @sleitnick and made with the help of Copilot :heart:

Basic Filter function that goes through every value and returns a new table using a predicate.

local function Filter<T>(t : {T}, predicate : (value : T) -> boolean)
    local newTable = {}
    
    for key,value in pairs(t) do
        if predicate(value) then
            newTable[key] = value
        end
    end

    return newTable
end

Basic Map function that can create a new modified table.

local function Map<T, U>(t : {T}, mapper : (value : T) -> U)
    local newTable = {}
    
    for key,value in pairs(t) do
        newTable[key] = mapper(value)
    end

    return newTable
end

Reducer function used to return a single of a table

local function Reduce<T, U>(t : {T}, reducer : (accumulator : U, value : T) -> U, initialValue : U)
    local accumulator = initialValue
    
    for _,value in pairs(t) do
        accumulator = reducer(accumulator, value)
    end

    return accumulator
end

Sorting functions

local function Sort<T>(t : {T}, comparator : (a : T, b : T) -> boolean)
    local newTable = {}
    
    for key,value in pairs(t) do
        table.insert(newTable, value)
    end

    table.sort(newTable, comparator)

    return newTable
end
local function SortBy<T, U>(t : {T}, mapper : (value : T) -> U, comparator : (a : U, b : U) -> boolean)
    local newTable = {}
    
    for key,value in pairs(t) do
        table.insert(newTable, value)
    end

    table.sort(newTable, function(a, b)
        return comparator(mapper(a), mapper(b))
    end)

    return newTable
end

Find functions

local function Find<T>(t : {T}, predicate : (value : T) -> boolean)
    for _,value in pairs(t) do
        if predicate(value) then
            return value
        end
    end

    return nil
end
local function FindIndex<T>(t : {T}, predicate : (value : T) -> boolean)
    for key,value in pairs(t) do
        if predicate(value) then
            return key
        end
    end

    return nil
end

Examples

local function GetFighterIndexByName(name)
    return FindIndex(fighters, function(fighter)
        return fighter.Name == name
    end)
end
local function GetFighterByName(name)
    return Find(fighters, function(fighter)
        return fighter.Name == name
    end)
end
local function SortFightersByExperience(t)
    return Sort(t, function(a, b)
        return a.Experience > b.Experience
    end)
end
local function SortFightersByExperience(t)
    return SortBy(t, function(fighter)
        return fighter.Experience
    end, function(a, b)
        return a > b
    end)
end
local function GetTotalHealthOfFighters(t)
    return Reduce(t, function(accumulator, fighter)
        return accumulator + fighter.Health
    end, 0)
end
local function GetFighterNames(t)
    return Map(t, function(fighter)
        return fighter.Name
    end)
end
local function GetFightersWithHealthGreaterThan50()
    return Filter(fighters, function(fighter)
        return fighter.Health > 50
    end)
end

Example Data

local users = {
    {UserId = 123, Name = "John"},
    {UserId = 321, Name = "Jane"},
}

local fighters = {
    {Name = "Sorzo", Health = 70, Experience = 100, UserId = 123},
    {Name = "Gummy", Health = 40, Experience = 50, UserId = 321},
    {Name = "Kanzi", Health = 100, Experience = 23, UserId = 123},
    {Name = "Sorzo", Health = 100, Experience = 93, UserId = 321},
}

5 Likes

Should I move all the examples to the bottom of the post and keep the functions at the top?

  • Yes
  • No
  • Remove all examples

0 voters