Home

F# Lists: Filtering Some Items

   

Description

A sub-list is a list derived from an existing list. To create a sub-list, you have many options.

To select some items from a list, you can apply a criterion that would filter some items, put them in a new list, and return that list. To support this operation, the List module provides a function named filter. Its signature is:

List.filter : ('T -> bool) -> 'T list -> 'T list

This function takes a function and a list as arguments. The internal function applies a condition to each item of the list. Every item that responds to the condition is put in a new list. At the end, that list is returned from the List.filter() function. Here is an example:

open System
open System.Windows.Forms

type OccupancyStatus =
| Other
| Available
| Occupied
| NeedsRepair

type Apartment = {
    UnitNumber      : string
    Bedrooms        : int
    Bathrooms       : float
    SecurityDeposit : int
    MonthlyRate     : int
    Status          : OccupancyStatus }

let apartments = [
    { UnitNumber = "101"; Bedrooms = 2; Bathrooms = 2.00; SecurityDeposit = 650; MonthlyRate = 1150; Status = OccupancyStatus.Available   }
    { UnitNumber = "102"; Bedrooms = 1; Bathrooms = 1.00; SecurityDeposit = 500; MonthlyRate =  950; Status = OccupancyStatus.NeedsRepair }
    { UnitNumber = "103"; Bedrooms = 1; Bathrooms = 1.00; SecurityDeposit = 500; MonthlyRate =  925; Status = OccupancyStatus.Available   }
    { UnitNumber = "104"; Bedrooms = 3; Bathrooms = 2.00; SecurityDeposit = 850; MonthlyRate = 1350; Status = OccupancyStatus.Occupied    }
    { UnitNumber = "105"; Bedrooms = 2; Bathrooms = 1.00; SecurityDeposit = 550; MonthlyRate = 1150; Status = OccupancyStatus.Available   }
    { UnitNumber = "106"; Bedrooms = 3; Bathrooms = 1.50; SecurityDeposit = 950; MonthlyRate = 1425; Status = OccupancyStatus.NeedsRepair }
    { UnitNumber = "107"; Bedrooms = 1; Bathrooms = 1.00; SecurityDeposit = 500; MonthlyRate =  950; Status = OccupancyStatus.Occupied    }
    { UnitNumber = "108"; Bedrooms = 2; Bathrooms = 1.00; SecurityDeposit = 550; MonthlyRate = 1150; Status = OccupancyStatus.Occupied    }
 ]

let ready = List.filter (fun (apart : Apartment) -> apart.Status = OccupancyStatus.Available) apartments

let apartmentsListing = new Form(Width = 335, Height = 150, Text = "Apartments Listing", MaximizeBox = false)

let lbxApartments = new ListBox(Left = 12, Top = 12, Width = 300, Height = 98)

lbxApartments.Items.Add("\t\t\tMonthly") |> ignore
lbxApartments.Items.Add("Unit # Beds\tBaths\tRent\tDeposit\tStatus") |> ignore
lbxApartments.Items.Add("----------------------------------------------------------------------------------------------") |> ignore
for apart in ready do
    lbxApartments.Items.Add(sprintf "%s\t%i\t%.2f\t%d\t%d\t%A" apart.UnitNumber apart.Bedrooms apart.Bathrooms apart.MonthlyRate apart.SecurityDeposit apart.Status) |> ignore
lbxApartments.Items.Add("----------------------------------------------------------------------------------------------") |> ignore

apartmentsListing.Controls.Add lbxApartments

do Application.Run apartmentsListing

This would produce:

Filtering by Some Items

 

     
     
 

Home Copyright © 2009-2015, FunctionX Home