Home

F# Arrays: Creating a Sub-Array

   

Description

A sub-array is an array extracted from values of an existing array. To let you create a sub-array, the Array class provides the sub() method. Its signature is:

Array.sub : 'T [] -> int -> int -> 'T []

This method takes three arguments. The first is the array that holds the values. The second and the third arguments specify the range to consider in the array. The method returns a new array. Here is an example:

type OccupancyStatus =
| Other       = 0
| Available   = 1
| Occupied    = 2
| NeedsRepair = 3
| NotReady    = 4

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 = 2.00; SecurityDeposit = 1350; MonthlyRate =  850; Status = OccupancyStatus.Available   }
    { UnitNumber = "107"; Bedrooms = 3; Bathrooms = 2.00; SecurityDeposit = 1285; MonthlyRate =  850; Status = OccupancyStatus.NotReady    }
    { UnitNumber = "108"; Bedrooms = 1; Bathrooms = 1.00; SecurityDeposit =  885; MonthlyRate =  500; Status = OccupancyStatus.Available   }
    { UnitNumber = "109"; Bedrooms = 2; Bathrooms = 2.00; SecurityDeposit = 1150; MonthlyRate =  650; Status = OccupancyStatus.Available   }
    { UnitNumber = "110"; Bedrooms = 1; Bathrooms = 1.00; SecurityDeposit =  895; MonthlyRate =  500; Status = OccupancyStatus.Available   }
 |]

let units = Array.sub apartments 2 5

This would produce apartments from 103 to 107.

     
     

Home Copyright © 2012-2015, FunctionX Home