MySQL drop table and foreign keys tip - the FOREIGN_KEY_CHECKS variable
MySQL DROP TABLE FAQ: Help, my MySQL database tables have a lot of foreign keys, and as a result it's a pain to use the MySQL DROP TABLE command in my scripts; they keep failing because of all the...
View ArticleScala - How to create a range of characters (list, sequence)
Scala range FAQ: How can I easily create a range of characters in Scala, such as a range of alpha or alphanumeric characters?I learned recently that you can easily create a range of characters (a...
View ArticleScala - How to create a list of alpha or alphanumeric characters
While looking for code on how to create a random string in Scala, I came across this article, which shows one approach for creating a random string. For my brain today, it also shows a nice way to...
View ArticleHow to convert a Scala array (sequence) to string with mkString
Scala collections FAQ: How can I convert a Scalaarray to a String? (Or, more, accurately, how do I convert any Scala sequence to a String.)A simple way to convert a Scala array to a String is with the...
View ArticleScala - How to find the unique items in a List, Array, Vector (sequence)
Scala FAQ: How do I find the unique items in a List, Array, Vector, or other Scala sequence?Use the distinct method. Here's a simple example using a List of integers:scala> val x =...
View ArticleScala tip: How to extract a field from a sequence of objects to create a new...
A fun thing you can do with the map method on Scala sequences (Array, List, Seq, Vector, etc.), is convert a sequence of objects into a sequence of something else, typically extracting a field from the...
View ArticleConverting a sequence of Scala objects to JSON using the Play Framework
The code in this blog post shows how to convert a Seq of Scala objects to their equivalent JSON representation. Specifically, I’m working on an application to display Twitter data, and I want to...
View ArticleScala: Extracting data from an array of XML elements
Problem: Your XML data has an array of elements, and you need to extract the first element, second element, or more generally, the Nth element, using Scala.SolutionThe following simplified version of...
View ArticleHow to use a Scala Set as a function predicate
There’s a nice Scala Set tip in the book, Functional Programming Patterns in Scala and Clojure. The tip is this:You can use a Set as a predicate to a function.(A predicate is just a function/method...
View ArticleGetting a random element from a list of elements in Scala
In working on projects like SARAH and other apps where I’m trying to make a computer seem more “human”, I’ve been making my software reply to me in random ways. What I’ve found is that this ends up...
View ArticleHow to flatten a List of Lists in Scala with flatten
Scala problem: You have a list of lists (or more generally, a sequence of sequences) and want to create one list (sequence) from them.(The following solution comes from the Scala Cookbook, which I...
View ArticleHow to generate random numbers, characters, and sequences in Scala
Scala FAQ: How do I generate random numbers (or characters) in Scala, such as when testing an application, performing a simulation, and many other situations?SolutionCreate random numbers with the...
View ArticleHow to choose a collection class in Scala
This is an excerpt from the Scala Cookbook (partially re-worded for the internet). This is Recipe 10.2.Scala FAQ: How do I choose a Scala collection class to solve a particular problem?SolutionTo being...
View ArticleMake the Vector class your default immutable sequence (Scala best practice)
This is an excerpt from the Scala Cookbook (partially re-worded for the internet). This is Recipe 10.7, “Make the Vector Class Your ‘Go To’ Immutable Sequence.”ProblemYou want a fast, general-purpose,...
View ArticleMake the ArrayBuffer class your default mutable indexed sequence (Scala best...
This is an excerpt from the Scala Cookbook (partially re-worded for the internet). This is Recipe 10.8, “Make the Scala ArrayBuffer Class Your ‘Go To’ Mutable Indexed Sequence”ProblemYou want to use a...
View ArticleScala: How to use zipWithIndex or zip to create loop counters
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 10.11, “How to Use zipWithIndex or zip to Create Loop Counters”ProblemYou want to loop over a sequential...
View ArticleHow to extract a sequence of elements from a Scala Collection
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 10.18, “How to Extract a Sequence of Elements from a Scala Collection”ProblemYou want to extract a...
View ArticleHow to extract unique elements from a Scala sequence
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 10.21, “How to Extract Unique Elements from a Scala Sequence”ProblemYou have a collection that contains...
View ArticleHow to merge Scala sequential collections (List, Vector, ArrayBuffer, Array,...
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 10.22, “How to Merge Scala Sequential Collections”ProblemYou want to join two sequences into one...
View ArticleHow to populate Scala collections with a Range
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 10.25, “How to Populate a Scala Collection with a Range”ProblemYou want to populate a List, Array,...
View Article