Home

F# Collections: Arrays - Getting a Reversed Array

   

Introduction

Reversing an array consists of changing the order of its value. The reverse can be done using the rev() method of the Array class. Its signature is:

Array.rev : 'T [] -> 'T []

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 reversedOrder = Array.rev apartments
     
     
 

Home Copyright © 2012-2015, FunctionX Home