variables

Functional programming: Math functions, not programming functions

“There’s only ONE rule, but it’s an important one: all of your values must be functions. Not programming functions, but math functions.”

I think I read that quote in an earlier version of this article. The quote is about functional programming, and it influenced something I wrote in my book, Functional Programming, Simplified: Functional programmers think about themselves as being mathematicians, and think of their code as being a combination of algebraic equations, where each function is a pure function that you can think of in mathematical terms.

Making wrong code look wrong (Joel on Software)

A long time ago — 2005, to be exact — I read this article named Making wrong code look wrong, and it was a big influence on me. These days I don’t know how many people use variable naming conventions, but when working on web applications I still like the “us” (unsafe) and “s” (safe) convention for handling user input. As Joel Spolsky discusses in that article, that convention has a good way of making wrong software code look wrong.

Erlang has single-assignment variables

“Erlang has single-assignment variables. As the name suggests, they can be given a value only once. If you try to change the value of a variable once it has been set, you’ll get an error.”

(“Single-assignment variables” are the same as val fields in Scala. Using them can make your code more like algebra.)

What are the Drupal 8 Node class fields (field names)?

I was just trying to modify one of my Drupal 8 template files — node.html.twig — and I couldn’t find any good documentation for what variables/values/fields are in the Drupal 8 Node class, so I dumped some output to my browser, and saw that these are the Node fields:

Drupal 8: How to put a View or Block between the Content and Comments

I’m not going to discuss this code much, but in short, the source code below is for a Drupal 8 preprocess_node function that I use to set variables for (a) a custom view and (b) a custom block. I set the variables in this function, and then display them in my node.html.twig file like this:

{{ similar_by_terms }}

and this:

How to use variable names with the Scala ‘foldLeft’ method

Here’s a quick example of how to use variable names with the Scala foldLeft method:

val pSum = movies.foldLeft(0.0)((accum, element) => accum + p1Movies(element) * p2Movies(element))

In this example the variable movies is a Seq, and the variables p1Movies and p2Movies are Map objects.

Scala - How to declare multiple variables on one line

I thought I’d have a little fun with Scala today, and show different ways to declare multiple variables on one line. These aren’t the most useful or common things to do, but they’re interesting, and I know I learned at least one thing while looking into this.

You can define multiple fields on one line, separated by commas, as long as they are all the same type and have the same value:

String interpolation in Scala 2.10 and newer (String variable substitution)

Scala string FAQ: How do I embed variables in strings in Scala?

A lot of nice new features were added in Scala in version 2.10, but a simple feature that makes me happy is string interpolation. This lets Scala developers embed variables inside strings just like you can do in other languages like Perl, PHP, and Ruby -- but perhaps in an even more powerful way. (I say "perhaps" because I haven't used those other languages recently.)