Groovy def and C# var
def in Groovy is the same as var in C#
See more posts like this on Tumblr
#def #Groovy #var #Cdef env = System.getenv()
println env.JAVA_HOME
- Everything is an Object (numbers, strings, etc.) except null.
- Collections
- List def list = [1, 2, 3, 4]
- Map def map = [“key”: “value”, “key2”: “value2”]
- Variables, Blocks and Methods are defined with “def”
- def day = “Thursday”
- def hello() {“Hey There”}
- No semi-colons!
- Classes
- public class MyClass {…}
- public class MyClass extends AnotherClass{…}
- Logic
- if(…){…} else if(…){…} else{…}
- switch(…) {case…:}
- Looping
for(…){…} [1, 2, 3].each(){print"${it}“}
More verbose notation:
class ChecksumAccumulator {
private var sum = 0
def add(b: Byte): Unit = {
sum += b
}
def checksum(): Int = {
return ~(sum & 0xFF) + 1
}
}
Shorthand for methods is that you can leave off the curly braces if a method computes only a single result expression. If the result expression is short, it can even be placed on the same line as the def itself. With these changes, class ChecksumAccumulator looks like this:
class ChecksumAccumulator{
private var sum = 0
def add(b: Byte): Unit = sum += b
def checksum(): Int = ~(sum & 0xFF) + 1
}
Another way to express such methods is to leave off the result type and the equals sign, and enclose the body of the method in curly braces. In this form, the method looks like a procedure, a method that is executed only for its side effects.
class ChecksumAccumulator {
class ChecksumAccumulatorConcise {
private var sum = 0
def add(b: Byte) {sum += b}
def checksum(): Int = ~(sum & 0xFF) + 1
}
Programming in Scala, Second Edition
Martin Odersky (Author), Lex Spoon (Author), Bill Venners (Author)
cd to the grails root dir
cmd:
grails dbm-generate-changelog changelog.groovy
file is created in:
./grails-app/migrations/changelog.groovy
If you want to clear the output pane on the groovyConsole. There is a clear output menu item under the View menu and the hotkey for that is Cmd-W.
Dynamic languages such as Groovy, Ruby, and Python do not give you nearly as much compile-time assistance as statically typed languages such as Java. Some errors that you might expect to be caught at compile time are actually left to runtime, including method resolution. Sadly, the comfort of the compiler often encourages Java developers to forget about testing altogether.
Source:
This pattern is sometimes know as the pub-sub pattern where there is the event publisher and zero or many even subscriber.
var net = require(‘net’);
var format = require('util’).format;
var server = net.createServer();
server.on('connection’, function(conn) {
var printPrefix = ’[’ + conn.remoteAddress + ’:’ + conn.remotePort + ’] ’;
function print() {
var formatted = format.apply({}, arguments);
console.log(printPrefix + formatted);
}
print('connected’);
conn.on('data’, function(data) {
print('got some data:’, data);
});
conn.on('end’, function() {
print('ended.’);
});
conn.on('close’, function() {
print('closed.’);
});
conn.on('error’, function(err) {
print('error:’, err);
});
conn.setEncoding('utf-8’);
});
server.on('error’, console.error);
server.listen(8080);
####
In this pattern the callback is referred to as the 'event listener’.
server.on(connection, function(conn){…});
In this case the conn object is also and event emitter.
- Event type
The event type is represented by a string, usually lower-cased and containing only letters. Each event type passes arguments to the listeners, and these arguments are specific to the event type — some documentation should be provided about which arguments the listeners for this event type should expect.
- The listner
The event listener is just a simple JavaScript function, either declared inline (like in the previous example) or declared in advance.
You can either declare it inline or name the function.
- Listen At Most Once
If some events will happen only once, at most once or if you’re only interested in the first instance of that event, you should use .once instead of .on like we did on the socket close and end events
- The Special Case of Error
The event type is just a meaningless identifier that is used by the event emitter to dispatch events with the exception of the error event type.
As a rule of thumb, we always listen to the error event.
You can define functions inside other functions. Just like local variables, such local functions are visible only in their enclosing block.
This is a traditional method:
import scala.io.Source
object LongLines {
def processFile(filename: String, width: Int) {
val source = Source.fromFile(filename)
for(line <- source.getLine())
processLine(filename, width, line)
}
private def processLine(filename: String, width: Int, line: String){
if(line.length > width)
printlin(filename + “: ” + line.trim)
}
}
Here is how you would leverage this as a local function.
def processLine(filename: String, width: Int){
//declare the function locally
def processLine(filename: String, width: Int, line: String) {
if(line.length > width)
print(filename + “: ” + line)
}
val source = Source.fromFile(filename)
for(line <- source.getLines()) {
processLine(filename, width,line)//then use the function
}
}
Programming in Scala, Second Edition
Martin Odersky (Author), Lex Spoon (Author), Bill Venners (Author)
The operator ===
is commonly referred as Deep Equals in JavaScript. The only difference between double equals ==
and deep equals is that the former does not perform type comparison but in fact, converts the type of one of the operands to make their types same. Deep equals, on the other hand, returns false if both types are not the same.