May 17, 2013
What is a “functional language”? If it’s a language with first-class functions, then C is a functional language. If it’s a language that disallows hidden side-effects, then Haskell isn’t a functional language.
— our own Kalani Thielen
foo = [1,2,3,4,5]
for i in (0...foo.size)
foo[i] = foo[i] * 2
end
foo
# => [2,4,6,8,10]
foo = [1,2,3,4,5]
bar = []
for element in foo
bar << element * 2
end
foo
# => [1,2,3,4,5]
bar
# => [2,4,6,8,10]
foo = [1,2,3,4,5]
foo.map {|x| x * 2}
# => [2,4,6,8,10]
foo = [1,2,3,4,5]
foo.map {|x| x * 2}
foo = some_dataset
foo.map {|x| costly_operation(x)}.reduce {|m, o| aggregation(m, o)}
element.addListener(new NotARealClosure() {
public void someCallback(Event e)
{
// ugh.
}
});
These [functional idioms] are by far the most easily (and most commonly) abused parts of Guava, and when you go to preposterous lengths to make your code "a one-liner," the Guava team weeps.
Option
scala> val x = List((1,2)).toMap
x: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2)
scala> x.get(1)
res2: Option[Int] = Some(2)
scala> x.get(3)
res3: Option[Int] = None
scala> x.getOrElse(4, 5)
res4: Option[Int] = 5
null
mean?
HashMap<Integer, Integer> someHashMap = new HashMap<>();
someHashMap.put(1, 2);
Optional<Integer> foo = Optional.fromNullable(someHashMap.get(1));
Optional<Integer> bar = Optional.fromNullable(someHashMap.get(3));
if (foo.isPresent()) foo.get(); // 2
bar.isPresent(); // false
bar.get(); // IllegalStateException
bar.or(5); // 5
NullPointerException
s. Ever.
Immutable________