Ron and Ella Wiki Page

Extremely Serious

Page 21 of 40

Functional Programming with Gosu

First Class Citizen

An entity that can be passed around as an argument, returned from a function, modified and assigned to a variable.

First Class Function

A function that is treated as first class citizen.

Higher-Order Functions (HOF)

A function which takes function as an argument and/or that return a function.

Closure

A function that remembers its lexical scope even when the function is executed outside that lexical scope.

function greeterFn() : block() {
  var name="World" //name is a local variable created by init
  var greet = \-> print("Hello ${name}") //greet is an inner function 
                                         //that uses the variable declared 
                                         //in parent function.
  return greet
}

var greeter = greeterFn()
greeter()

Currying

The process of converting a function that takes multiple arguments into a function that takes them one at a time.

var sum = \ a : int, b : int -> a + b
print(sum(1,2))

var curriedSum = \ a : int -> \ b : int -> a + b
print(curriedSum(1)(2))

Function Composition

The act of putting two functions together to form a third function where the output of one function is the input of the other.

uses java.lang.Integer
uses java.lang.Double
uses java.lang.Math

var compose = \ output : block(out : Integer) : String, func : block(param: Double) : Integer -> \ arg : Double -> func(output(arg)) //Definition

var floorToString = compose(\ out -> out.toString(), \ param -> Math.floor(param)) //Usage

print(floorToString(121.212121))

Continuation

The part of the code that's yet to be executed.

uses java.lang.Double

var printAsString = \ num : Double -> print("Given ${num}")

var addOneAndContinue = \ num: Double, cc : block(___num : Double) -> {
  var result = num + 1
  cc(result)
}

addOneAndContinue(2, printAsString)

Purity

A function is pure if the return value is only determined by its input values, and does not produce side effects.

var greet = \ name: String -> print("Hello ${name}")
greet("World")

The following is not pure since it modifies state outside of the function:

var greeting : String
var greet = \ name: String -> {greeting ="Hello ${name}"}
greet("World")
print(greeting)

Side Effects

A function or expression is said to have a side if apart from returning a value, it interacts with (reads from or writes to) external mutable state.

var currentDate = java.util.Date.CurrentDate //Retrieves the date from the system.
print(currentDate)

gw.api.util.Logger.forCategory("side-effect").info('IO is a side effect!')

Idempotent

A function is idempotent if reapplying it to its result does not produce a different result.

print(java.lang.Math.abs(java.lang.Math.abs(10)))

Point-Free Style (Tacit Programming)

Write functions where the definition does not explicitly identify the arguments used. This style usually requires currying or other higher order functions.

uses java.lang.Integer

// Given
var map = \ fn : block(item : int) : int -> \ list : List<Integer> -> list.map<Integer>(\ item -> fn(item))
var add = \ a : int -> \ b : int -> a + b
var nums : List<Integer> = (0..5).toList()

// Not points-free - 'numbers' is an explicit argument
var incrementAll = \ numbers : List<Integer> -> map(add(1))(numbers)

print(incrementAll(nums))

// Points-free - The list is an implicit argument
var incrementAll2 = map(add(1))

print(incrementAll2(nums))

Predicate

A function that returns true or false for a given value.

var predicate = \ a : int -> a > 2

print((1..4).where(\ a -> predicate(a)))

Lambda

An anonymous function that can be treated like a value.

(\ a : int -> a + 1)(1)

Lambda can be assigned to a variable

var add1 = \ a : int -> a + 1
print(add1(1))

Reference

https://github.com/hemanth/functional-programming-jargon
https://en.wiktionary.org/wiki/second-class_object#English
https://en.wiktionary.org/wiki/third-class_object#English

Gosu Collection Enhancements Functions

flatMap

Maps each element of the Collection to a Collection of values and then flattens them into a single List.

Syntax

flatMap<R>(mapper(item : Collection<?>) : Collection<R>) : List<R>

Example

var items = {{1},{2,2},{3,3,3},{4,4,4,4}}
print(items.flatMap(\ ___item -> ___item))
Output
[1, 2, 2, 3, 3, 3, 4, 4, 4, 4]

fold

Accumulates the values of an Collection into a single T.

Syntax

fold(aggregator(aggregate : T, item : T) : T) : T

Example

var items = {{1},{2,2},{3,3,3},{4,4,4,4}}
var flatItems = items.flatMap(\ ___item -> ___item)
print(flatItems.fold(\ ___aggr, ___item -> ___aggr + ___item))
Output
30

intersect

Returns a Set that is the intersection of the two Collection objects.

Syntax

intersect(that: Collection<T>) : Set<T>

Example

var items1 = {1,2,3,4,5,6}
var items2 = {4,5,6,7,8,9}
print(items1.intersect(items2))
Output
[4, 5, 6]

