Wednesday, February 3, 2021

Visual Studio Code: Disabling Code References on OS/X (a.k.a. Disabling CodeLens)

In the post Visual Studio Code: Disabling Code References on Windows (a.k.a. Disabling CodeLens) it was shown how to disable CodeLens on Visual Studio Code running on Windows.The steps required to disable CodeLens on OS/X (a Macintosh) are shown in this post.

The CodeLens feature of Visual Studio Code shows (amongst other things) the number of references to a method or function such as shown below in an excerpt from the post PowerShell: Fibonacci Interview Question




In order to disable Visual Studio Code's CodeLens on OS/X select the Code menu from Visual Studio Code and invoke Preferences | Settings:


From the Settings dialog select the Text Editor tab and scroll down to the Code Lens checkbox:


Uncheck the Code Lens checkbox. On OS/X it is necessary to close and reopen VisuaL Studio Code in order to have the CodeLens settings changes recognized.



Docker: Installing Docker on OS/X (the final step)

The site where the Docker installer for OS/X (a Mac). can be downloaded is Install Docker Desktop on Mac. After the Docker install is run on an OS/X computer Docker is not completely installed. The Docker Desktop actually needs to be run in order to finish installing Docker.

The Docker community forums site, https://forums.docker.com/, has at least one thread on this topic, Docker:command not found after installing Docker desktop on Mac:



Tuesday, February 2, 2021

PowerShell: Fibonacci Interview Question

The first Fibonacci program I wrote was on my assembly language final in 1984 (PDP-11 assembly). During a DevOps interview I was asked to write an example of a Fibonacci program so I answered the question using PowerShell. The complete source code in text form can be found at the end of this post. 

An example of a Fibonacci number being computed recursively (method Recursive) and sequentially (method Iterative):

The methods, Iterative and Recursive, are hidden. The publicly visible method, Compute, is used to compute the Fibonacci number:

The SequenceRecusrsive method is a hidden method that returns a Fibonacci sequence using a recursive computation:


The SequenceIterartive method is a hidden method that returns a Fibonacci sequence using an iterative computation:

The Sequence method is publicly visible and returns a Fibonacci sequence computed using either an iterative or recursive algorithm: 


The test functions (below) verify the Fibonacci number computed and the Fibonacci sequence computed by comparing the value returned by the iterative implementation to the value returned by the recursive implementation:


Repeatedly invoking the functions TestFibonacci and TestFibonacci (see above) for values of 0, 1, 5, and 10 tests test the iterative against recursive implementations for multiple input values.

Appendix A: Source Code

Set-StrictMode -Version 3.0

