YouTube link: https://www.youtube.com/watch?v=T6A3FpfEJG8
Surfboards, suntans, and... Scala?
Your hosts this episode: Dick Wall, Daniel Spiewak, Heather Miller,
YouTube link: https://www.youtube.com/watch?v=T6A3FpfEJG8
Surfboards, suntans, and... Scala?
Your hosts this episode: Dick Wall, Daniel Spiewak, Heather Miller,
Here are 10 one-liners which show the power of scala programming, impress your friends and woo women; ok, maybe not. However, these one liners are a good set of examples using functional programming and scala syntax you may not be familiar with. I feel there is no better way to learn than to see real examples.
Updated: June 17, 2011 - I'm amazed at the popularity of this post, glad everyone enjoyed it and to see it duplicated across so many languages. I've included some of the suggestions to shorten up some of my scala examples. Some I intentionally left longer as a way for explaining / understanding what the functions were doing, not necessarily to produce the shortest possible code; so I'll include both.
The map function takes each element in the list and applies it to the corresponding function. In this example, we take each element and multiply it by 2. This will return a list of equivalent size, compare to o
| #!/bin/sh | |
| # Homebrew Script for OSX | |
| # To execute: save and `chmod +x ./brew-install-script.sh` then `./brew-install-script.sh` | |
| echo "Installing brew..." | |
| /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" | |
| echo "Installing brew cask..." | |
| brew tap homebrew/cask |
| import scala.xml._ | |
| // To convert a Maven pom.xml to build.sbt: | |
| // 1) Place this code into a file called PomToSbt.scala next to pom.xml | |
| // 2) Type scala PomtoSbt.scala > build.sbt | |
| // The dependencies from pom.xml will be extracted and place into a complete build.sbt file | |
| // Because most pom.xml files only refernence non-Scala dependencies, I did not use %% | |
| val lines = (XML.load("pom.xml") \\ "dependencies") \ "dependency" map { dependency => | |
| val groupId = (dependency \ "groupId").text | |
| val artifactId = (dependency \ "artifactId").text |
| echo "$STRING" | iconv -t ascii//TRANSLIT | sed -r s/[^a-zA-Z0-9]+/-/g | sed -r s/^-+\|-+$//g | tr A-Z a-z |
| #!/usr/bin/env bash | |
| # | |
| # Bootstrap script for setting up a new OSX machine | |
| # | |
| # This should be idempotent so it can be run multiple times. | |
| # | |
| # Some apps don't have a cask and so still need to be installed by hand. These | |
| # include: | |
| # | |
| # - Twitter (app store) |
| # Run complete. Total time: 00:34:14 | |
| Benchmark Mode Samples Score Error Units | |
| o.g.m.f.FibonacciMicroBenchmark.groovy_primitive_cs avgt 30 1.961 ± 0.055 ms/op | |
| o.g.m.f.FibonacciMicroBenchmark.baseline_java_30 avgt 30 2.004 ± 0.084 ms/op | |
| o.g.m.f.FibonacciMicroBenchmark.baseline_java_boxing_30 avgt 30 5.504 ± 0.036 ms/op | |
| o.g.m.f.FibonacciMicroBenchmark.groovy_primitive_30 avgt 30 5.733 ± 0.024 ms/op | |
| o.g.m.f.FibonacciMicroBenchmark.groovy_indy_30 avgt 30 5.903 ± 0.033 ms/op | |
| o.g.m.f.FibonacciMicroBenchmark.golo_30 avgt 30 6.545 ± 0.060 ms/op | |
| o.g.m.f.FibonacciMicroBenchmark.nashorn_30 avgt 30 7.812 ± 0.061 ms/op |