val listOfLists = List(List(1, 3, 5), List(2, 4, 6)) listOfLists flatten // List(1, 3, 5, 2, 4, 6)
I wanted a place to create random jabberings about software topics I'm working on. You'll find posts about software architecture, Java, web development, DSL development, language implementation, etc.
Friday, May 10, 2013
Scala Lists Flatten
The flatten method on Scala lists is used to remove one level of nested structure. For example, if we have a list that contains two lists, we can flatten that structure and result in a single one-dimensional list. Here's an example.
Scala Lists Zip and Unzip
Zipping is the process of taking two lists and merge them into a single list of tuples, where the first item in each list comprises the first tuple, the second item in each list comprises the second tuple, etc. It's best to think of this operation like a zipper on a jacket where the first teeth on each side of the zipper become merged, the second teeth on each side of the zipper become merged, etc.
Here's an example of two lists that are zipped.
The opposite of the zip method is unzip. Here's the result of unzipping the result from above.
Here's an example of two lists that are zipped.
val digits = List(1, 2, 3) val chars = List("a", "b", "c") val result = digits zip chars // result : List((1,"a"), (2,"b"), (3,"c"))
The opposite of the zip method is unzip. Here's the result of unzipping the result from above.
result unzip // (List(1, 2, 3),List("a", "b", "c"))
Thursday, May 9, 2013
Scala - Transform All Elements in a Scala Collection
Often times, you need to transform all of the elements in a Scala collection. This is where the map method comes in handy. With the map method, you can apply a function to all of the elements in a collection and obtain a new collection with the results. Here's an example that uses the map method to create a new list where all of the elements in the list are converted to uppercase.
This has the same effect as iterating over all of the elements and calling toUpperCase on each element individually.
Sometimes you'll have a collection of various types. For example, your collection might contain Int and String values and you want to convert all of the String values to uppercase and still keep the Int values in the resulting list. For this, we have the collect method, along with case classes. Here's an example.
val places = List("Atlanta", "Chicago", "Dallas", "New York City") val uppercasePlaces = places.map(_.toUpperCase)
This has the same effect as iterating over all of the elements and calling toUpperCase on each element individually.
Sometimes you'll have a collection of various types. For example, your collection might contain Int and String values and you want to convert all of the String values to uppercase and still keep the Int values in the resulting list. For this, we have the collect method, along with case classes. Here's an example.
val numbersAndStrings = List(1, "Atlanta", 10, "Boston", "Dallas") val numbersAndUppercaseStrings = numbersAndStrings.collect { case s: String => s.toUpperCase; case i: Int => i }
Wednesday, May 1, 2013
Throttling Service Requests Using Camel
This tutorial shows how to use Camel to throttle the sending of messages to a service. This is necessary when you don't want to overload the service. For example, the service might have a published service level agreement (SLA) requiring you to only send a certain number of requests per second or minute. If you're not familiar with Camel, read my blog post on Implementing Pipes and Filters Using Camel.
The source code for this example is available on GitHub: https://github.com/travisdazell/camel-throttler
Let's consider an integration scenario where we receive orders via an incoming file drop. We then pick up the file, process it with some custom Java code, and then place a message on a JMS queue. Here's the Camel route, with a throttler that only allows one message to be sent to the JMS queue every five seconds.
The source code for this example is available on GitHub: https://github.com/travisdazell/camel-throttler
Let's consider an integration scenario where we receive orders via an incoming file drop. We then pick up the file, process it with some custom Java code, and then place a message on a JMS queue. Here's the Camel route, with a throttler that only allows one message to be sent to the JMS queue every five seconds.
<camelcontext xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="file://incoming" /> <to uri="bean:orderProcessor?method=processIncomingOrder" /> <throttle timePeriodMillis="5000"> <constant>1</constant> <to uri="activemq:queue:ORDERS.INCOMING" /> </throttle> </route> </camelcontext>
Subscribe to:
Posts (Atom)