function

Scala - Using tuples in an anonymous function

I just ran across this post about using tuples in an anonymous function, and thought it was good enough to reproduce here, with only the solution part.

In essence, the question is, if I have a Map like this, which is actually composed of tuples:

scala> val x = Map(1 -> "foo", 2 -> "bar")
x: scala.collection.immutable.Map[Int,String] = Map(1 -> foo, 2 -> bar)

how do I use each value in the tuples in an anonymous function?

Scala reduceLeft examples

The reduceLeft method on the Scala collections is fun. Just start with a collection:

scala> val a = Array(20, 12, 6, 15, 2, 9)
a: Array[Int] = Array(20, 12, 6, 15, 2, 9)

Then give reduceLeft a simple function to work with, and let it do its thing:

Creating Play Framework template functions (examples)

When you want to create a Play Framework template function, you can find some examples on the Scala Templates page. There you'll find these two examples.

First, assuming you have a Product model defined something like this:

case class Product(var name: String, var price: BigDecimal)

The first template looks like this:

Using a map function on a collection in a Play Framework template

Here's a quick example of how to use a map function call on a Scala collection in a Play Framework template:

A Scala implicit method argument and field example

UPDATE: This example shows how to create an implicit method in Scala 2.9 or older. You can use a slightly simpler approach with Scala 2.10 and newer, which I've documented in this Scala 2.10 implicit class example.

I'm not going to do much writing here today, but instead I'll just demonstrate how an implicit method argument works with implicit fields in Scala. Without any further ado, here's some code:

Scala - How to add new methods to existing classes

Update: This article was written for Scala 2.9. Things changed a little bit in Scala 2.10, so see this new article, Creating implicit methods in Scala 2.10, for correct examples for 2.10 and newer versions of Scala.

A cool thing about implicit conversions in Scala is that they let you add new methods to existing classes, including existing Java and Scala classes such as String, File, and so on.

Scala List class filter method examples

The Scala List class filter method implicitly loops over the List you supply, tests each element of the List with the function you supply. Your function must return true or false, and filter returns the list elements where your function returns true.

(Note: Even though I use a List in these examples, the filter method can be used on any Scala sequence, including Array, List, Vector, Seq, etc.)

Source code for an example Android 'send email' function/method

Android FAQ: Can you share some source code for an Android send email method?

If you need a simple Android 'send email' function/method, this source code should do the trick for you:

A Scala REST 'get content' client function using Apache HttpClient

As quick post here today, if you need a Scala REST client function, the following source code should be able to work for you, or at least be a good starting point. I'm using it in several applications today, and the only thing I think it needs is the ability to set a connection timeout and socket timeout, and I share the code for that down below.

Here's my Scala REST 'get content' client function, using the Apache HttpClient library:

Passing a function to a function in Scala (callbacks); cleaning up Java Swing

I've posted a lot of Scala source code examples out here lately, and as I keep trying to learn more about passing one function to another function in Scala (function callbacks), here's another example showing how you can use Scala's functional programming approach to clean up some Java Swing code:

Syndicate content