Skip to content

Instantly share code, notes, and snippets.

@jsaund
jsaund / Stats.scala
Last active October 11, 2015 02:08
A collection of statistical operations performed in Scala using functional principles.
object Stats {
def simpleMovingAverage1(xs: List[Dobule], w: Int): List[Double] = {
xs sliding w map (_.sum) map (_ / w) toList
}
def simpleMovingAverage2(xs: List[Double], w: Int): List[Double] = {
def movingSum(xs: List[Double], w: Int): List[Double] = w match {
case 0 => throw new IllegalArgumentException("Window size must be greater than 0")
case 1 => xs
@jsaund
jsaund / Strings.scala
Last active October 15, 2015 04:49
A collection of string operations using Scala's functional principles.
object Strings {
def isAnagram(s1: String, s2: String): Boolean = {
if (s1 == null || s2 == null || s1.length != s2.length) return false
s1.zip(s2).foldLeft(0){ case (result, (left, right)) => result ^ (left ^ right) } == 0
}
def isAnagram2(s1: String, s2: String): Boolean = {
val m1 = s1 groupBy(c => c) mapValues(_.length)
val m2 = s2 groupBy(c => c) mapValues(_.length)
@jsaund
jsaund / ViewUtils.java
Last active March 2, 2016 06:47
A utility to dump the Android view hierarchy and detect duplicate resource IDs in the hierarchy
public class ViewUtils {
private static final String TAG = "ViewUtils";
public static void dumpViewHierarchy(ViewGroup root) {
final Set<String> visitedResNames = new HashSet<>();
walkNode(root, root.getResources(), visitedResNames);
}
private static void walkNode(ViewGroup node, Resources resources, Set<String> visitedResNames) {
final String resName = getResourceName(node.getId(), resources);
@jsaund
jsaund / UIHandler.java
Last active March 15, 2016 20:18
A safer Android Handler for use with Activities or other components that require posting back on to the UI thread via an Activity reference.
public abstract class UIHandler<T extends Activity> extends Handler {
private final WeakReference<T> mRef;
public UIHandler(T a) {
mRef = new WeakReference<>(a);
}
@Override
public void handleMessage(Message msg) {
final T a = mRef.get();
public class ImageFetcherActivity extends AppCompactActivity {
class WorkerThread extends Thread {
void fetchImage(String url) {
// network logic to create and execute request
handler.post(new Runnable() {
@Override
public void run() {
imageView.setImageBitmap(image);
}
});
public class ImageFetcherAltActivity extends AppCompactActivity {
class WorkerThread extends Thread {
void fetchImage(String url) {
handler.sendEmptyMessage(MSG_SHOW_LOADER);
// network call to load image
handler.obtainMessage(MSG_SHOW_IMAGE, imageBitmap).sendToTarget();
}
}
class UIHandler extends Handler {
static class UIHandler extends Handler {
private final WeakReference<ImageFetcherActivity> mActivityRef;
UIHandler(ImageFetcherActivity activity) {
mActivityRef = new WeakReference(activity);
}
@Override
public void handleMessage(Message msg) {
final ImageFetcherActivity activity = mActivityRef.get();
static class UIHandler extends Handler {
private final WeakReference<ImageFetcherActivity> mActivityRef;
UIHandler(ImageFetcherActivity activity) {
mActivityRef = new WeakReference(activity);
}
@Override
public void handleMessage(Message msg) {
final ImageFetcherActivity activity = mActivityRef.get();
public class MainActivity extends AppCompatActivity {
private static final String IMAGE_URL = "https://www.android.com/static/img/android.png";
private static final int MSG_SHOW_PROGRESS = 1;
private static final int MSG_SHOW_IMAGE = 2;
private ProgressBar progressIndicator;
private ImageView imageView;
private Handler handler;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "Ping";
private Handler handler;
class PingHandler extends Handler {
@Override
public void handleMessage(Message msg) {
Log.d(TAG, "Ping message received");
}