Skip to content

Instantly share code, notes, and snippets.

View bergwerf's full-sized avatar

Herman Bergwerf bergwerf

View GitHub Profile
@bergwerf
bergwerf / buffered_string_sink.dart
Created September 28, 2018 04:30
Dart buffered StringSink
/// Buffered stream sink that uses a string buffer.
class BufferedStringSink implements StringSink {
final buffer = new StringBuffer();
final int bufferThreshold;
final StringSink target;
BufferedStringSink(this.target, {this.bufferThreshold: 10000});
void flush() {
target.write(buffer);
@bergwerf
bergwerf / main.dart
Last active September 21, 2018 12:27
Dart WhereIterable does not cache
void main() {
final mappedIterable = ['a', 'b', 'c', 'd'].map((ch) {
print('mapped $ch');
return ch;
}).where((ch) {
return ch != 'a';
});
if (mappedIterable.isNotEmpty) {
print('iterable is non-empty');
print(mappedIterable.first);
@bergwerf
bergwerf / IDEAS.md
Created August 24, 2018 14:44
Ideas for cool Dart utilities

IDEAS

  • Wrapper of List<List<T>> as List2D<T>
  • List shaped tree processing utilities
@bergwerf
bergwerf / main.dart
Created August 21, 2018 12:59
Dart Iterable.where and Iterable.map lazyness test
void main() {
final data = new List.generate(100, (i) => i);
final result = data.where((i) {
print('where $i');
return i > 0 && i % 10 == 0;
}).map((i) {
print('map $i');
return i / 10;
});
print('==========');
@bergwerf
bergwerf / gcov.css
Created August 8, 2018 12:44
A brief attempt at a new minimalist LCOV genhtml theme
body {
font-family: monospace;
}
.title {
font-size: 1em;
font-weight: bold;
text-decoration: underline;
padding: 1em;
}
@bergwerf
bergwerf / main.dart
Last active December 9, 2017 02:44
Currying in Dart
typedef R Fn1<R, T1>(T1 arg1);
typedef R Fn2<R, T1, T2>(T1 arg1, T2 arg2);
Fn1<Fn1<R, T2>, T1> curry12<R, T1, T2>(Fn2<R, T1, T2> fn) {
return (T1 arg1) => (T2 arg2) => fn(arg1, arg2);
}
Fn1<Fn1<R, T1>, T2> curry21<R, T1, T2>(Fn2<R, T1, T2> fn) {
return (T2 arg2) => (T1 arg1) => fn(arg1, arg2);
}
@bergwerf
bergwerf / main.dart
Last active December 6, 2017 00:46
Dart pattern matching like Rust
typedef bool Case<T>(T input);
Case<num> gt(num than) => (num x) => x != null && x > than;
Case nil = (instance) => instance == null;
O match<T, O>(T input, Map<dynamic, Function> cases) {
for (final _case in cases.keys) {
if (_case == input || (_case is Function && _case(input) == true)) {
return cases[_case]();
}
}
@bergwerf
bergwerf / code.dart
Created September 30, 2017 17:01
Packing and unpacking integer lists into a single integer list
/// Unpack an integer list into a list of integer lists.
List<List<int>> _unpackIntLists(List<int> input) {
final output = new List<List<int>>();
for (var i = 0; i < input.length;) {
final length = input[i++];
output.add(input.sublist(i, i + length));
i += length;
}
return output;
}
@bergwerf
bergwerf / clipboard.dart
Created April 28, 2017 11:54
Copy to clipboard for Dart
/// A hack to copy a string to the clipboard.
bool _copyToClipboardHack(String text) {
final textarea = new TextAreaElement();
document.body.append(textarea);
textarea.style.border = '0';
textarea.style.margin = '0';
textarea.style.padding = '0';
textarea.style.opacity = '0';
textarea.style.position = 'absolute';
textarea.readOnly = true;
@bergwerf
bergwerf / checkbox.scss
Created April 9, 2017 18:05
Simple CSS checkbox
// Copyright (c) 2017, Herman Bergwerf. All rights reserved.
// Use of this source code is governed by a MIT-style license
// that can be found in the LICENSE file.
.checkbox {
font-family: KaTeX_Main;
user-select: none;
padding: .3em;
&:before {