![]() ![]() Note that the lengths of the two lists are different: letters has 26 elements and ranges has 24, which means that the last two elements of letters (‘y’ and ‘z’) get dropped in the zipped list. This produces a Map from letters to lists of three numbers. Next, we zip the letters with the ranges and create a Map from the pairs using toMap. (The sliding function is a beautiful thing, especially for natural language processing problems.) scala> val ranges = (1 to 26).toList.sliding(3).toList Let’s start by creating the sliding list of number ranges. Also, what is “_” and what is “x”? (By which I mean, what are they in terms of the logic of the program? We know they are ways of referring to the elements being mapped over, but they don’t help the human reading the code understand what is going on.) ![]() ![]() That did it, but that one-liner isn’t clear at all, so we should break things up a bit. scala> letters.zip((1 to 26).toList.sliding(3).toList).toMap.mapValues(_.map(x => letters(x-1)).sorted.reverse) (Did I mention this was a pointless task in and of itself?) Here’s a one-liner that can do it. Okay, now here’s our (pointless) task: we want to create a map from every letter (from ‘a’ to ‘x’) to a list containing that letter and the two letters that follow it in reverse alphabetical order. Letters: List = List(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z) scala> val letters = "abcdefghijklmnopqrstuvwxyz".split("").toList.tail We begin by creating a list of all the letters in the alphabet. It’s not a very meaningful example, but it serves the purpose without being very complex. of what you can do to give your code more breathing space. This isn’t just for others - you are likely to be a reader of your own code, often months after you originally wrote it, and you want to be kind to your future self. In fact much to the contrary: it is crucial to use strategies that allow readers of your code to see the logic behind your statements. Also, some students indicated that they had gotten the impression that one should try to pack everything onto one line if possible, and that breaking things up was somehow less advanced or less Scala-like. So taking a step back, it is important to break operation sequences up a bit, but it isn’t always obvious to beginners how one can do so. It works well in the REPL and when you have lots of text to explain what is going on around the piece of code in question, but it seems to have given a bad model for writing actual code. I do admit to a fair amount of guilt in using such sequences of operations in class lectures and even in some of these tutorials. These map-over-mapValues-over-map sequences of statements can be almost incomprensible, both for some other person reading the code, and even for the person writing the code. It was instigated by patterns I was noting in my students’ code namely, that they were packing everything into one-liners with map after map with map after map, etc. This post isn’t so much a tutorial as a comment on coding style with a few pointers on how code blocks in Scala work. Other posts are on this blog, and you can get links to those and other resources on the links page of the Computational Linguistics course I’m creating these for. Additionally you can find this and other tutorial series on the JCG Java Tutorials page. This is part 12 of tutorials for first-time programmers getting into Scala.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |