copy

How to paste and load blocks of code in the Scala REPL

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 14.2, “How to paste and load blocks of code in the Scala REPL.”

Problem

You want to experiment with some code in the Scala REPL, and typing it in or trying to paste it into the REPL won’t work.

How to copy files to an Android emulator’s data directory with ‘adb push’

As an Android developer, you can normally use the adb push command to copy files from your computer’s hard drive to an Android device. However, I just ran into a problem where I couldn’t copy files to my Android emulator’s “data” directory, i.e., the /data/data filesystem. When I tried to copy a file using this command:

$ adb push foo.jpg /data/data/com.alvinalexander.myapp/files

I got this Android error:

A Scala case class ‘copy’ method example

Scala FAQ: When you create a case class in Scala, a copy method is generated for your case class. What does this copy method do?

In short, it lets you make a copy of an object, where a “copy” is different than a clone, because with a copy you can change fields as desired during the copying process. The copy method is important in functional programming, where values (val) are immutable.

A Java deep clone (deep copy) example

While interviewing for computer programming positions in Boulder, Colorado, I found that most interviewers ask questions about Java serialization. After being asked about serialization for the third time recently, I suddenly remembered an old Java deep clone hack that takes advantage of serialization.

The basic idea is this:

Java: How to copy an image to the clipboard

Java Swing clipboard FAQ: How do I copy an image to the clipboard in a Java/Swing application?

Last night I needed to do just this, get an image that I'm currently displaying in a Java JFrame, and copy that image to the clipboard. I won't discuss the solution here too much, other than to say that in the example source code below, a MetaFrame is a customized subclass of a JFrame, and when I call the getCurrentImage() function, it simply returns a Java Image type.

Here's the source code:

Use sed to modify files in place

sed command FAQ: How can I use the Unix/Linux sed command to edit (modify) files in place?

The short answer is that you just need to use the -i or --in-place sed arguments, as shown in the sed man page:

-i[SUFFIX], --in-place[=SUFFIX]

edit files in place (makes backup if extension supplied)

Then, if you have a file named 'hello.txt' with contents like this:

jello, world
this is a test

you can then run a sed command like this to modify that file: