# Scala crammer/reminder and links

This page is my crib sheet to some of the great resources needed for Scala developers beyond the syntax guides. It is also notes and explanations for stuff I found in the large scala code base I’m working on, and an explanation/reminder/crammer for the terms that people use when showing off about about IT and functional programming.

## Links

## Wierd syntax

If you have java keywords or method names which conflict with scala reserved words then you can either quote it Thread.`yield`

or you can
assign it a new symbol on import. eg eq=>ceq below

import com.datastax.driver.core.querybuilder.QueryBuilder.{put,eq=>ceq}

## Class for traits

classOf[MyTrait]

## SOLID

S | Single Responsibility Principle | Do one thing well, nots lots of things |

O | Open Closed Principle | "software entities...should be open for extension, but closed for modification." |

L | Liskov substitution principle | "objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program". See also design by contract. |

ISP | Interface segregation principle | "many client-specific interfaces are better than one general-purpose interface" |

DIP | Dependency Inversion Principle | one should "Depend upon Abstractions. Do not depend upon concretions." |

## Big O

http://bigocheatsheet.com/

Asymptotic Complexity. eg someone may say: O(N log(N)) in the average case and O(N squared) in the worst case. Aymptotic means approaching a value or curve arbitrarily closely. In IT it is used to describe how algorithms respond to changes in input size. eg how does the processing time of an algorithm change as the problem size increases.

Worse case time complexity.

reminder about log: in base 10, 10 to the power 3 is 10*10*10 = 1000. Or exponential 3. Opposite of exp is log. So log base 10 of 1000 is 3.

Example from some scala notes: The complexity of msort is O(N log(N)), where N is the length of the input list. To see why, note that splitting the list in two and merging two sorted lists each take time proportional to the length of the argument list(s). Each recursive call of msort halves the number of elements in its input, so there are O(log(N)) consecutive calls until the base case of lists of length 1 is reached. However, for longer lists each call spawns off two further calls. Adding everything up we obtain that at each of the O(log(N)) call levels, every element of the original lists takes part in one split operation and in one merge operation. Hence, every call level has a total cost proportional to O(N). Since there are O(log(N)) call levels, we obtain an overall cost of O(N log(N)).

What? Well, being base 2, then as for base 10, log(8) is 3. ie 2*2*2=8. So every time you chop something in 2 you are doing a log(N) - the reverse of doubling it. If you have to do this for every element, ie for N elements then you are multiplying it by the number of elements.

## Bluffers glossary for Enterprise bus and patterns in 2017

ETL | Extract Transform and Load. eg from a DB, muck about a bit, reload it |

Imperative programming | Use statements to change program state. Consists of commands for the computer to perform. The imperative style of programming emphasises careful sequencing of operations so that their effects happen in the right order. This style is characterised by iteration with loops, mutating data in place, and methods with side effects. It is the dominant paradigm of languages such as C, C++, C# and Java, and contrasts with the functional style. |

Declarative programming | Expresses the logic of a computation without describing its control flow - eg SQL, regexp, functional programming. |

Functional programming | The functional style of programming emphasises functions and evaluation results and deemphasises the order in which operations occur. The style is characterised by passing function values into looping methods, immutable data, methods with no side effects. It is the dominant paradigm of languages such as Haskell and Erlang, and contrasts with the imperative style. |

Idempotent | Unary operations or Functions: if, whenever it is applied twice to any value, it gives the same results as if it were applied once; i.e. f(f(x)) is equivalent to f(x). For example, the absolute value function, where abs(abs(x)) is the same as abs(x). Binary operation: if, whenever it is applied to two equal values, it gives that value as the result. For example, the function giving the max value of two equal values is idempotent: max(x,x) is exactly the same as x. |

Invarient | It can mean a property that always holds true when a data structure is well-formed. For example, it is invarient of a sorted binary tree that each node is ordered before its right subnode, if it has a right subnode. Invarient is also sometimes used as a synonym for nonvarient: "class Array is invarient in its type parameter" |

First class function | Scala supports first-class functions, which means tyou can express functions in function literal syntax, i.e. (x: Int) => x+1, and that functions can be represented by objects, which are called function values. |

Function literal | A function with no name in Scala source code, specified with function literal syntax. For example, (x:Int, y:Int)=>x+y. Anonymous functions in source code are called function literals. At run time, function literals are instantiated into objects called function values. |

Transitive | X is transitive if whenever an element a is related to an element b, and b is related to element c, then a is also related to c. |

Commutative | An operation op is commutative if (a op b) = (b op a) for all values a,b |

Associative | An operation op is associative if ((a op b) op c) = (a op (b op c)) for all values a, b and c Example, sum and max are commutative and associative operations that are commonly used in Spark accumulators. |

Pessimistic vs Optimistic locking | Optimistic - read the record, noting version or timestamp and then write it back only if that version/timestamp has not changed. Pessimistic means you lock the record for your personal use prior to working on it. |

