« Type systems and newspapersMass Effect and Fable 2 (and Kameo) »

First deployed F# code

24/10/09

Permalink 06:01:10 pm, 192 words
Categories: Code

First deployed F# code

A one-off script deployed to a test machine, but still…

let rnd = System.Random()
let next _ = rnd.Next() |> char
let generate size = Seq.map next {0L .. size}
[<EntryPoint>]
let main args =
  match args with
  | [| size |] ->
    generate (int64 size) |> Seq.iter System.Console.Write
    0
  | _ ->
    printfn "Incorrect number of arguments"
    1

It generates more than 4 GB of random characters. I wrote the parameter error checking out of habit, although it’s unnecessary in a one-off. The program as written is pretty slow, too, so I’m sure somebody reading this can suggest something faster.

Interestingly, the guy next to me wrote the same thing yesterday in Perl. His solution looked vaguely like:

open $file or die;
for($i = 0; $i < $ARGS[0]; $i++) {
  print >>$file, "."
}

Except it was somehow longer than that…I think the file handling made it longer*. Excuse the massively incorrect Perl, I don’t remember it after 5 years of disuse. His version doesn’t generate random characters, but I think that would be easy to do in Perl. (probably char(rand()) or something?)

*I will post the code once I retrieve it from my IM history at work.

1 comment

Comment from: sandersn [Member]
I couldn't find the Perl code in my chat logs. Also during some training today I realised that if we were golfing, I could just say

let rnd = System.Random()
let main args = Seq.iter (fun _ -> Console.Write (char (rnd.Next())) {0L...int64 args.[0]}
26/10/09 @ 20:58

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)
powered by b2evolution free blog software