Skip to content

Instantly share code, notes, and snippets.

View yongjhih's full-sized avatar
🏠
Working from home

Andrew Chen yongjhih

🏠
Working from home
View GitHub Profile
abstract class Preference<T> {
String get key;
T? get value;
set value(T? newValue);
T? get();
Future<bool> set(T? newValue);
Future<bool> remove();
extension FutureZipX<T> on Future<T> {
Future<(T, T2)> zipWith<T2>(Future<T2> future2) async {
late T result1;
late T2 result2;
await Future.wait(
[then((it) => result1 = it), future2.then((it) => result2 = it)]);
return (result1, result2);
}
}
fun <T: Any> byAtomic(initialValue: T) = object : ReadWriteProperty<Any?, T> {
val ref = AtomicReference(initialValue)
override fun getValue(thisRef: Any?, property: KProperty<*>): T = ref.get()
override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) = ref.set(value)
}
fun <T: Any?> byAtomic(initialValue: T? = null) = object : ReadWriteProperty<Any?, T?> {
val ref = AtomicReference<T?>(initialValue)
override fun getValue(thisRef: Any?, property: KProperty<*>): T? = ref.get()
override fun setValue(thisRef: Any?, property: KProperty<*>, value: T?) = ref.set(value)
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
Stream<Iterable<Map<String, dynamic>?>?> fetchGitHubRepos(String? url) async* {
while (url != null) {
print("$url");
final response = await http.get(Uri.parse(url));
if (response.statusCode == 200) {
/// Weights of [0,0] anchor:
/// | j j j j j
/// | 0 1 2 3 4
/// ----+-----------
/// i 0 | 0 1 2 3 4
/// i 1 | 1 2 2 3 4
/// i 2 | 2 2 3 3 4
/// i 3 | 3 3 3 4 4
/// i 4 | 4 4 4 4 5
///
const Generator = Object.getPrototypeOf(function* () { });
Generator.prototype.map = function* (mapper) {
for (const it of this) {
yield mapper(it);
}
};
Generator.prototype.filter = function* (predicate) {
for (const it of this) {
if (predicate(it)) yield it;
}
/// | j j j j j
/// | 0 1 2 3 4 < j-index
/// ----+----------- round-th
/// i 0 | 0 1 2 3 4 round-0: [0,0]
/// i 1 | 1 2 3 4 5 round-1: [0,1][1,0]
/// i 2 | 2 3 4 5 6 round-2: [0,2][1,1][2,0]
/// i 3 | 3 4 5 6 7 round-3: [0,3][1,2][2,1][3,0]
/// i 4 | 4 5 6 7 8 round-4: [0,4][1,3][2,2][3,1][4,0]
/// round-5: [1,4][2,3][3,2][4,1]
/// round-6: [2,4][3,3][4,2]
/// For dart 2:
///
/// ```
/// import 'package:tuple/tuple.dart';
///
/// Future<Tuple2<T1, T2>> zipAsync<T1, T2>(
/// Future<T1> future1, Future<T2> future2) async {
/// late T1 result1;
/// late T2 result2;
///
/**
* Compatibility
*
* See Also: androidx.core.content.res.use
*/
@OptIn(ExperimentalContracts::class)
inline fun <T: TypedArray, R> T.use(block: (T) -> R): R {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
class Post {
final String? content;
const Post([this.content]);
}
class UserProfile {
final List<Post> posts;
final int followersCount;
const UserProfile(this.posts, this.followersCount);