functions

Kotlin functions to create Lists, Maps, and Sets

Table of Contents1 - Kotlin Arrays2 - Kotlin List functions3 - Kotlin Map functions4 - Kotlin Set functions5 - Summary: Kotlin List, Map, and Set creation functions

With Kotlin you can create lists, maps, and sets with standard functions that are automatically in scope. Here are those functions.

Scala: What do “effect” and “effectful” mean in functional programming?

When you get started with functional programming (FP) a common question you’ll have is, “What is an effect in functional programming?” You’ll hear advanced FPers use the words effects and effectful, but rarely do you get a definition of what they mean.

Effects are related to monads

The first step in the process of understanding effects is to say that they’re related to monads, so you have to know a little bit about monads to understand effects.

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.

The essence of Scala ~ Martin Odersky

Per this tweet, back on May 15 Martin Odersky shared a slide with these contents:

The essence of Scala: Fusion of functional and object-oriented programming in a typed settings.

- Functions for the logic
- Objects for the modularity

Scala: A look at flatMap and map on Option

As a quick Scala tip, if you haven’t worked with the flatMap on an Option much, it can help to know that flatMap’s function should return an Option, which you can see in this REPL example:

scala> Some(1).flatMap{ i => Option(i) }
res0: Option[Int] = Some(1)

You can tell this by looking at the function signature in the scaladoc for the flatMap method on the Option class:

Quotes from Clean Code

Back in 2013 I read the book Clean Code by Robert C. Martin, and in an effort to keep that book alive with me a little while longer, I decided to make my own “Cliffs Notes” version of the book on this page. One of my favorite notes from below is that a language named LOGO used the keyword to in the same way that Scala uses def, so a method named double would be defined as to double... instead of def double..., which seems like it would help developers name methods better.

Using structs in JavaScript (example, syntax)

Bearing in mind that I rarely use JavaScript and I’m not an expert in it, I like the idea of using something like a C-like “struct” in my JavaScript code, so I used the following approach on a recent project. First, I define my JavaScript struct:

function Book(title, href, imageUri, description) {
    this.title = title;
    this.href = href;
    this.imageUri = imageUri;
    this.description = description;
}

Then I define an array of Book types:

Examples of Play Framework Twirl template functions

If you ever need to create a Play Framework Twirl template function, here’s an example of how to create and use one. First, create the Twirl function like this:

@fullUrl(uri: String) = @{
    s"http://kbhr.co/$uri"
}

That function creates a complete URL from the URI it’s given.

Later in your template you can call the Twirl template function like this: