Tuesday, June 28, 2011

Parallel Data Fetching by F#



namespace JQD.Parallel
open System
open System.Collections.Generic
type DataFetcher() =
let f (hhr) =Reporting.HoldingMapper.MapHolding(hhr)
let f2 (ar:Reporting.Domain.Objects.GetHoldingsHierarchyResult[]) =
ar|> Array.Parallel.map (fun (g) -> Reporting.HoldingMapper.MapHolding(g))
|> Array.Parallel.map (fun h -> h.MarketValue )
|> Array.sum

member this.DA(ar:Reporting.Domain.Objects.GetHoldingsHierarchyResult[]) =
ar|> Array.Parallel.map (fun (g) -> Reporting.HoldingMapper.MapHolding(g))
|> Array.Parallel.map (fun h -> h.MarketValue )
|> Array.sum

member this.DA3(ar:DateTime[],groupId:int) =
ar|> Array.Parallel.map( fun i -> Seq.toArray(Reporting.Dal.GetHoldingsHierarchy(groupId,i,i)))
|> Array.Parallel.map(fun g -> f2(g))

Wednesday, June 15, 2011

WPF DataGrid Row/Cell Accessor


public static T GetVisualChild<T>(Visual parent) where T : Visual
{
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T;
if (child == null)
{
child = GetVisualChild<T>(v);
}
if (child != null)
{
break;
}
}
return child;
}

public static DataGridRow GetSelectedRow( DataGrid grid)
{
return (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem);
}

public static DataGridRow GetRow( DataGrid grid, int index)
{
DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
if (row == null)
{
// May be virtualized, bring into view and try again.
grid.UpdateLayout();
grid.ScrollIntoView(grid.Items[index]);
row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
}
return row;
}

public static DataGridCell GetCell( DataGrid grid, DataGridRow row, int column)
{
if (row != null)
{
DataGridCellsPresenter presenter = GetVisualChild(row);

if (presenter == null)
{
grid.ScrollIntoView(row, grid.Columns[column]);
presenter = GetVisualChild<DataGridCellsPresenter>(row);
}

DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
return cell;
}
return null;
}

public static DataGridCell GetCell( DataGrid grid, int row, int column)
{
DataGridRow rowContainer = GetRow(grid,row);
return GetCell(grid,rowContainer, column);
}

Tuesday, June 14, 2011

F# Active Parttern to break down data



// break down into tuple
open System.Drawing
let (|RGB|) (col:System.Drawing.Color) = (col.R, col.G, col.B)
let (|HSB|) (col:System.Drawing.Color) = (col.GetHue(), col.GetSaturation(),col.GetBrightness())

//parsing
open System
let (|Int|_|)(x:string) =
let mutable y =0.0
if System.Double.TryParse(x, &y) then Some(x)
else None

let PrintRGB x =
match x with
| RGB (r,g,b) -> printfn"%d %d %d" r g b

let Parse x =
match x with
| Int t -> printfn "%A" t
| _ -> printfn "not integer"


Sunday, June 12, 2011

F# Discriminated Union as Option Type



type Options<'a> =
| Some of 'a
| None

let op1 = Some(10.0)
let op2 = None
let op3 = Options.Some("adf")

let ShowOption x =
match x with
| None ->printfn "%A" "Nothing"
| Some t ->printfn "%A" t

ShowOption op1
ShowOption op2
ShowOption op3

Saturday, June 11, 2011

F# Finding Prime Number



let IsNotMultipleOf n x =
x=n || x%n<>0

let rec RemoveAllMultiples listn listx =
match listn with
| head::tail -> RemoveAllMultiples tail (List.filter (IsNotMultipleOf head) listx)
| [] -> listx

let GetPrimeTo n =
let r= (int) ( sqrt(float n))
RemoveAllMultiples [2..r] [1..100]

printfn "%A" (GetPrimeTo 100)

Wednesday, June 8, 2011

F# Async Workflow


#light
open System.IO
open System.Net
open System

let AyncHttp(url:string) =
async {
let req=WebRequest.Create(url)
let! rsp=req.AsyncGetResponse()
use s= rsp.GetResponseStream()
use r= new System.IO.StreamReader(s)
r.ReadToEnd()
}

//let SyncHttp(url:string) =
// let req=WebRequest.Create(url)
// let rsp=req.GetResponse()
// use s= rsp.GetResponseStream()
// use r= new System.IO.StreamReader(s)
// r.ReadToEnd()



let dt=System.DateTime.Now

//SyncHttp "http://maps.google.com"
//SyncHttp "http://maps.live.com"
//SyncHttp("http://www.nhl.com");
//SyncHttp("http://www.nfl.com");
//SyncHttp("http://www.china.com");
//SyncHttp("http://www.sina.com");
//SyncHttp("http://www.nba.com");
//printf "%A" (DateTime.Now-dt).Milliseconds

Async.Parallel [
AyncHttp("http://maps.google.com");
AyncHttp("http://maps.live.com");
AyncHttp("http://www.nhl.com");
AyncHttp("http://www.nfl.com");
AyncHttp("http://www.china.com");
AyncHttp("http://www.sina.com");
AyncHttp("http://www.nba.com");
]
|> Async.RunSynchronously

printf "%A" (DateTime.Now-dt).Milliseconds