Home

F# Arrays: Iterating Through an Array

   

Introduction

To support the ability to  execute a function on each member of an array, the Array class provides a method named iter. Its signature is:

Array.iter : ('T -> unit) -> 'T [] -> unit

This method takes two arguments. The first argument is a function that will be applied to each member of the array. The second argument is the array that holds the values. Here is an example:

open System
open System.Drawing
open System.Windows.Forms 

type Customer = {
    AccountNumber : string
    FirstName     : string
    LastName      : string
    (*Address       : string
    City          : string
    State         : string*) }

and GasBill(billNbr, client : Customer, meterReading) =
    let bNbr = ref billNbr
    let cust = ref client
    let (rStart, rEnd) = meterReading

    member this.InvoiceNumber with get() = !bNbr and set(nbr) = bNbr := nbr
    member this.Customer with get() = !cust and set(nbr) = cust := nbr
    member this.MeterReadingStart with get() = rStart
    member this.MeterReadingEnd with get() = rEnd
    member this.Usage with get() = rEnd - rStart
    member this.TotalCCF with get() = this.Usage * 1.012
    member this.TotalTherms with get() = this.TotalCCF * 1.034

    member this.ServiceCharge with get() = 5.85
    member this.DistributionCharge with get() =
                                    let first100Therms = ref 0.00
                                    let above100Therms = ref 0.00

                                    if this.TotalTherms <= 100.00 then
                                        first100Therms := this.TotalTherms * 0.351869
                                        above100Therms := 0.00
                                    else
                                        first100Therms := 100.00 * 0.3518693
                                        above100Therms := (this.TotalTherms - 100.00) * 0.292748
                                    !first100Therms + !above100Therms
    member this.TotalDelivery with get() = this.ServiceCharge + this.DistributionCharge
                                    
    member this.MunicipalTax with get() = this.DistributionCharge * 0.86 / 100.00
    member this.StateTax with get() = this.DistributionCharge * 0.258 / 100.00
    member this.TotalInvoice with get() = this.TotalDelivery + this.MunicipalTax + this.StateTax
    new() = GasBill(0, { AccountNumber = ""; FirstName = ""; LastName = "" }, (0.00, 0.00))

let customers : Customer array = Array.zeroCreate 5

customers.[0] <- { AccountNumber = "807495"; FirstName = "Julius"; LastName = "Gluck" }
customers.[1] <- { AccountNumber = "928037"; FirstName = "Stephanie"; LastName = "Dugan" }
customers.[2] <- { AccountNumber = "279714"; FirstName = "William"; LastName = "Bamberg" }
customers.[3] <- { AccountNumber = "680513"; FirstName = "Kelly"; LastName = "Stewart" }
customers.[4] <- { AccountNumber = "479242"; FirstName = "Mark"; LastName = "Goddard" }

let mutable bills = [| new GasBill(); new GasBill(); new GasBill(); new GasBill(); new GasBill(); new GasBill(); new GasBill() |];

Array.set bills 0 (new GasBill(100001, customers.[1], ( 6527.00,  6739.00)))
Array.set bills 1 (new GasBill(100002, customers.[3], (12582.00, 12846.00)))
Array.set bills 2 (new GasBill(100003, customers.[4], ( 3019.00,  3102.00)))
Array.set bills 3 (new GasBill(100004, customers.[2], (27471.00, 27829.00)))
Array.set bills 4 (new GasBill(100005, customers.[0], (48731.00, 48825.00)))
Array.set bills 5 (new GasBill(100006, customers.[1], ( 6739.00,  6997.00)))
Array.set bills 6 (new GasBill(100007, customers.[4], ( 3102.00,  3218.00)))

Array.append bills [| new GasBill(100008, customers.[3], (12846.00, 128921.00)) |] |> ignore
Array.append bills [| new GasBill(100009, customers.[2], (27829.00, 28229.00)) |] |> ignore

let gasUtilityCompany : Form = new Form(MaximizeBox = false, Text = "Gas Utility company - Customers Invoices", ClientSize = new System.Drawing.Size(400, 165), StartPosition = FormStartPosition.CenterScreen)

let lvwInvoices : ListView = new ListView(GridLines = true, View = View.Details, FullRowSelect = true, Location = new Point(24, 21), Size = new System.Drawing.Size(355, 124))

let mutable colInvoice = lvwInvoices.Columns.Add("Invoice #", 60)
colInvoice <- lvwInvoices.Columns.Add("Customer", 65, HorizontalAlignment.Center)
colInvoice <- lvwInvoices.Columns.Add("Usage", 45, HorizontalAlignment.Right)
colInvoice <- lvwInvoices.Columns.Add("Delivery", 55, HorizontalAlignment.Right)
colInvoice <- lvwInvoices.Columns.Add("Taxes", 50, HorizontalAlignment.Right)
colInvoice <- lvwInvoices.Columns.Add("Amt Owed", 75, HorizontalAlignment.Right)

Array.iter (fun (bill : GasBill) ->
    let lviInvoice = new ListViewItem(sprintf "%i" bill.InvoiceNumber)
    lviInvoice.SubItems.Add(sprintf "%s" bill.Customer.AccountNumber) |> ignore
    lviInvoice.SubItems.Add(sprintf "%0.0f" bill.TotalTherms) |> ignore
    lviInvoice.SubItems.Add(sprintf "%0.02f" bill.TotalDelivery) |> ignore
    lviInvoice.SubItems.Add(sprintf "%0.02f" (bill.MunicipalTax + bill.StateTax)) |> ignore
    lviInvoice.SubItems.Add(sprintf "%0.02f" bill.TotalInvoice) |> ignore
    lvwInvoices.Items.Add lviInvoice |> ignore) bills
gasUtilityCompany.Controls.Add lvwInvoices

[<EntryPoint>]
let main argv =
    Application.Run gasUtilityCompany

This would produce:

Iterating Through an Array

     
     

Home Copyright © 2012-2015, FunctionX Home