class Fibonacci  {    
    hidden static [int] Recursive([int] $n) {
        if ($n -gt 1) {
            return `
              [Fibonacci]::Recursive($n - 1) + 
              [Fibonacci]::Recursive($n - 2)
        }

        else {
            return $n
        }
    }

    hidden static [int] Iterative([int] $n) {
        if ($n -le 1) {
            return $n
        }

        [int] $nMinus2 = 0
        [int] $nMinus1 = 1
        [int] $current = 0

        for ([int] $i = 2; $i -le $n; $i++)  
        {  
            $current = $nMinus2 + $nMinus1 
            $nMinus2 = $nMinus1;  
            $nMinus1 = $current
        } 

        return $current
    }

    static [int] Compute([int] $n, [bool] $isIterative) {
        if ($isIterative) {
            return [Fibonacci]::Iterative($n)
        }

        else {
            return [Fibonacci]::Recursive($n)
        }
    }

    hidden static [int[]] SequenceRecursive([int] $n) {
        [Collections.Generic.List[int]] $sequence = `
            [Collections.Generic.List[int]]::new()

        if ($n -le 1) {
            $sequence.Add($n)

            return $sequence
        }

        for ([int] $i = 0; $i -le $n; $i++) {
            $sequence.Add([Fibonacci]::Recursive($i))
        }

        return $sequence
    }

    hidden static [int[]] SequenceIterative([int] $n) {
        [Collections.Generic.List[int]] $sequence = `
            [Collections.Generic.List[int]]::new()

        if ($n -le 1) {
            $sequence.Add($n)

            return $sequence
        }

        [int] $nMinus2 = 0
        [int] $nMinus1 = 1
        [int] $current = 0

        for ([int] $i = 2; $i -le $n; $i++)  
        {  
            $sequence.Add($nMinus2)            
            $current = $nMinus2 + $nMinus1 
            $nMinus2 = $nMinus1;  
            $nMinus1 = $current
        } 

        $sequence.Add($nMinus2)            
        $sequence.Add($current)            

        return $sequence
    }

    static [int[]] Sequence([int] $n, [bool] $isIterative) {
        if ($isIterative) {
            return [Fibonacci]::SequenceIterative($n)
        }

        else {
            return [Fibonacci]::SequenceRecursive($n)
        }
    }
}

function  TestFibonacci([int] $n) {
    [int] $iterative = [Fibonacci]::Compute($n, $true)
    [int] $recursive = [Fibonacci]::Compute($n, $false)

    if ($iterative -ne $recursive) {
        throw 
            "Fibonacci n = $n " +
            "(iterative $iterative -ne recursive $recursive)"
    }

    return $iterative
}

function TestFibonacciSequence([int] $n) {
    [string] $iterative = [Fibonacci]::Sequence($n, $true)
    [string] $recursive = [Fibonacci]::Sequence($n, $false)

    if ($iterative -ne $recursive) {
        throw 
            "Fibonacci n = $n " +
            "(iterative $iterative -ne recursive $recursive)"
    }

    return $iterative
}

TestFibonacci 0
TestFibonacciSequence 0
TestFibonacci 1
TestFibonacciSequence 1
TestFibonacci 5
TestFibonacciSequence 5
TestFibonacci 10
TestFibonacciSequence 10




Monday, February 1, 2021

Azure/Kubernetes: New-AzAksCluster contributed to Azure/azure-powershell (again) Documentation Fix

February 1, 2021 has been a productive day tweaking inconsequential issues in https://github.com/jdnark/azure-powershell. The examples in the documentation for New-AzAksCluster, https://docs.microsoft.com/en-us/powershell/module/az.aks/new-azakscluster?view=azps-5.4.0, used New-AzAks which is the legacy cmdlet. New-AzAksCluster is the cmdlet that is currently used to create a new Kubernetes cluster. New-AzAks is the alias for New-AzAksCluster so New-AzAksCluster should be used in the documentation examples: 



Azure/Kubernetes: contributed to Azure/azure-powershell

This was not the greatest contribution to the azure-powershell Git repository, https://github.com/jdnark/azure-powershell. The PR below fixes four instances of the same typo (changed SshKeyVaule to SshKeyValue) which manifests itself in the New-AzAksCluster cmdlet where a duplicate or missing SSH key is detected:





Sunday, January 31, 2021

Azure/PowerShell: Listing All Locations

Certain Azure PowerShell cmdlets require a location parameter value be specified (e.g. East Asia, West US 2, Brazil South, etc.). In order to display list of of locations the following PowerShell script can be invoked:

Get-AzureRmLocation | Select-Object -Property Location, DisplayName

The location parameter of a cmdlet should be passed the DisplayName of a given resource location. The below example demonstrates this where a new resource group is created for Norway East:

New-AzResourceGroup `
    -Name 'rg_ahha_take_on_me' `
    -Location 'Norway East' | Out-Null

Invoking the above script on January 31, 2020 listed the following locations:


Saturday, January 30, 2021

Azure: Installing Azure CLI on OS/X

Microsoft documents installing the Azure CLI on OS/X at Install Azure CLI on macOS but unfortunately following these instructions results in an error. The aforementioned documentation shows the following introduction and brew command to be used to install the AzureCLI on OS/X 


The error generated by using the above brew command to install the Azure CLI on OS/X is as follows:

This command may take a few minutes to run due to the large size of the repository.
This restriction has been made on GitHub's request because updating shallow
clones is an extremely expensive operation due to the tree layout and traffic of
Homebrew/homebrew-core and Homebrew/homebrew-cask. We don't do this for you
automatically to avoid repeatedly performing an expensive unshallow operation in
CI systems (which should instead be fixed to not use shallow clones). Sorry for
the inconvenience!

The following command (removing the brew update &) install the Azure CLI on OS/X without error:

brew install azure-cli