map

Returns a List of each element of the Collection mapped to a new value.

Syntax

map<Q>(mapper(item : <INPUT>) : <Q>) : List<Q>

Example

var numbers = {1,2,3}
var words = {"one","two","three"}
print(numbers.map(\ ___number -> words[___number-1]))
Output
[one, two, three]

partition

Partitions this Collection into a Map of keys to a list of elements in this Collection.

Syntax

partition<Q>(partitioner(item : R) : Q) : Map<Q, List<R>>

Example

var numbers = {1,2,3,4,5,6,8}
print(numbers.partition<String>(\ ___number -> ___number % 2 == 0 ? "even" : "odd"))
Output
{even=[2, 4, 6, 8], odd=[1, 3, 5]}

reduce

Accumulates the values of a Collection into a single V given an initial seed value

Syntax

reduce<T>(init : T, aggregrator(aggregate: T, item : ?) : T) : T

Example

var numbers = {1,2,3,4,5,6,8}
var sumOfNumbers = numbers.reduce(0, \ ___aggr, ___number -> ___aggr + ___number)
print(sumOfNumbers)
Output
29

union

Returns a new Set that is the union of the two Collections

Syntax

union(that : Collection<T>) : Set<T>

Example

var items1 = {1,2,3,4,5,6}
var items2 = {4,5,6,7,8,9}
print(items1.union(items2))
Output
[1, 2, 3, 4, 5, 6, 7, 8, 9]

disjunction

Returns a new Set that is the set disjunction of this collection and the other collection

Syntax

disjunction(that : Collection<T>) : Set<T>

Example

var items1 = {1,2,3,4,5,6}
var items2 = {4,5,6,7,8,9}
print(items1.disjunction(items2))
Output
[1, 2, 3, 7, 8, 9]

join

Joins all elements together as a string with a delimiter

Syntax

join(delim : String) : String

Example

var words = {"Hello", "World"}
print(words.join(" "))
Output
Hello World

Maven Scope to Gradle Configuration Mapping

Maven Gradle Comment
compile api if the dependency should be exposed to consumers.
implementation if the dependency should not be exposed to consumers.
provided compileOnly Maven provided is also available at runtime. Gradle compileOnly is limited to compile only.
runtime runtimeOnly
test testImplementation

IPv6 Unicast Address Types

Global Unicast Address

This address has a global scope with the same purpose as IPv4 public address.

Link Local Address

This address has a local scope and cannot be used outside the link (i.e. network segment or broadcast address) and non routable. Normally has the following prefix:

FE80::/10

Loopback Address

This address corresponds to the software loopback interface of the network card and doesn't necessarily requires hardware associated with it. Normally has the following address:

::1/128

Unspecified Address

This address indicates an absence of address. This is represented by the following:

::/128

Unique Local Address

This is analogous to IPv4 private networking and has the following range:

FC00::/7
FD00::/8

Assigning an IP to an Interface of a Cisco Router

Pre-requisite

  • Putty application

Displaying the Interfaces

Use the following command to display the available interfaces and their states:

show ip interface brief

Assigning an IP

  1. Connect to cisco console using putty.

  2. Press the enter key to enter into user mode.

  3. Execute the following command to enter into privilege mode:

    enable
  4. Load the startup-config into the running-config using the following command:

    copy startup-config running-config
  5. Execute the following command to enter into the configuration mode:

    config terminal
  6. Configure an interface using the following syntax:

    interface <INTERFACE_NAME> 

    Example

    interface GigabitEthernet0/0
  7. Assign an IP address using the following syntax:

    ip address <IP_ADDRESS> <SUBNET_MASK>

    Example

    ip address 10.0.0.210 255.255.255.0
  8. Turn on the interface using the following command:

    no shutdown
  9. Exit the interface configuration using the following command:

    exit
  10. Exit the configuration mode:

    exit

    After this you can display the interfaces and see the state of the interface just configured

  11. Save the update on the running-config to the startup-config file using the following command:

    copy running-config startup-config

Synchronizing Logging in Cisco Router

To always have a readable command line on cisco console, aux and/or virtual terminals it is recommended to synchronize the logging.

Pre-requisite

  • Putty application

Synchronizing the Logging

  1. Connect to cisco console using putty.

  2. Press the enter key to enter into user mode.

  3. Execute the following command to enter into privilege mode:

    enable
  4. Load the startup-config into the running-config using the following command:

    copy startup-config running-config
  5. Execute the following command to enter into the configuration mode:

    config terminal
  6. Synchronize the logging on console using the following commands:

    line con 0
    logging sync
  7. Synchronize the logging on AUX using the following command:

    line aux 0
    logging sync
  8. (Optional) Synchronize the logging on 5 virtual terminals using the following command:

    line vty 0 4
    logging sync

    Only do this if you are using virtual terminals, specially with SSH connections.

  9. Exit the virtual terminal configuration using the following command:

    exit
  10. Exit the configuration mode:

    exit
  11. Save the update on the running-config to the startup-config file using the following command:

    copy running-config startup-config

