Featured post

c# - Usage of Server Side Controls in MVC Frame work -

i using asp.net 4.0 , mvc 2.0 web application. project requiremrnt have use server side control in application not possibl in noraml case. ideally want use adrotator control , datalist control. i saw few samples , references in codepleax mvc controllib howwver found less useful. can tell how utilize theese controls in asp.net application along mvc. note: please provide functionalities related adrotator , datalist controls not equivalent functionalities thanks in advace. mvc pages not use normal .net solution makes use of normal .net components impossible. a normal .net page use event driven solution call different methods service side mvc use actions , view completly different way handle things. also, mvc not use viewstate normal .net controlls require. found article discussing mixing of normal .net , mvc.

.net - Comparing file contents in F# -


i wrote quick , dirty function compare file contents (btw, have tested of equal size):

let eqfiles f1 f2 =   let bytes1 = seq.ofarray (file.readallbytes f1)   let bytes2 = seq.ofarray (file.readallbytes f2)   let res = seq.comparewith (fun x y -> (int x) - (int y)) bytes1 bytes2   res = 0 

i'm not happy reading whole contents array. i'd rather have lazy sequence of bytes, can't find right api in f#.

if want use full power of f#, can asynchronously. idea can asynchronously read block of specified size both files , compare blocks (using standard & simple comparison of byte arrays).

this interesting problem, because need generate asynchronous sequence (a sequence of async<t> values generated on demand, without blocking threads simple seq<t> or iteration). function read data , declaration of async sequence this:

edit posted snippet http://fssnip.net/1k has nicer f# formatting :-)

open system.io  /// represents sequence of values 't items  /// generated asynchronously on-demand type asyncseq<'t> = async<asyncseqinner<'t>>  , asyncseqinner<'t> =   | ended   | item of 't * asyncseq<'t>  /// read file 'fn' in blocks of size 'size' /// (returns on-demand asynchronous sequence) let readinblocks fn size = async {   let stream = file.openread(fn)   let buffer = array.zerocreate size    /// returns next block 'item' of async seq   let rec nextblock() = async {     let! count = stream.asyncread(buffer, 0, size)     if count > 0 return ended     else        // create buffer right size       let res =          if count = size buffer         else buffer |> seq.take count |> array.ofseq       return item(res, nextblock()) }    return! nextblock() } 

the asynchronous workflow comparison quite simple:

let rec compareblocks seq1 seq2 = async {   let! item1 = seq1   let! item2 = seq1   match item1, item2    | item(b1, ns1), item(b2, ns2) when b1 <> b2 -> return false   | item(b1, ns1), item(b2, ns2) -> return! compareblocks ns1 ns2   | ended, ended -> return true   | _ -> return failwith "size doesn't match" }  let s1 = readinblocks "f1" 1000 let s2 = readinblocks "f2" 1000 compareblocks s1 s2 

Comments

Popular posts from this blog

c# - Usage of Server Side Controls in MVC Frame work -

cocoa - Nesting arrays into NSDictionary object (Objective-C) -

ios - Very simple iPhone App crashes on UILabel settext -