It was with great trepidation that I wrote this:

proc serve_directory(k:socket_t, fname:string)
{
  var dirname = Filename::basename fname;
  fun rf(f:string)=>'  <a href="/$'+ fname + '/' +f+'">'+f+'</a>';
  var eof_flag = false;
  val top = "A DIRECTORY " + fname + "\r\n";
  val flist = 
    match FileSystem::filesin fname with
    | Some ?files =>
      let ?aux = 
          fun (ls2:list[string] * list[string]) (f:string) =>
          match ls2 with | ?ds,?rs => match FileSystem::filetype 
(Filename::join (fname,f)) with
            | DIRECTORY => Cons (f,ds), rs
            | REGULAR => ds, Cons (f,rs)
            | _ => ls2
            endmatch
          endmatch
      in
      let ?dirs,?regs = fold_left aux (Empty[string], Empty[string]) files in
        "<pre>"+ 
        '  <a href="/"><em>home</em></a>\r\n'+
        ' Directories: \r\n' +
        (fold_left (fun (acc: string) (f:string) => 
          match f with 
          | "." => acc 
          | ".." => acc
          | _ => acc + rf f + "\r\n" endmatch) 
           "" dirs
        ) +
        ' Files: \r\n' +
        (fold_left (fun (acc: string) (f:string) => 
           acc + rf f + "\r\n" ) 
           "" regs 
        ) +
        "</pre>"
    | None => "ERROR ACCESSING DIRECTORY"
    endmatch
  ;
  val page = make_html(top + flist);
  write_string(k,page,&eof_flag);
}

This code generates the HTML for the webserver to display a directory,
splitting files into directories and regular files now. The splitup code
is one huge expression, heavy functional stuff, including tail recursion
and lets, matches etc.

I'm a marginally disappointed that the "aux" function wasn't inlined,
Felix used a double closure here.. everything else was completely flattened
into straight line code.


--
john skaller
skal...@users.sourceforge.net





------------------------------------------------------------------------------
The Next 800 Companies to Lead America's Growth: New Video Whitepaper
David G. Thomson, author of the best-selling book "Blueprint to a 
Billion" shares his insights and actions to help propel your 
business during the next growth cycle. Listen Now!
http://p.sf.net/sfu/SAP-dev2dev
_______________________________________________
Felix-language mailing list
Felix-language@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/felix-language

Reply via email to