Quantcast
Channel: alvinalexander.com - sequence
Browsing index pages (25 articles)

How to drop the first matching element in a Scala sequence

Summary: This blog post shows one way to drop/filter the first matching element from a Scala sequence (Seq, List, Vector, Array, etc.). I don’t claim that the algorithm is efficient, but it does...

View Article


How to use a Scala 'for' loop with embedded 'if' statements (guards)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 3.3, “How to use a 'for' loop with embedded 'if' statements (guards).”ProblemYou want to add one or more...

View Article


How to use Scala for loops with multiple counters

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 3.2, “How to use Scala for loops with multiple counters.”ProblemYou want to create a loop with multiple...

View Article

Scala: How to loop over a collection with for and foreach (plus for loop...

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 3.1, “How to loop over a collection with for and foreach (and how a for loop is translated)”ProblemYou...

View Article

How to merge (concatenate) Lists in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 11.5, “How to Merge (Concatenate) Lists in Scala”ProblemYou want to merge/concatenate the contents of...

View Article


How 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

How 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 Article

How 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 Article


How 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 Article


Scala: 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 Article

Make 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 Article

Make 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 Article

How 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 Article


How 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 Article

How 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 Article


Getting 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 Article

How 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 Article


Scala: 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 Article

Converting 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 Article

Scala 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 Article
Browsing index pages (25 articles)


Latest Images