Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
def managers(emps) {
	return emps.findAll { e -> e.isManager() }
}
Code Block
def highPaid(emps) {
	threshold = 150
	return emps.findAll { e -> e.salary > threshold }
}
Code Block
def paidMore(amount) {
	return { e -> e.salary > amount}
}

...

Code Block
class Employee {
	def name, salary
	boolean manager
	String toString() { return name }
}

def emps = [new Employee(name:'Guillaume', manager:true, salary:200),
	new Employee(name:'GraimeGraeme', manager:true, salary:200),
	new Employee(name:'Dierk', manager:false, salary:151),
	new Employee(name:'Bernd', manager:false, salary:50)]

def managers(emps) {
	return emps.findAll { e -> e.isManager() }
}

assert emps[0..1] == managers(emps) // [Guillaume, GraimeGraeme]

def highPaid(emps) {
	threshold = 150
	return emps.findAll { e -> e.salary > threshold }
}

assert emps[0..2] == highPaid(emps) // [Guillaume, GraimeGraeme, Dierk]

def paidMore(amount) {
	return { e -> e.salary > amount}
}
def highPaid = paidMore(150)

assert highPaid(emps[0]) // true
assert emps[0..2] == emps.findAll(highPaid)

def filename = 'test.txt'
new File(filename).withReader{ reader -> doSomethingWith(reader) }

def readersText
def doSomethingWith(reader) { readersText = reader.text }

assert new File(filename).text == readersText