Skip to content

Latest commit

 

History

History
118 lines (78 loc) · 2.98 KB

2019-01.md

File metadata and controls

118 lines (78 loc) · 2.98 KB

2019

F# fun time.

use lambda function

Tue. 15 Jan 2019

if you need a elablorate control of the passed argument

let isIsogram (str: string) = 
    let rec count (c: char) (cs: char list) =
        match c with
        | ' ' | '-' -> 1
        | _ ->  match cs with
                | [] -> 0
                | x::xs ->  if (x = c) then 1 + count c xs
                            else count c xs
    
    str.ToLower() 
    |> Seq.toList
    |> fun xs -> List.forall (fun elem -> (count elem xs) = 1) xs

loop by yield! and get nth item from list

Mon. 14 Jan 2019

type Robot(name) =
    do  
        Robot.ExistingNames = Robot.ExistingNames @ [name] |> ignore

    static member ExistingNames:List<string> = []
    member _this.Name = name


let mkRobot() = 
    let rand = new Random()
    let rec generateName() : string =
        let name =
            [
                yield! [1..2] |> List.map (fun _ -> ['A'..'Z'].[rand.Next(['A'..'Z'].Length)])
                yield! [1..3] |> List.map (fun _ -> ['0'..'9'].[rand.Next(['0'..'9'].Length)])
            ]
            |> String.Concat
        
        if List.contains name Robot.ExistingNames then generateName()
        else name

    new Robot(generateName())

Iterate a list in F# with recursion

Fri. 11 Jan 2019

Why? Because it's coole and functional programming is all about recursion! Jokingly I said, but it seems so.

let distance (strand1: string) (strand2: string): int option = 
    let compareChar (c1: char) (c2: char): int = 
        if c1 = c2 then 0
        else 1
    
    let rec compare ((cs1: char list), (cs2: char list)): int =
        match cs1, cs2 with
        | (x::xs, y::ys) -> (compareChar x y) + (compare (xs, ys))
        | _, _ -> 0

    if String.IsNullOrEmpty(strand1) && String.IsNullOrEmpty(strand2) then Some(0)
    else if strand1.Length <> strand2.Length then None
    else Some(compare (Seq.toList(strand1), Seq.toList(strand2)))

Unfortunately, you have to use .NET String.IsNullOrEmpty to check if the string is null or empty. I expected an elegant F# way, but it didn't exist.

To restart F# Interactive

Right click in the F# interactive window, and select Reset Interactive Session.

Download a blob from azure storage

Tue. 8 Jan 2019

Add a connection string to the storage

<connectionStrings>
  <add name="storage" connectionString="UseDevelopmentStorage=true" />
</connectionStrings>

Use container to download the blob

var connectionString = "conn string";
var containerName = "container";
var blobName = "blobName";

CloudStorageAccount.TryParse(connectionString, out CloudStorageAccount account)
var client = account.CreateCloudBlobClient();

var container = client.GetContainerReference(containerName.ToLower());
await container.CreateIfNotExistsAsync(BlobContainerPublicAccessType.Off, null, null, cancellationToken);

var blob = await container.GetBlobReferenceFromServerAsync(blobName);
var stream = await blob.OpenReadAsync();