Skip to content

Instantly share code, notes, and snippets.

View ViewModelProvider.kt
// ViewModelのfactoryを毎回書くのが面倒
fun <T : ViewModel> viewModelFactory(creator: () -> T) =
object : ViewModelProvider.NewInstanceFactory() {
@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel> create(modelClass: Class<T>): T {
val viewModel = creator()
if (!modelClass.isAssignableFrom(viewModel::class.java)) {
error("unexpected modelClass. ${modelClass.simpleName}")
}
return viewModel as T
View mapNotNullToIterable.kt
// variations of filterNotNull that returns Iterable.
// it proxies iteration to avoid cost of create/update a list.
fun <T> Iterable<T?>.filterNotNullToIterable() =
object : Iterable<T> {
override fun iterator() = object : Iterator<T> {
val source = this@filterNotNullToIterable.iterator()
var item: T? = null
private fun read() {
while (source.hasNext()) {
item = source.next() ?: continue
View collectToChannel
// parser with recursive call
suspend fun simpleParser(ch: Channel<Char>, nest: Int=0 ) {
while (true) {
when (val a = ch.receiveOrNull()) {
null, ']' -> break
'[' -> simpleParser(ch, nest + 1 )
else -> println("$nest $a")
}
}
}
View gist:4f554f5bab049ec8a488fa66bb1ee372
javascript:(function(u,asin,m){asin=(m=u.match(/gp\/product(?:-details)?\/([^\/\?]+)/))?m[1]:(m=u.match(/dp(?:\/product-details)?\/([^\/\?]+)/))?m[1]:null;if(asin)location.href=('https://www.amazon.co.jp/dp/'+asin+'/')})(location.href);void(0);
@tateisu
tateisu / removePurgedDelivery.pl
Created May 26, 2020
remove deliveries to purged domain.
View removePurgedDelivery.pl
#!/usr/bin/perl
use strict;
use warnings;
use JSON::XS;
use Data::Dump qw(dump);
use DBI;
use Redis;
my $r = `docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mastodon1_db_backend_1`;
$r =~ /(\S+)/ or die "can't find db address. $r\n";
@tateisu
tateisu / recordHls.pl
Created May 18, 2020
あのサイトのHLS(TS)の録画
View recordHls.pl
#!/usr/bin/perl --
# usage: perl recordHls.pl (room_url)
use strict;
use warnings;
use LWP::UserAgent;
use JSON::XS;
use File::Path;
use MIME::Base64;
View testLog.md

help of parent commands

$ docker-compose run --rm web tootctl
Commands:
  tootctl accounts SUBCOMMAND ...ARGS             # Manage accounts
  tootctl cache SUBCOMMAND ...ARGS                # Manage cache
  tootctl domains SUBCOMMAND ...ARGS              # Manage account domains
  tootctl email-domain-blocks SUBCOMMAND ...ARGS  # Manage E-mail domain blocks
  tootctl emoji SUBCOMMAND ...ARGS                # Manage custom emoji
View foo.cs
public delegate Boolean WhenCondition(CanStartRoom arg);
public delegate Int64 WhenAction(CanStartRoom arg);
public delegate void WhenPre(CanStartRoom arg);
public class WhenNode {
public String? situation;
public WhenCondition condition;
public WhenAction action;
public WhenPre? preAction;
View CanStartRoom.cs
class CanStartRoom{
public static Tuple<Func<CanStartRoom, Boolean>, Func<CanStartRoom, Int64>> wc(Func<CanStartRoom, Boolean> c, Func<CanStartRoom, Int64> v)
=> whenCase( c, v );
private When<CanStartRoom,Int64> check = new When<CanStartRoom,Int64>(
(self) => self.dontOpen("fallback","マッチする条件がありません"),
wc(
(self) => self.garner.forceOpenReason != null,
(self) => self.willOpen( "forceOpen", self.garner.forceOpenReason ?? "?" )
View gist:1af488861ea0272f7111e2e7736b9c0b
ジェネリックメソッドの戻り値を T? と書けないC#8.0かっこ悪い
#nullable disable
public static T elementOrNull<T>(this List<T> list, Int32 index) {
try {
return list[ index ];
} catch (IndexOutOfRangeException) {
return default;
}
}