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
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]
/**
* 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)
}
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);
}
}
class Post {
final String? content;
const Post([this.content]);
}
class UserProfile {
final List<Post> posts;
final int followersCount;
const UserProfile(this.posts, this.followersCount);
import 'package:rxdart/rxdart.dart';
void main() async {
final (name, year) = await Stream.fromFuture(Future.value("a"))
.zipWith(Stream.fromFuture(Future.value(2)), (a, b) => (a, b))
.first;
print(name);
print(year);
}
/// 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;
///
class GsonConverterFactory2(private val gson: Gson = Gson()) : Converter.Factory() {
override fun responseBodyConverter(type: Type, annotations: Array<Annotation>, retrofit: Retrofit): Converter<ResponseBody, *> {
return GsonResponseBodyConverter2(gson, gson.getAdapter(TypeToken.get(type)))
}
override fun requestBodyConverter(type: Type, parameterAnnotations: Array<Annotation>, methodAnnotations: Array<Annotation>, retrofit: Retrofit): Converter<*, RequestBody> {
return GsonRequestBodyConverter2(gson, gson.getAdapter(TypeToken.get(type)))
}
}