Skip to content

Instantly share code, notes, and snippets.

@zoechi
zoechi / main.dart
Created April 10, 2015 10:02
SO 29331079 yield from within a callback
import 'dart:async';
class SqlResultSet {
List rows;
SqlResultSet(this.rows);
}
Future _runInTxn(txn(Transaction x)) {
print('_runInTxn');
return new Future.value(txn(new Transaction()));
@zoechi
zoechi / main.dart
Last active August 29, 2015 14:18
SO 29320012 Do streams buffer
import 'dart:async';
Stream<int> a() async* {
for (int i = 1; i <= 10; ++i) {
print('yield $i');
yield i;
}
}
main() {
@zoechi
zoechi / main.dart
Last active August 29, 2015 14:18
SO 29565067 Inverse map
// 29565067
Map f = {
0 : 0,
1 : 1,
2 : 0,
3 : 1,
4 : 0,
5 : 1
};
@zoechi
zoechi / main.dart
Last active August 29, 2015 14:18
SO 29567236 Enum as default value
// 29567236
class Status {
static const off = const Status._(0);
static const on = const Status._(1);
final int value;
const Status._(this.value);
}
doSomething1([Status status = Status.off]) {
@zoechi
zoechi / main.dart
Created April 11, 2015 09:25
setter return type
import 'dart:async' show Future, Stream;
class SomeClass {
int _someValue;
void set someValue(int val) =>
new Future.delayed(new Duration(milliseconds: 500), () => _someValue = val);
Future set someValue(int val) =>
new Future.delayed(new Duration(milliseconds: 500), () => _someValue = val);
// SO 29629868
main() {
var languages = <String>["dart", "javascript", "coffeescript", "typescript"];
var tmpList = languages.map((e) => e.toUpperCase()).toList();
print(tmpList);
}
@zoechi
zoechi / main.dart
Last active August 29, 2015 14:19
set.contains with custom equals
// SO 29567322
class Action {
final Function function;
final String description;
Action(this.function, this.description);
call() => function();
int get hashCode => description.hashCode;
@zoechi
zoechi / main.dart
Last active August 29, 2015 14:19
deduplicator transformer
// SO 29681639
import 'dart:convert';
import 'dart:async' show Future, Stream;
import 'dart:math' show Random;
final rnd = new Random();
main() {
lineStream()
.transform(new Utf8Decoder())
// not tested with binding expressions (port of http://stackoverflow.com/a/31320384/217408)
// probably needs bound values set to `template`
injectBoundHtml(highlightedValue, $['container']);
// TODO(zoechi) create feature request to get this into the `Polymer` class
get _polymerBase {
final JsObject _polymer = context['Polymer'];
return (o) {
return o is JsObject ? o : new JsObject.fromBrowserObject(o);
}(_polymer['Base']);
@zoechi
zoechi / List open ports
Created May 20, 2013 13:40
List all ports and the apps which are listening (-l listening, -t TCP, -p show process name)
ss -ltp