Snippets

scala

Return Expressions

Syntax:
    Expr1 ::=  ‘return’ [Expr]
A return expression return e must occur inside the body of some enclosing named method or function. The innermost enclosing named method or function in a source program, f, must have an explicitly declared result type, and the type of e must conform to it. The return expression evaluates the expression e and returns its value as the result of f. The evaluation of any statements or expressions following the return expression is omitted. The type of a return expression is scala.Nothing.
The expression e may be omitted. The return expression return is type-checked and evaluated as if it was return().
Read more, section 6.20.

return statement

scala

Return Expr and anonymous functions

Syntax:
    Expr1 ::=  ‘return’ [Expr]
An apply method which is generated by the compiler as an expansion of an anonymous function does not count as a named function in the source program, and therefore is never the target of a return expression.
Returning from a nested anonymous function is implemented by throwing and catching a scala.runtime.NonLocalReturnException. Any exception catches between the point of return and the enclosing methods might see the exception. A key comparison makes sure that these exceptions are only caught by the method instance which is terminated by the return.
If the return expression is itself part of an anonymous function, it is possible that the enclosing instance of f has already returned before the return expression is executed. In that case, the thrown scala.runtime.NonLocalReturnException will not be caught, and will propagate up the call stack.

return type NonLocalReturnException syntax

scala

MUST NOT use "var" inside a case class

Case classes are syntactic sugar for defining classes in which - all constructor arguments are public and immutable and thus part of the value's identity, have structural equality, a corresponding hashCode implementation and apply/unapply auto-generated functions provided by the compiler.
By doing this:
case class Sample(str: String, var number: Int)
You just broke its equality and hashCode operation. Now try using it as a key in a map.
As a general rule of thumb, structural equality only works for immutable things, because the equality operation must be stable (and not change according to the object's history). Case classes are for strictly immutable things. If you need to mutate stuff, don't use case classes.
"The Joy of Clojure": if any two mutable objects resolve as being equal now, then there’s no guarantee that they will a moment from now.
And if two objects aren’t equal forever, then they’re technically never equal ;-)

case-classes best practices variables equals hashCode

scala

Declaring abstract "var" members

It's a bad practice to declare abstract vars in abstract classes or traits. Do not do this:
trait Foo {
  var value: String
}
Instead, prefer to declare abstract things as def:
trait Foo {
  def value: String
}

// can then be overridden as anything
class Bar(val value: String) extends Foo
The reason has to do with the imposed restriction - a var can only be overridden with a var. The way to allow freedom to choose on inheritance is to use def for abstract members. And why would you impose the restriction to use a var on those that inherit from your interface. def is generic so use it instead.
Read more.

override traits interfaces trait inheritance

sql

JOIN, CROSS JOIN, and INNER JOIN in MySQL

In MySQL, JOIN, CROSS JOIN, and INNER JOIN are syntactic equivalents (they can replace each other). In standard SQL, they are not equivalent. INNER JOIN is used with an ON clause, CROSS JOIN is used otherwise.

join cross-join on-clause

sql

CARTESIAN JOIN or CROSS JOIN

The CARTESIAN JOIN or CROSS JOIN returns the Cartesian product of the sets of records from the two or more joined tables. Thus, it equates to an inner join where the join-condition always evaluates to True or where the join-condition is absent from the statement.

Syntax:

The basic syntax of CARTESIAN JOIN or CROSS JOIN is as follows:
SELECT table1.column1, table2.column2...
FROM  table1, table2 [, table3 ]

Read more: SQL - CARTESIAN or CROSS JOINS

inner-join

sql

INNER JOIN example

Example:

Consider the following two tables,
(a) CUSTOMERS table is as follows:
| ID | NAME     |
+----+----------+
|  1 | Ramesh   |
|  2 | Khilan   |
(b) Another table is ORDERS as follows:
|OID  | DATE  | C_ID | AMOUNT |
+-----+-------+------+--------+
| 102 | 10-08 |    3 |   3000 |
| 100 | 10-08 |    3 |   1500 |
| 101 | 11-20 |    2 |   1560 |
Now, let us join these two tables using INNER JOIN as follows:
SQL> SELECT  ID, NAME, AMOUNT, DATE
     FROM CUSTOMERS, ORDERS;
This would produce the following result:
| ID | NAME     | AMOUNT | DATE  |
+----+----------+--------+-------+
|  1 | Ramesh   |   3000 | 10-08 |
|  1 | Ramesh   |   1500 | 10-08 |
|  1 | Ramesh   |   1560 | 11-20 |
|  2 | Khilan   |   3000 | 10-08 |
|  2 | Khilan   |   1500 | 10-08 |
|  2 | Khilan   |   1560 | 11-20 |

sql

TOP clause

The SQL TOP clause is used to fetch a TOP N number or X percent records from a table.
Note: All the databases do not support TOP clause. For example MySQL supports LIMIT clause to fetch limited number of records and Oracle uses ROWNUM to fetch limited number of records.

Syntax:

The basic syntax of TOP clause with SELECT statement would be as follows:
SELECT TOP number|percent column_name(s)
FROM table_name
WHERE [condition]
Read more: SQL - TOP, LIMIT or ROWNUM Clause

TOP-clause ROWNUM

sql

SELECT TOP example

Example:

Consider the CUSTOMERS table having the following records:

| ID | NAME     | AGE | ADDRESS   | SALARY   |
+----+----------+-----+-----------+----------+
|  1 | Ramesh   |  32 | Ahmedabad |  2000.00 |
|  2 | Khilan   |  25 | Delhi     |  1500.00 |
|  3 | kaushik  |  23 | Kota      |  2000.00 |
|  4 | Chaitali |  25 | Mumbai    |  6500.00 |
Following is an example on SQL server, which would fetch top 3 records from CUSTOMERS table:

SQL> SELECT TOP 3 * FROM CUSTOMERS;
This would produce the following result:

| ID | NAME    | AGE | ADDRESS   | SALARY  |
+----+---------+-----+-----------+---------+
|  1 | Ramesh  |  32 | Ahmedabad | 2000.00 |
|  2 | Khilan  |  25 | Delhi     | 1500.00 |
|  3 | kaushik |  23 | Kota      | 2000.00 |
Read more: SQL - TOP, LIMIT or ROWNUM Clause

SQL-server

sql

SELECT LIMIT example

Example:

Consider the CUSTOMERS table having the following records:

| ID | NAME     | AGE | ADDRESS   | SALARY   |
+----+----------+-----+-----------+----------+
|  1 | Ramesh   |  32 | Ahmedabad |  2000.00 |
|  2 | Khilan   |  25 | Delhi     |  1500.00 |
|  3 | kaushik  |  23 | Kota      |  2000.00 |
|  4 | Chaitali |  25 | Mumbai    |  6500.00 |
Following is an example on MySQL server, which would fetch top 3 records from CUSTOMERS table:

SQL> SELECT * FROM CUSTOMERS
LIMIT 3;
This would produce the following result:

| ID | NAME    | AGE | ADDRESS   | SALARY  |
+----+---------+-----+-----------+---------+
|  1 | Ramesh  |  32 | Ahmedabad | 2000.00 |
|  2 | Khilan  |  25 | Delhi     | 1500.00 |
|  3 | kaushik |  23 | Kota      | 2000.00 |
Read more: SQL - TOP, LIMIT or ROWNUM Clause

MySQL limit-keyword

Loading rules...