call by name

This is a page from my book, Functional Programming, Simplified

How to Use By-Name Parameters in Scala

“Call me, call me by my name, or call me by number.”

Chesney Hawkes, “The One and Only”

Introduction

In previous lessons I showed how to pass a function into another function. I showed how to do that (the syntax), and I also showed why to do that (to easily pass in new algorithms).

Examples of how to use types in your Scala classes (generics, call-by-name parameters) alvin June 21, 2015 - 1:29pm

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 19.8, “Examples of how to use types in your Scala classes.”

To put what you’ve learned in this chapter to use, let’s create two examples. First, you’ll create a “timer” that looks like a control structure and works like the Unix time com‐ mand. Second, you’ll create another control structure that works like the Scala 2.10 Try/ Success/Failure classes.

Scala: How to build control structures with types and call-by-name parameters

Table of Contents1 - Example 1: Creating a Timer2 - Example 2: Writing Your Own “Try” Classes3 - The Scala 2.10 Try classes

(This article is an excerpt from the Scala Cookbook.)

To put what you’ve learned in this chapter to use, let’s create two examples. First, you’ll create a timer method that looks like a Scala control structure, and works like the Unix time command. Second, you’ll create another control structure that works like the Scala 2.10 Try/Success/Failure classes.

Back to top

Example 1: Creating a Timer

On Unix systems you can run a time command (timex on some systems) to see how long commands take to execute:

$ time find . -name "*.scala"