Enable SSH on a Cisco Router

Pre-requisite

  • Putty application

Enabling SSH

  1. Connect to cisco console using putty.

  2. Press the enter key to enter into user mode.

  3. Execute the following command to enter into privilege mode:

    enable
  4. Load the startup-config into the running-config using the following command:

    copy startup-config running-config
  5. Execute the following command to enter into the configuration mode:

    config terminal
  6. Change the hostname using the following syntax:

    hostname 
  7. Change the domain name using the following syntax:

    ip domain-name 
  8. Generate the SSH keys using the following command:

    crypto key generate rsa general-keys
  9. On How many bits in the modules [512]:, type in 2048 and press the enter key.

  10. Enable SSH version 2 using the following command:

    ip ssh version 2
  11. Create an SSH credential using the following syntax:

    username  secret 
  12. Create 5 (i.e. vty 0 to 4) virtual terminals for SSH connections, using the following command.

    line vty 0 4
  13. Allow only SSH to the newly created virtual connection using the following command:

    transport input ssh
  14. Use only the local database for credentials using the following command:

    login local
  15. Exit the virtual terminal configuration:

    exit
  16. Exit the configuration mode:

    exit
  17. Save the update on the running-config to the startup-config file using the following command:

    copy running-config startup-config
  18. Using putty, connect on any known IP of the router via SSH using the credentials you made from step 11.

Simple Java Keystore Management

Importing a certificate to a keystore

keytool -importcert -alias <ALIAS> -v -keystore <KEYSTORE_FILE> -file <INPUT_FILE> -storepass <KEYSTORE_PASSWORD>

Listing the certificates from a keystore

keytool -list -v -keystore <KEYSTORE_FILE> -storepass <KEYSTORE_PASSWORD>

Include the -a <ALIAS> parameter to just display a single certificate

Delete a certificate from a keystore

keytool -delete -v -alias <ALIAS> -keystore <KEYSTORE_FILE> -storepass <KEYSTORE_PASSWORD>

Deleting an Entry from a Java Keystore

Use the following command to delete an entry of a Java keystore:

The keytool is normally found in $JAVA_HOME/jre/bin (i.e. the $JAVA_HOME variable is where you’ve installed JDK).

keytool -delete -v -alias <ALIAS> -keystore <KEYSTORE_FILE> -storepass <KEYSTORE_PASSWORD>
Token Description
ALIAS The alias used upon importing the certificate.
KEYSTORE_FILE The target key store file (e.g. cacerts found in $JAVA_HOME/jre/lib/security)
KEYSTORE_PASSWORD The password for accessing the keystore (i.e. the default is changeit)

Override the Forgotten Password on a Cisco Router

Pre-requisite

  • Putty application

Overriding the Forgotten Password

  1. Connect to cisco console using putty.

  2. Press the enter key to enter into user mode.

  3. Once in the user mode (i.e. the prompt with greater than sign >), reboot the cisco router (i.e. using the physical switch of the router).

  4. Going back to your putty terminal, once you see any text on it, issue the break command.

    1. Point and click your mouse to the putty icon on putty title bar.

    2. Select Special Command.

    3. Select Break.

      Expect the see the rom monitor prompt as follows:

      rommon 1 >

  5. Execute the following command to disable the startup-config:

    confreg 0x2142
  6. Reset cisco router using the following command:

    reset
  7. Expect to see the following question:

    Would you like to enter the initialization configuration dialog? [yes/no]
  8. Type in no for the answer and press the enter key.

  9. Press the enter key one more time to enter into user mode.

  10. Execute the following command to enter into privilege mode:

    enable
  11. Load the startup-config into the running-config using the following command:

    copy startup-config running-config
  12. On Destination filename [running-config]?, press the enter key.

  13. Execute the following command to enter into the configuration mode:

    config terminal
  14. Change the password using the following syntax:

    enable secret <PASSWORD>

    Example of setting cisco as the password:

    enable secret cisco
  15. Execute the following command to enable the startup-config:

    config-register 0x2102
  16. Exit from the configuration mode using the following command:

    exit
  17. Save the update on the running-config to the startup-config file using the following command:

    copy running-config startup-config
  18. On Destination filename [startup-config]?, press enter key.

  19. Reboot the cisco router (i.e. using the physical switch of the router).

    After this you have the new password in effect.

« Older posts Newer posts »