Home

F# Collections: Picking an Element from an Array

   

Description

The Array module provides a function that can be used to apply a condition to the elements from the first item to the last. The function returns the first item that responds true to the condition. The function is named pick and its signature is:

Array.pick : ('T -> 'U option) -> 'T [] -> 'U

The condition is created as a function in the first argument of the function. That condition is created as a matching option. The second argument is the array that holds the values. Here is an example of calling the function:

type Employee = {
    EmployeeNumber : string
    FirstName      : string
    LastName       : string
    HourlySalary   : double }

let employees = [| { EmployeeNumber = "8022-3840"; FirstName = "Mark";  LastName = "Plant";  HourlySalary = 24.55 };
                   { EmployeeNumber = "5840-2497"; FirstName = "Carol"; LastName = "Mylans"; HourlySalary = 16.25 };
                   { EmployeeNumber = "2081-7008"; FirstName = "Maria"; LastName = "Pappas"; HourlySalary = 22.75 }; |]

type PayrollPreparation(number, times) =
    let mutable nbr = number;
    let mutable tms = times;
    member this.EmployeeeNumber with get() = nbr and set(value) = nbr <- value;
    member this.TimesWorked with get() = tms and set(value) = tms <- value;
    member this.PaySummary() : float array =
        let mutable regularTime = 0.00
        let mutable overtime = 0.00
        let mutable regularAmount = 0.00
        let mutable overtimeSalary = 0.00
        let mutable totalWeekTime = 0.00
        let mutable overtimeAmount = 0.00
        
        let empl = Array.pick (fun (staff : Employee) ->
            match staff.EmployeeNumber with
            | nbr -> Some staff
            | _ -> None) employees

        for t in tms do totalWeekTime <- totalWeekTime + t
        overtimeSalary <- empl.HourlySalary * 1.50

        // If the employee worked under 40 hours, there is no overtime
        if totalWeekTime < 40. then
            regularTime <- totalWeekTime
            regularAmount <- empl.HourlySalary * regularTime
            //overtime <- 0.00
            //overtimeAmount <- 0.00
            [| regularTime; 0.00; regularAmount; 0.00; regularAmount |]
        // If the employee worked over 40 hours, calculate the overtime
        else //if totalWeekTime >= 40. then
            regularTime <- 40.
            regularAmount <- empl.HourlySalary * 40.
            overtime <- totalWeekTime - 40.
            overtimeAmount <- overtime * overtimeSalary
            [| 40.; overtime; regularAmount; overtimeAmount; regularAmount + overtimeAmount |]

//let pp = PayrollPreparation("5840-2497", [| 6.0; 7.5; 8.0; 6.5; 7.5; 0.0; 0.0 |]);
let pp = PayrollPreparation("8022-3840", [| 8.50; 9.50; 10.00; 8.50; 9.50; 0.0; 0.0 |]);

let empl = Array.pick (fun (staff : Employee) ->
    match staff.EmployeeNumber with
    | "8022-3840" -> Some staff
    | _ -> None) employees
     
     

Home Copyright © 2012-2015, FunctionX Home