Two phase commit | 1st phase, ask all the participant nodes to prepare for the transaction. ie in oracle record the information in redo logs in case of error, and then places a distributed lock on modified tables, which prevents read. 2nd phase tell participating nodes to commit, but if that is not possible then all must rollback. |

ACID | Atomicity, Consistency, Isolation, Durability |

CAP theorem (also called Brewer's theorem) | Impossible for a distributed system to simultaneously provide all three of : Consistency, Availability, Partition tolerance (eg n/w fail) |

CQRS | Command Query Response Segregation |

Referential transparency and referential opacity | are properties of parts of computer programs. An expression is said to be referentially transparent if it can be replaced with its value without changing the behaviour of a program (in other words, yielding a program that has the same effect and output on the same input). The opposite term is referential opacity. |

Reciprical | 1/x |

Cannonical | literally a 'rule', and has also come to mean a 'standard' Canonical form, a natural unique representation of an object, or a preferred notation for some object. Canonical form, data that has been cannonicalised into a completely unique representation, from a previous form that had more than one possible representation. |

Super class | the one you extend |

Sub class | any class which inherits from this class |

Covarient | putting a plus sign (+) before the type parameter. The class or trait then subtypes covariently with - in the same direction as - the type annotated parameter. For example, List is covarient in its type parameter, so List[String] is a subtype of List[Any] |

Contravarient | Putting a minus sign (-) before the type parameter. The class or trait then subtypes contravariently with - in the opposite direction as - the type annotated parameter. For examople, Function1 is contravarient in its first type parameter, and so Function1[Any, Any] is a subtype of Function1[String, Any]. |

Invarient | c[T] and c[T'] are not related. |

Currying | A way to write functions with multiple parameter lists. For instance def(x:Int)(y:Int) is a curried function with two parameter lists. A curried function is applied by passing several lists, as in: f(3)(4). However, it is also possible to write a partial application of a curried function, such as f(3). |

ETA expansion (Scala puzzlers, puzzler 12 in the book) | Eta expansion is the operation of automatically coercing a method into an equivalent function. A method belongs to a class, and has no separate existance, whereas a function exists and can be passed about into higher order functions and so on. Eta expansion basically wraps the methos with a function so it can be passed about....
Given the method def foo[A,B](a:A):B Eta expansion is performed in either of the following conditions: - an underscore is given in lieu of an argument list val f = foo _ // f is inferred as A=>B - No argument list is provided and a function type is expected: val f: A => B = foo What? With no _ or (_) scala can determine the type and know that foo is a function. In some cases it wont be able to in which case you must use (_) or _ depending on your aim. (_) means pass in as a function value - evaluated as it is used _ means a partially applied function - evaluate it now. For example, you have a function with a side effect which changes state and takes no parameters, if you pass in as a function which is evaluated now then the side effect is immediate, if you pass in as a function value then the side effect only occurs when the function is used, and each time it is used. Scala allows you to leave off the underscore after a method name when it knows that the expected type is a function, and the type of the function is consistent with the signature of the method. A function value is not evaluated until it is used, and it is evaluated every time. A partially applied function has parameters evaluated at the time. |

Existential type | An existential type includes references to type variables that are unknown. For example, Array[T] forSome { type T} is an existential type. It is an array of T, where T is some completely unknown type. All that is assumed about T is that it exists at all. This assumption is weak, but means at least that an Array[T] forSome { type T} is indeed an array and not a banana. The most practical existential type is Class[_] - lets you pass a class into a function, much as you would in Java. |

Try[Unit] is a valid return type | But match { case Success => doesn't work....instead you use Success(_) which is another nice little nugget. |

Orthogonal | is the relation of two lines at right angles to another (perpendicularity) |

Monad | A monad is an object that wraps another object. You pass the Monad a mini-program, i.e. functions, to perform the data manipulation of the underlying object, instead of manipulating the object directly. Monad chooses how to apply the program to the underlying object. |

Persistent data structure | ie you never edit it, you always create new parts. Example is a set in functional corsera when additional of a New element to the set will create new items linked to it, rather than editing/mutating the prev ptr. |

Implicit | Implicit functions allow automatic conversion. More precisely, they allow on-demand function application when this can help satisfy type inference.implicit def strToInt(c:String)=x.toInt val y: Int = "123" math.max("123", 111) |

View bounds | like type bounds demand such a function exists for the given type. You specify a view bound with <%// this says that A has to be viewable as Int class Container[A<%Int] {def addIt(x:A)=123+x} |

Type upper and lower bound | B >: SomeClass, lower bound, means B is a supertype of SomeClass B <: SomeClass, upper bound, means B is a subclass of SomeClass - inherits from it. |

Context bound | This is used in Type Class, see my other post |

Scala Dynamic | scala.language.dynamics Its mad. Lets you have a list of constants and then add more at run time. |

It is a strange mix of tech/phrases etc above, but its a handy refresher for things used in the past, and more functional things that are not used all the time, and so can be forgotten but should not be.