mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-10-29 17:50:22 +00:00
Added samples for Xtend
This commit is contained in:
93
samples/Xtend/BasicExpressions.xtend
Normal file
93
samples/Xtend/BasicExpressions.xtend
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
/*******************************************************************************
|
||||||
|
* Copyright (c) 2012 itemis AG (http://www.itemis.eu) and others.
|
||||||
|
* All rights reserved. This program and the accompanying materials
|
||||||
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
|
* which accompanies this distribution, and is available at
|
||||||
|
* http://www.eclipse.org/legal/epl-v10.html
|
||||||
|
*******************************************************************************/
|
||||||
|
package example2
|
||||||
|
|
||||||
|
import org.junit.Test
|
||||||
|
import static org.junit.Assert.*
|
||||||
|
|
||||||
|
|
||||||
|
class BasicExpressions {
|
||||||
|
|
||||||
|
@Test def void literals() {
|
||||||
|
// string literals work with single or double quotes
|
||||||
|
assertEquals('Hello', "Hello")
|
||||||
|
|
||||||
|
// number literals (big decimals in this case)
|
||||||
|
assertEquals(42, 20 + 20 + 1 * 2)
|
||||||
|
assertEquals(42.00bd, 0.00bd + 42bd)
|
||||||
|
|
||||||
|
// boolean literals
|
||||||
|
assertEquals(true, !false)
|
||||||
|
|
||||||
|
// class literals
|
||||||
|
assertEquals(getClass(), typeof(BasicExpressions))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test def void collections() {
|
||||||
|
// There are various static methods to create collections
|
||||||
|
// and numerous extension methods which make working with them
|
||||||
|
// convenient.
|
||||||
|
val list = newArrayList('Hello', 'World')
|
||||||
|
assertEquals('HELLO', list.map[toUpperCase].head)
|
||||||
|
|
||||||
|
val set = newHashSet(1, 3, 5)
|
||||||
|
assertEquals(2, set.filter[ it >= 3].size)
|
||||||
|
|
||||||
|
val map = newHashMap('one' -> 1, 'two' -> 2, 'three' -> 3)
|
||||||
|
assertEquals( 2 , map.get('two'))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test def void controlStructures() {
|
||||||
|
// 'if' looks like in Java
|
||||||
|
if ('text'.length == 4) {
|
||||||
|
// but it's an expression so it can be used in more flexible ways:
|
||||||
|
assertEquals( 42 , if ('foo' != 'bar') 42 else -24 )
|
||||||
|
} else {
|
||||||
|
fail('Never happens!')
|
||||||
|
}
|
||||||
|
|
||||||
|
// in a switch the first match wins
|
||||||
|
switch (t : 'text') {
|
||||||
|
// use predicates
|
||||||
|
case t.length > 8 :
|
||||||
|
fail('Never happens!')
|
||||||
|
// use equals
|
||||||
|
case 'text' :
|
||||||
|
assertTrue(true)
|
||||||
|
default :
|
||||||
|
fail('never happens!')
|
||||||
|
}
|
||||||
|
|
||||||
|
// switch also supports type guards, which provide a safe
|
||||||
|
// and convenient alternative to Java's 'instanceof'-cascades.
|
||||||
|
val Object someValue = 'a string typed to Object'
|
||||||
|
assertEquals('string',
|
||||||
|
switch someValue {
|
||||||
|
Number : 'number'
|
||||||
|
String : 'string'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test def void loops() {
|
||||||
|
// for loop
|
||||||
|
var counter = 1
|
||||||
|
for (i : 1 .. 10) {
|
||||||
|
assertEquals(counter, i)
|
||||||
|
counter = counter + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
// while loop
|
||||||
|
val iterator = newArrayList(1,2,3,4,5).iterator
|
||||||
|
counter = 1
|
||||||
|
while(iterator.hasNext) {
|
||||||
|
val i = iterator.next
|
||||||
|
assertEquals(counter, i)
|
||||||
|
counter = counter + 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
60
samples/Xtend/Movies.xtend
Normal file
60
samples/Xtend/Movies.xtend
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
/*******************************************************************************
|
||||||
|
* Copyright (c) 2012 itemis AG (http://www.itemis.eu) and others.
|
||||||
|
* All rights reserved. This program and the accompanying materials
|
||||||
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
|
* which accompanies this distribution, and is available at
|
||||||
|
* http://www.eclipse.org/legal/epl-v10.html
|
||||||
|
*
|
||||||
|
* Author - Sven Efftinge
|
||||||
|
*******************************************************************************/
|
||||||
|
package example6
|
||||||
|
|
||||||
|
import org.junit.Test
|
||||||
|
import static org.junit.Assert.*
|
||||||
|
import java.io.FileReader
|
||||||
|
import java.util.Set
|
||||||
|
import static extension com.google.common.io.CharStreams.*
|
||||||
|
|
||||||
|
class Movies {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the total number of action movies
|
||||||
|
*/
|
||||||
|
@Test def void numberOfActionMovies() {
|
||||||
|
assertEquals(828, movies.filter[categories.contains('Action')].size)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the year the best rated movie of 80ies (1980-1989) was released.
|
||||||
|
*/
|
||||||
|
@Test def void yearOfBestMovieFrom80ies() {
|
||||||
|
assertEquals(1989, movies.filter[(1980..1989).contains(year)].sortBy[rating].last.year)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the sum of the number of votes of the two top rated movies.
|
||||||
|
*/
|
||||||
|
@Test def void sumOfVotesOfTop2() {
|
||||||
|
val long movies = movies.sortBy[-rating].take(2).map[numberOfVotes].reduce[a, b| a + b]
|
||||||
|
assertEquals(47_229, movies)
|
||||||
|
}
|
||||||
|
|
||||||
|
val movies = new FileReader('data.csv').readLines.map[ line |
|
||||||
|
val segments = line.split(' ').iterator
|
||||||
|
return new Movie(
|
||||||
|
segments.next,
|
||||||
|
Integer::parseInt(segments.next),
|
||||||
|
Double::parseDouble(segments.next),
|
||||||
|
Long::parseLong(segments.next),
|
||||||
|
segments.toSet
|
||||||
|
)
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data class Movie {
|
||||||
|
String title
|
||||||
|
int year
|
||||||
|
double rating
|
||||||
|
long numberOfVotes
|
||||||
|
Set<String> categories
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user