Skip to content

Instantly share code, notes, and snippets.

@Groxx
Groxx / thread_safe_race.go
Last active July 14, 2017 18:30
Golang channel closes are threadsafe?
/*
The race detector complains about code like this:
c := make(chan struct{}, 1)
go func() { c <- struct{}{} }()
go func() { close(c) }()
go func() { _ = <- c }()
But I have yet to see this test suite fail (which would imply undetectably losing data).
If it doesn't ever lose data, is it actually worthy of a race detector failure?
Obviously it's risky for most code, as it can panic and cannot be synchronized e.g. while

say you have a string: "hello". this is the sequence of characters:

[h, e, l, l, o]

spread it out a bit so the string looks like this, with "|" as the divider and the end-markers.

| h | e | l | l | o |
  0   1   2   3   4     <- these are the indexes of the characters
@Groxx
Groxx / Diff.java
Created June 10, 2015 21:29
"find differences between data sets X and Y" helper
public class Diff<Type> {
public interface Finder<Type> {
/** Return true to denote a "represents the same thing" match (do not compare contents) */
boolean find(Type left, Type right);
}
public interface Equality<Type> {
boolean equals(Type left, Type right);
}
#!/bin/bash
# Copies anything sent over stdout.
# e.g. `screencap -p` or `run-as com.whatever cat databases/file.db`
if [ $# -ne 2 ]; then
echo "Needs two arguments."
echo "Example usage: "
echo " acp 'run-as com.whatever cat databases/file.db' file.db"
echo " acp 'screencap -p' screen.png"
public class GridDividers extends RecyclerView.ItemDecoration {
private final Rect viewRect = new Rect();
private final Paint stroke = new Paint();
private final int padding;
/**
* @param parentPadding should be 1/2 the padding desired between views, as views will be surrounded by the
* passed padding, causing it to double up on all edges. This is also used to determine
* where to draw a fake padding border to hide our trickery.
* @param backgroundColor background color used by the parent view(s), used to draw a border to look like normal
private class MyPanListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
mDrawingMatrix.postTranslate(distanceX, distanceY);
invalidate();
return true;
}
}
@Groxx
Groxx / GridDividers.java
Created March 27, 2015 00:37
RecyclerView item decorator to mimic GridView's dividers
private static class GridDividers extends RecyclerView.ItemDecoration {
private final Rect viewRect = new Rect();
private final Paint stroke = new Paint();
private final int padding;
/**
* @param parentPadding should be 1/2 the padding desired between views, as views will be surrounded by the
* passed padding, causing it to double up on all edges. This is also used to determine
* where to draw a fake padding border to hide our trickery.
*/
GridDividers(final int parentPadding, final int backgroundColor) {
### Keybase proof
I hereby claim:
* I am groxx on github.
* I am groxx (https://keybase.io/groxx) on keybase.
* I have a public key whose fingerprint is BF5A C9C8 375F 1D63 52BA F6CC 0125 3DF1 B093 7470
To claim this, I am signing this object:
>> h = {
:jpn=>"日本語",
:abc=>{:jpn=>"日本語"}.with_indifferent_access,
:xyz=>{:jpn=>"日本語"}
}
>> JSON.generate(h)
# => "{\"abc\":{\"jpn\":\"\\u65e5\\u672c\\u8a9e\"},\"jpn\":\"日本語\",\"xyz\":{\"jpn\":\"日本語\"}}"
>> http://cl.ly/image/2X0l0s1y0v2T
@Groxx
Groxx / UITableView+UpdateBlock.m
Created July 31, 2012 22:26
Table updates w/w/o animations
@interface UITableView (UpdateBlock)
- (void)updates:(void(^)())updates;
- (void)updatesWithoutAnimations:(void(^)())updates;
@end
@implementation
- (void)updates:(void(^)())updates
{
[self beginUpdates];
updates();