return

Dart: How to return a known, constant, or literal value in a Future

Sometimes a Dart or Flutter API may require you to return a Future even if you already have a known, constant, or literal value. If/when that happens, you can use this approach:

return new Future(() { return 42; });

In this example the known value is 42, but it could have been a string like "Hello", a boolean like true, or any other known or literal value.

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

Recursion: How to Write a ‘sum’ Function in Scala

With all of the images of the previous lesson firmly ingrained in your brain, let’s write a sum function using recursion!

Sketching the sum function signature

Given a List of integers, such as this one:

val list = List(1, 2, 3, 4)

let’s start tackling the problem in the usual way, by thinking, “Write the function signature first.”

How to write a Scala function that returns multiple values

As a quick note today, if you want to write a Scala function that returns multiple values, just return the values inside a tuple. For example, I just wrote a function to return radio station number and name, and the last part of that function looks like this:

def getRadioStationInfo(...) = {
   ...
   (104.3, "The Fan")
}

The two values are returned in an instance of a Scala Tuple2 class.

How to support a fluent style of programming in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 5.9, “How to support a fluent style of programming in Scala.”

Problem

You want to create an API so developers can write code in a fluent programming style, also known as method chaining.

Solution

A fluent style of programming lets users of your API write code by chaining method calls together, as in this example:

Perl subroutine - how to return multiple values

Perl subroutines - multiple return values FAQ: Can you share some examples of how to return multiple values from a Perl subroutine?

Did you know that you can return multiple values from a Perl subroutine (function)? As a practical matter I haven't used this feature very much, but I've always thought it was an interesting programming language feature, very different from many other languages.

A Java class that lists constants for all FTP server return codes

Here's a Java class that I created today that creates constants for all the FTP server return codes listed on this Wikipedia page. If you ever start doing programming against a real FTP server, or perhaps a mock FTP server, you'll know what these FTP return codes are needed for.

Without any further delay, here's the Java source code for a class I've named FtpServerReturnCodes:

Perl subroutine values - How to return multiple values from a Perl function

Perl subroutine FAQ: How do I return multiple values from a Perl subroutine (Perl function)?

One of the things I really like about Perl is that you can return multiple values from a function (and you don't have to create some type of artificial class to encapsulate them). Here's the basic way to return multiple values from a function/subroutine named foo:

AppleScript keystroke tip: simulating the Enter key

AppleScript keystroke FAQ: Can you share an AppleScript keystroke/keyboard example?

When I created my AppleScript program to open a list of URLs in Safari I needed a way to type the URLs into Safari. That turned out to be pretty easy, I did it like this:

keystroke "http://www.apple.com/"

The much harder part was figuring out how to simulate typing the Enter (or Return) key from AppleScript. Without further ado, here's the answer:

AppleScript time example: The current time as hours and minutes

AppleScript time subroutine FAQ: Can you share an example of an AppleScript subroutine (function) that returns te current time?

Here's an example AppleScript subroutine (also known as a function or method) that returns the current time as hours and minutes, along with the AM/PM modifier. For the purposes of creating an alarm clock AppleScript program, I didn't like the default format of the AppleScript current date command, so I use this method to extract the current time information I want.