Sometimes, you need to be able to look at the private members of your classes in order to test that something has gone to plan. Unit testing is one scenario where this makes sense.
By using the getDeclaredField method, passing the name of the field; the reflection framework will send back the definition. This field gets executed through the use of the get method, passing in the object instance.
To finish the picture here, we can also get access on methods that are private as well:
The functional programming paradigm has certainly seen a lot of attention of late, where some of the features that can be exploited from it have properties that assist in scale programming.
In today’s post, I’ll walk through Java 8 streams which allow you to treat collection data structures in a functional way.
What is a stream?
A stream prepares a collection of elements in such a way that you can operate on it functionally. The higher-order functions that you’d commonly see in a functional arrangement are:
Now that we have a basic structure of artists and songs, we can define some test data to work with.
Don’t judge me.
ArtistrickAstley=newArtist("Rick Astley",50,Arrays.asList(newSong("Never Gonna Give You Up",1987,500000)));ArtistbonJovi=newArtist("Jon Bon Jovi",54,Arrays.asList(newSong("Livin' on a Prayer",1986,3400000),newSong("Wanted Dead or Alive",1987,4000000)));List<Artist>artists=Arrays.asList(bonJovi,rickAstley);
Ok, now that the boilerplate is out of the way; we can start the fun stuff.
Map
map is a function that allows you to apply a function to each element of a list; transforming and returning it. We can grab just the artist’s names with the following:
toList comes out of the java.util.stream.Collectors class. So you can import static:
importstaticjava.util.stream.Collectors.toList;
Mapping deeper
flatMap allows you to perform the map process on arrays of arrays. Each artist has an array of songs, so we can flat map (at the artist level) to emit a flat list of songs:
Scala has the concept of implicit parameters which allows the developer to implicitly apply a value that has been previously defined. According to the documentation
A method with implicit parameters can be applied to arguments just like a normal method. In this case the implicit label has no effect. However, if such a method misses arguments for its implicit parameters, such arguments will be automatically provided.
An example
We’ll create a Person class that expects a first and last name. There’s also a manager field to be supplied:
Immediately, you can see that the manager parameter has been decorated with the implicit keyword. What this says is:
First, any Person that can be accessed (at the point of construction) that has been declared implicit.
Second, any Person also declared implicit in companion modules.
For demonstration purposes, a showHierarchy function has been created to show management:
defshowHierarchy()={print(s"$firstName $lastName is ")if(manager==null){println("not managed by anybody")}else{valmanagerFirstName=manager.firstNamevalmanagerLastName=manager.lastNameprintln(s"managed by $managerFirstName $managerLastName")}}
When the Person has a manager, you’ll see their name; otherwise this function will show that they are “not managed by anybody”.
Using the class
With all of this definition, we now take a look at usage. Note that the manager parameter has a default value of null. So, any invocation where an implicit can not be supplied still doesn’t need to be specified as it’s been defaulted.
Sam Brown is not managed by anybody
Joe Smith is not managed by anybody
Makes sense. sam wasn’t declared as implicit and as such, wouldn’t be offered in the construction of joe. So, we modify the construction of sam and add the keyword:
implicitvalsam=Person("Sam","Brown")
This has an immediate impact on the output of the program:
Sam Brown is not managed by anybody
Joe Smith is managed by Sam Brown
As you can see, sam has now been implicitly offered to the construction of joe and as such, the manager parameter gets filled (in the construction of joe).
Scala provides some simple options to get up and running the development of domain specific languages. In today’s post, I’ll take you through the usage of the implicit keyword that easily allows for this to happen.
End goal
For the purposes of today, I want to create a language that defines simple, directional instructions. “Move left”, “move forward”, etc. The end goal, I want a language that looks like this:
var jump = 5.0 units up
var shuffle = -2.0 units left
var fall = -6000.0 units down
Implicit conversion
The implicit conversion will allow us to get this process started. It’ll provide the ability for us to take that literal, double value (at the start of each of those statements), and convert them into a class instance that we can do more interesting things with.
Ok, so we don’t know what a DirectionUtil class is just yet. That’s coming. Right now, it’s important to focus on this function which is an implicit conversion; allows us to express a double-value like normal and have Scala treat it as a DirectionUtil instance.
. . which, as expected gives us an output like this:
(0.0,-5.0,0.0)
In closing
This is a really simple example, but you can see immediately how it can be applied to much more complex scenarios; and how you can be a lot more expressive in your scala source code.
In a previous article, we’d spent a little bit of time working with maps in clojure; we also looked at formalising the structure of a map into records. In today’s post, I’ll discuss some of the collection types that will allows us to carry around many values at once.
General
Before we start looking at the collection types, there are some general-purpose functions that you can use with these types. These are very helpful functions that really form the basis of your collection processing:
first will return you the head of your collection; with rest providing you with everything else but the head. last will return you the final item in your collection and cons will provide you with the ability to construct a new list with a given head and tail.
nth will give you indexed access to you collection.
List
A list is an ordered collection of elements. From the documentation:
Lists are collections. They implement the ISeq interface directly (except for the empty list, which is not a valid seq). count is O(1). conj puts the item at the front of the list.
Because lists are a natural part of the language, they’ll be invoked as a function if they’re not defined correctly or quoted adequately. A list can be defined using the list function, but can also be quoted:
A vector is an ordered collection of elements, but is optimised for the random-access case. From the documentation:
A Vector is a collection of values indexed by contiguous integers. Vectors support access to items by index in log32N hops. count is O(1). conj puts the item at the end of the vector.
Sets have methods to easily union two sets, or find the difference.
Map
Definition
A map is a collection of key-value-pairs. Declaring maps allows you to define attribute keys and values in an organised or arbitrary way. From the documentation:
A Map is a collection that maps keys to values. Two different map types are provided - hashed and sorted. Hash maps require keys that correctly support hashCode and equals. Sorted maps require keys that implement Comparable, or an instance of Comparator. Hash maps provide faster access (log32N hops) vs (logN hops), but sorted maps are, well, sorted. count is O(1). conj expects another (possibly single entry) map as the item, and returns a new map which is the old map plus the entries from the new, which may overwrite entries of the old.