Short source code examples

If you need to have multiple types extend a base type in Haskell, while using the data keyword, and while using Haskell’s record syntax, this approach seems to work:

I’m currently trying to cram Haskell into my brain, and in an effort to do so, I’ve been writing a fake POS (point of sales) application. It has a command-line UI that prompts the user to add Pizzas and a Customer to an Order.

I need to re-organize the code, but this is what about 50% of the code looks like right now. The other 50% is in a module that defines what a Pizza is, and also defines its helper functions.

I’m relatively new to Haskell, so I don’t know if there are better ways to do this, but if you need to define a function that takes no input parameters and returns a value, this function type signature works:

foo :: [Char]
foo = "foo"

I don’t know a good reason for using that specific approach, but I have found in test code that I want to do something with input/output, and so I needed a function that returned an IO String, but didn’t take any input parameters, and that function definition looks like this:

To configure your GHCI prompt, create a Haskell/GHCI configuration file named .ghci, which should be in your home directory. Assuming that you are first creating it, put content like this in it:

:set prompt "ghci> "
:set +t

The first command shown sets the GHCI prompt, and the second command tells GHCI to show the type of each expression you type.

As an example of the file location, my home directory is /Users/Al, so I created my file as /Users/Al/.ghci.

There may be other ways to do this, but if you need to determine the type (type signature) of an object in a Haskell program, the following approach worked for me.

First, add this import statement to your program:

import Data.Typeable

What that does is give you access to a function named typeOf, which you can then use like this:

I was working on a Haskell factorial function (which turned out to be easy) and decided I wanted to write a program to prompt a user at the command line for an Int, and then do the factorial of that Int.

I quickly learned that this required a String to Int conversion, which I wanted to do safely (I didn’t want my program to blow up). Technically what I do is actually a String to Maybe Int conversion, as you can see in this code:

This is some test code I wrote. It shows how to read a text file in Scala.

This first program shows how to read the entire file into memory and print out the “byte values” of each byte/character in the file:

FWIW, this is the source code for a sed script I use on my Mac OS X system to convert HTML output generated by MacDown into a format I need. MacDown generates some extra “cruft” that I don’t need, so I use these sed commands to clean up that HTML output:

If you need to turn off (disable) java.util.logging logging, adding this line of code to your main method seems to work:

As a quick note, I just found this getUrlInputStream method in an old Scala project. It needs to be updated and cleaned-up, but if you are looking for some code to get you started with opening a URL as an InputStream in Scala, this may be helpful:

Java Jar file FAQ: Is there an easy way to delete a file from a Jar file?

Yes. Because a Jar file is just a Zip file, you can use the zip command to remove a file from a Jar file, like this:

zip -d MyApp.jar fileToDelete.foo

Of course the file to delete can be any sort of file. I just went through this process where I had to delete a configuration file, and my command looked like this:

Android FAQ: How do I make my Android app display in landscape mode only?

To get an Android application to display in landscape mode only (to lock it in landscape mode), add this line to your Activity definition in the AndroidManifest.xml file:

android:screenOrientation="landscape"

For example, I’m writing an Android football game right now, and this is the definition for my main activity in the Android manifest file:

If you want to make sure that your users’ Android display stays on while your app is running -- such as if you are writing a game/gaming app -- use this call and flag in the onCreate method of your Android Activity:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

Here’s what the Android docs say about the FLAG_KEEP_SCREEN_ON setting:

Without looking things up I don’t know when this changed, but ... to refer to a built-in Android color with the android:drawable XML tag, use code like this:

Android FAQ: What does the Android isScrollContainer XML setting do?

From the Android docs:

Set this if the view will serve as a scrolling container, meaning that it can be resized to shrink its overall window so that there will be space for an input method. If not set, the default value will be true if “scrollbars” has the vertical scrollbar set, else it will be false.

Android FAQ: Where should the ’assets’ directory be when using Android Studio?

Solution: If you want to include an assets folder in your project when using Android Studio, create the folder as src/main/assets, i.e., as an assets folder under src/main.

You can later open files in the assets folder using code like this:

InputStream is = getBaseContext().getAssets().open(relativeFilename);

where relativeFilename is the name of your file, like foo.jpg.

I just added an editable EditText widget to an Android application, and I needed to make the EditText a certain height, and also make the text in the EditText scroll, in case the user added some really long text. I used the following XML in my Android layout to make this happen:

Here’s some source code that shows how to add an Android OnClickListener to a CheckBox:

Here’s a source code snippet that shows how to create an Android AlertDialog. If I remember right, I got the initial code from Stack Overflow, and then adapted it for my need, which was to confirm that the user wanted to delete an image from an image gallery:

While writing some Java code today, I needed a method to get the extension from a filename. This code solved the problem:

public static String getFilenameExtension(String filename) {
    String extension = "NoExtension";
    int i = filename.lastIndexOf('.');
    if (i > 0) {
        extension = filename.substring(i + 1);
    }
    return extension;
}

Given an input filename like foo.jpg, the getFilenameExtension method returns the “jpg” part as a String.