
Scala Collections for the Easily Bored Part 2: One at a Time 28 Jul 2008 As I hinted previously, this series is intended to delve into Scala’s extensive collections API and the many ways in which it can make your life easier. Probably the most important operations you could ever perform on collections are those which examine each element, one at a time. After all, what’s a more common array idiom than looping over all values? In that vein, this article starts by looking at foreach, the imperative programmer’s bread-and-butter when it comes to types like Array and List. Iterating As I said above, looping over every item in a collection is probably the most heavily used operation in a programmer’s repertoire. This code should be old hat to anyone coming from a Java background. In this case, we aren’t using a loop of any kind, but rather creating a block (Ruby’s name for a closure) which takes a single parameter and passes it to the built-in puts method. The same approach is taken in Scala. Folding Yeah, I can’t read it either. Reduce Mapping Binding
Scala in practice: Composing Traits – Lego style As a kid, i loved to play with Lego bricks, especially to build freaky spacecrafts. At that time it was easy to let my phantasy go (where noone has gone before) and build completely new models simply by composing some standard bricks. Those bricks weren’t too specialized, meaning that there weren’t too many constraints on how to combine them. On the other side you always had to compose a new spacecraft from the very ground up as there weren’t some more higher organized units like engines or control cabins. Nowadays, you’ll find such units. Now you may ask how that cute childhood story relates to Scala? Look, it’s a spacecraft Let’s start with a hull for our spacecraft. As you can see, method engage and therefore the whole class is abstract, so you can’t instantiate a pure hull of a spacecaft. Captains place Typically, a spacecraft possess a kind of ‘control center’ which is normally well suited for initiating the start of a craft, hence should provide an implementation for method engage.
Lots And Lots Of foldLeft Examples | Matt Malone's Old-Fashioned Software Development Blog In my last post I reviewed the implementation of scala.List’s foldLeft and foldRight methods. That post included a couple of simple examples, but today I’d like to give you a whole lot more. The foldLeft method is extremely versatile. It can do thousands of jobs. Below, I’ll present a list of problem descriptions and solutions. Sum Write a function called ‘sum’ which takes a List[Int] and returns the sum of the Ints in the list. I’ll explain this first example in a bit more depth than the others, just to make sure we all know how foldLeft works. These two definitions above are equivalent. The foldLeft method takes that initial value, 0, and the function literal, and it begins to apply the function on each member of the list (parameter ‘c’), updating the result value (parameter ‘r’) each time. In the first defintion, foldLeft’s second parameter (a function literal) uses explicitly named parameters. Product Now that you’ve got the idea, try this one. Did you get it? Count Average Last Whew!
RFC 5849 - The OAuth 1.0 Protocol [Docs] [txt|pdf] [draft-hammer-oauth] [Diff1] [Diff2] [Errata] Obsoleted by: 6749 INFORMATIONAL Errata Exist Internet Engineering Task Force (IETF) E. Hammer-Lahav, Ed. Request for Comments: 5849 April 2010 Category: Informational ISSN: 2070-1721 Abstract OAuth provides a method for clients to access server resources on behalf of a resource owner (such as a different client or an end- user). It also provides a process for end-users to authorize third- party access to their server resources without sharing their credentials (typically, a username and password pair), using user- agent redirections. RFC 5849 OAuth 1.0 April 2010 Table of Contents 1. RFC 5849 OAuth 1.0 April 2010 1. The OAuth protocol was originally created by a small community of web developers from a variety of websites and other Internet services who wanted to solve the common problem of enabling delegated access to protected resources. 1.1. 1.2. 1.3.
Damn Cool Algorithms, Part 1: BK-Trees Posted by Nick Johnson | Filed under coding, tech, damn-cool-algorithms This is the first post in (hopefully) a series of posts on Damn Cool Algorithms - essentially, any algorithm I think is really Damn Cool, particularly if it's simple but non-obvious. BK-Trees, or Burkhard-Keller Trees are a tree-based data structure engineered for quickly finding near-matches to a string, for example, as used by a spelling checker, or when doing a 'fuzzy' search for a term. BK-Trees were first proposed by Burkhard and Keller in 1973, in their paper "Some approaches to best match file searching". Before we can define BK-Trees, we need to define a couple of preliminaries. Now we can make a particularly useful observation about the Levenshtein Distance: It forms a Metric Space. These three criteria, basic as they are, are all that's required for something such as the Levenshtein Distance to qualify as a Metric Space. The tree is N-ary and irregular (but generally well-balanced). Previous PostNext Post
A Tale of 3 Nightclubs * Part Zero : 10:15 Saturday Night * (In which we will see how to let the type system help you handle failure)... * First let's define a domain. import scalaz import Scalaz object Sobriety extends Enumeration { val Sober, Tipsy, Drunk, Paralytic, Unconscious = Value } object Gender extends Enumeration { val Male, Female = Value } case class Person(gender : Gender.Value, age : Int, clothes : Set[String], sobriety : Sobriety.Value) * Let's define a trait which will contain the checks that *all* nightclubs make! trait Nightclub { //First CHECK def checkAge(p : Person) : Validation[String, Person] = if (p.age < 18) "Too Young!". else if (p.age > 40) "Too Old!". else p.success //Second CHECK def checkClothes(p : Person) : Validation[String, Person] = if (p.gender == Gender.Male && ! "Smarten Up!". else if (p.gender == Gender.Female && p.clothes("Trainers")) "Wear high heels".fail //Third CHECK def checkSobriety(p : Person): Validation[String, Person] "Sober Up!". * Part One : Clubbed to Death for { a <- checkAge(p)
Functional Scala: Polymorphic Functions ?!? Welcome to another episode of Functional Scala! If you’re coming from the object oriented world, chances are good that you’ve already heard something about a concept called Polymorphism. You may also know that there are different classes of Polymorphism out there: most of us are familiar with so called (Sub-)Type polymorphism and Parametric polymorphism (you may call it Generics). Since functions take a central role within Functional Programming (i need to mention this, in case you’ve forgotten it), we want to take a closer look if Scala allows for so called polymorphic functions, which plays in the area of parametric polymorphism. Parametric polymorphism on types Let’s start the journey and take a look at an example of a polymorphic type, coming from well- known ground: Lists … You may know, that a List can take elements of an arbitrary type. See that explicit type declaration at line 3? type List takes type parameters Parametric polymorphism on functions Wait wait wait, you may say.
JCR with Scala « contentGoesHere Having seen Scala being mentioned in a recent comment I thought I'd give it a try with JCR. First I installed the Scala Plugin for Eclipse. Alternatively I could have also used Scala's command line tools. Then I created a new Scala project from within the Eclipse IDE and put the Jackrabbit jars on the classpath. Now I set out to implement Hop 3 from the article First Hops with Jackrabbit. I therefore downloaded the test.xml file and put it into a subfolder of my Scala project called data. The main method is a straight forward translation from java. The dump method takes a Node argument and recursively outputs its properties and its child nodes. Using Scala for JCR programming seems very promising. (*) <rant>This is in some ways similar to implicit type conversion in C++ but done right (i.e. more powerful and safer).