This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} |
OlderNewer