Why coffee is good for developers
Moritz Grauel
Moritz Grauel
Kaffee ist weltweit am zweithäufigsten gehandelte Ware
(direkt nach Öl!)
<script type="text/javascript">
/* equals - how hard can it be? */
if (1 == '1') alert("WTF?!");
</script>
<script type="text/javascript">
/* It's math - I know math! */
if (Math.min() > Math.max()) {
alert('WTF?!');
}
</script>
<script type="text/javascript">
/* What is your favorite Number? */
alert('My favorite number is: '
+ 9999999999999999);
</script>
<script type="text/javascript">
/* Is this even possible? */
if (5 < 4 < 1) alert ('WTF?!')
</script>
<script type="text/javascript">
/* My favorite */
var wat = Array(16).join("wtf" - 1)
+ " Batman!";
alert(wat);
</script>
<script type="text/javascript">
/* Well... uhm...*/
if ('10' == ++[[]][+[]]+[+[]]) {
alert('WTF?!')
}
</script>
// Generated Javascript:
<script type="text/coffeescript">
if (1 == '1')
alert("WTF?!")
else
alert("Unicorns!")
</script>
$(document).ready(function() {
$(".hideable").each(function(index, item) {
$(item).on("click", function(event) {
$(event.target).slideUp(1000, function() {
console.log('animation complete');
});
});
});
});
$(document).ready(function() {
$(".hideable").each(function(index, item) {
$(item).on("click", function(event) {
$(event.target).slideUp(1000, function() {
console.log('animation complete')
})
})
})
})
$(document).ready function() {
$(".hideable").each function(index, item) {
$(item).on "click", function(event) {
$(event.target).slideUp 1000, function() {
console.log('animation complete')
}
}
}
}
$(document).ready function()
$(".hideable").each function(index, item)
$(item).on "click", function(event)
$(event.target).slideUp 1000, function()
console.log('animation complete')
$(document).ready () ->
$(".hideable").each (index, item) ->
$(item).on "click", (event) ->
$(event.target).slideUp 1000, () ->
console.log('animation complete')
# CoffeeScript version
$(document).ready () ->
$(".hideable").each (index, item) ->
$(item).on "click", (event) ->
$(event.target).slideUp 1000, () ->
console.log('animation complete')
// CoffeeScript compiler output
$(document).ready(function() {
return $(".hideable").each(function(index, item) {
return $(item).on("click", function(event) {
return $(event.target).slideUp(1000, function() {
return console.log('animation complete');
});
});
});
});
###
Block Comments and auto-var
###
drink = "coffee"
alert coffee
# default arguments and string interpolation
fill = (container, liquid = "coffee") ->
"Filling #{liquid} in the #{container}"
alert fill 'bucket'
# existential operator and aliases
# like: not/! , and/&&, is/=== etc
if not coffee? then alert "panic"
# JSON in coffee
json = bedcon:
year: 2012
presenter:
name: "Moritz"
# loops return an array
count = 0
addicted = until count > 5
count++
"Drinking Coffee #{count}"
alert addicted
# iterating over lists and maps
for x in [1..10]
console.log x
myLikes = { 'Espresso': true, 'Tea': false }
for key, value of myLikes
alert("I like #{key}") if value
# comprehensions
list = [1 .. 10]
evenDoubled =
(i * 2 for i in list when (i % 2 == 0))
alert evenDoubled
# Destructuring Assignment
fooBar = (baz) -> ["Foo#{baz}", "Bar#{baz}"]
[foo, bar] = fooBar(42)
class Person
constructor: (@name)
hi: ->
alert "Hi #{@name}"
class Developer extends Person
constructor: (@name, @favoriteBrewStyle) ->
super name
brew: ->
alert "Brewing #{@favoriteBrewStyle}"
mo = new Developer "Moritz", "Espresso"
<plugin>
<groupId>com.theoryinpractise</groupId>
<artifactId>coffee-maven-plugin</artifactId>
<version>1.4.1</version>
<configuration>
<coffeeDir>${basedir}/src/main/webapp/resources/scripts</coffeeDir>
<coffeeOutputDirectory>
${project.build.directory}/${artifactId}-${version}/resources/scripts
</coffeeOutputDirectory>
<bare>true</bare>
<compileIndividualFiles>true</compileIndividualFiles>
</configuration>
<executions>
<execution>
<id>coffee</id>
<goals>
<goal>coffee</goal>
</goals>
</execution>
</executions>
</plugin>