Short source code examples

As a quick note today, the following Scala function is more or less the same as the Unix cat command:

I was curious about how the “input redirection” program on page 170 of Learn You a Haskell for Great Good (LYAH) worked, so I typed it into a file named do1.hs, with one additional line:

import Data.Char

main = do
    contents <- getContents
    putStrLn "dude"
    putStr $ map toUpper contents

The line I added is the putStrLn "dude" line.

I compiled it like this:

Without much discussion, here is some source code for a Scalaz 7 “Hello, world” example, using the Scalaz putStrLn function:

This short article shows how to write a command-line timer in Scala. There are simple ways to do this as just a regular Unix/Linux shell script, but I wanted to use Scala, in part so I could try to remember how to use the Java sound libraries. (I also initially thought about writing this is a GUI app, but decided to just make it a command-line app.)

I wanted a simple command-line timer, so I wrote one in Scala.

I just used this ImageMagick code in a Unix shell script to add a 20% black border to every image in the current directory:

To install the Python pip command on Debian Linux, a Raspberry Pi in my case, use this command:

sudo apt-get install python-pip

That command works with Python 2. For Python 3 install information see this link.

Note: I used that command so I could install the Python feedparser libary with this pip command:

sudo pip install feedparser

Java FAQ: How do I initialize/populate a static List (ArrayList, LinkedList) in Java?

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: