Flog: the functional logger
Scala Prof
by
3y ago
In this series of blogs, we've covered functional versions of comparers, table parsers, and just "code." It's time to revisit an earlier functional topic: loggers (see Spy). Spy was OK but the syntax was not ideal. In particular, it required an extra set of parentheses which had to be removed when removing the log messages. So, over the last few years, I have created a new functional logger called Flog. You can find it at Flog. Take a look, in particular, at the README.md file, and also under the test directory: FlogExamples.sc as well as other spec files in the test directory ..read more
Visit website
Using: Did they forget something?
Scala Prof
by
3y ago
I was upgrading one of my open-source projects (TableParser) to Scala 2.13 and I realized that I could now use the new "Using" resource management utility. But, it seems they missed a necessary signature. Let's review: The basic signature for normal (single-resource use) is this apply method (I've used a context bound to save a bit of space, and I also renamed the first parameter to r): def apply[R : Releasable, A](r: => R)(f: (R) => A): Try[A]  = Try { Using.resource(r)(f) } As you can see, it invokes the other single-resource signature resource: def&nb ..read more
Visit website
Continued Fractions
Scala Prof
by
3y ago
I was inspired recently by a wonderful Mathologer video to implement continued fractions in Scala. I chose Scala for two reasons: first (as you would know if you've been here before) is that it's my favorite language and, second, it requires lazy programming.  What are ? And why are they so interesting? They've been around, essentially, since Euclid, although they hit their heyday in the 17th century with the likes of Leibniz, Wallis, and Euler (who came a little later), and many other less well known mathematicians. The particular subject of the video (all his videos are great, BTW) is t ..read more
Visit website
A functional comparer
Scala Prof
by
3y ago
One of the aspects of Scala and Java that I've always felt could be improved is the mechanism for comparing things. The basic scheme, inherited from Java, is that two objects, x and y, can be compared and if the result is less than zero, then x is smaller than y, if it's greater than zero, then x is larger than y, otherwise they are equal. This is the kind of code which we need to write for some user-defined class: case class Date(year: Int, month: Int, day: Int) extends Ordered[Date] { def compareTo(that: Date): Int = { val cfy = year.compareTo(that.year) if (cfy!=0) cfy else ..read more
Visit website
Posts on Quora
Scala Prof
by
3y ago
Sometimes, I am inspired to post answers on Quora. Many of these relate to Scala or functional programming in general, so I will try to list them here, in reverse chronological order: What are the key ingredients that make functional programming distinct from object-oriented or imperative programming? Isn't a monad just admitting that functional programming isn't useful? Which objects in Scala are mutable which are immutable and why does it matter? What are typeclasses in Scala, and how are they useful? Do you need to know any Java classes to be a Scala master? What is the apply function in ..read more
Visit website
Scala table parser
Scala Prof
by
3y ago
I've been busy over the last year with some new Scala projects in my GitHub space. In this blog I will talk about TableParser. The current release is v1.0.5. TableParser is a CSV file parser which uses type classes to facilitate the task of programming the parser. I have written elsewhere about the advantages of type classes, but in a nutshell, a type class (which is usually defined as a trait with a single parametric type, e.g. trait T[X]) can allow you to create classes which provide functionality which derives from the combination of the type class T and its underlying type X. The totality ..read more
Visit website
Functionalizing code
Scala Prof
by
3y ago
I'm not sure if functionalizing is really a word. But let's suppose it is. Some time ago, I wrote an application to allow me to view the output of the "Results" option that Blackboard Lean (oops: Learn) provides us. Since most answers use HTML, the content of individual answers can be obscured at best. At worst, impossible. So, I created a filter that takes the CSV file and turns it into an HTML file with a table (aspects of questions are columns, student submissions are rows). I recently upgraded it to allow me to specify a particular set of columns that I was interested in. But I was ..read more
Visit website
2018, Spark 2.2 and Scala 2.12
Scala Prof
by
3y ago
Here we are already in 2018. I haven't written much here lately but I have plenty of ideas. I just need time to write them up. My new BigData/Scala class will begin next week and I have been thinking about what exactly it is about Scala that made it the choice for implementing Spark in the first place. Indeed, I've developed a new module for introducing Scala in that way, i.e. if you were trying to implement Spark, what features would you require in order to be able to do it. Here, for example, is a very promising lode of insights, starting with an answer from Mattei Zaharia, the originator o ..read more
Visit website
Making comparisons more functional
Scala Prof
by
3y ago
In Scala 2.7 we got Ordering, a type class that essentially made Ordered, the original Scala extension of Java's Comparable, somewhat obsolete. Ordering (and type classes in general) is great, of course, and a big improvement. But what a shame they didn't go all the way and have Ordering provide a much more functional way of comparing. Let me explain what I mean. Take any Java/Scala class with at least two fields in it. Implementing the comparison function, whether in Scala or Java is typically going to look like this (from the source of java.time.LocalDate.java): int compar ..read more
Visit website
Some observations on foldLeft with reference to short-circuiting
Scala Prof
by
3y ago
Have you ever written code such as the following and expected it to quit accessing values of y after it hits the first true value? val y = Seq[Boolean](false, true, ..., false)val x = y.foldLeft[Boolean](false)(_ || _) After all, the second parameter of || is call-by-name so, if it's not needed to determine the outcome of the value for x, then that should mean that the recursion of the foldLeft should stop, right? But it doesn't in practice, and if you look at the definition of foldLeft, you can see why. There is no short-circuit defined. I therefore created a method in th ..read more
Visit website

Follow Scala Prof on FeedSpot

Continue with Google
Continue with Apple
OR