Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View kikuchy's full-sized avatar

Hiroshi Kikuchi kikuchy

View GitHub Profile
@kikuchy
kikuchy / jobqueue.go
Last active July 5, 2023 15:06
Simple JobQueue for goroutines.
import "sync"
type JobQueue struct {
limit int
wg sync.WaitGroup
mu sync.Mutex
running int
waiting []func()
}
@kikuchy
kikuchy / MainActivity.kt
Created June 17, 2022 10:33
雑にAndroidでNFC-F (FeliCa)読み取りをしたときのコード 参考 -> https://www.kenichi-odo.com/articles/2020_10_08_read-suica-by-android
package com.example.nfcftest
import android.nfc.NfcAdapter
import android.nfc.Tag
import android.nfc.tech.NfcF
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
@kikuchy
kikuchy / broadcaster.go
Last active April 13, 2022 12:47
Simple broadcast mechanism for multiple goroutines and a channel.
type Broadcaster[T any] struct {
chans []chan T
}
func (b *Broadcaster[T]) Listen() chan T {
c := make(chan T)
b.chans = append(b.chans, c)
return c
}
@kikuchy
kikuchy / enumwhen.dart
Created February 17, 2022 11:59
Enum live template with when method like freezed sealed classes
enum $NAME$ {
$V1$,
$V2$,
}
extension $NAME$X on $NAME$ {
T when<T>({
required T Function() $V1$,
required T Function() $V2$,
}) {
@kikuchy
kikuchy / combine_latest.dart
Last active November 15, 2021 17:14
Just a little study for making the function like `combineLatest2` of RxDart
// https://gist.github.com/kikuchy/d96a45d0b22b0baa437ccca69ad6679b
import 'dart:async';
typedef Combinator<T1, T2, R> = R Function(T1, T2);
Stream<R> combineLatestN<R>(
List<Stream<dynamic>> streams, R Function(List<dynamic>) combinator) {
final combined = StreamController<R>();
final subscriptions = <StreamSubscription>{};
@kikuchy
kikuchy / main.dart
Last active March 18, 2021 12:50
null-safety explanation
import "package:flutter/material.dart";
final VoidCallback? nullableFunction = null;
final Map<String, String>? nullableMap = null;
void main() {
// これはnon-null
final int a = 1;
// なのでこれは静的型検査でエラーになる
enum ColorExpression {
rgb,
cymk,
}
abstract class ColorValue {
ColorExpression get expression;
}
class RgbColor extends ColorValue {
@kikuchy
kikuchy / main.c
Created October 4, 2017 07:28
GoからCの関数をコールバック関数として呼び出したい。けどGoからCの関数ポインタを関数として呼び出すことはできない。のでなんとかした。
#include <stdio.h>
#include "worker.h"
void cb(int hoge) {
printf("value: %d\n", hoge);
}
int main() {
DoSomeCallback(cb);
}
@kikuchy
kikuchy / main.dart
Created May 15, 2019 08:04
Collection If/For (from Dart 2.3) also can be used in Set literal! 🎉
enum Hoge {
hoge, fuga, moge, piyo,
}
void main() {
Set s = {
for (Hoge h in Hoge.values)
if (h != Hoge.hoge) h
};
print(s);
@kikuchy
kikuchy / main.dart
Created April 5, 2019 06:02
When Completable#isCompleted will be true if you give running Future to Completable#complete?
import "dart:async";
void main() async {
final job = Future.delayed(Duration(seconds: 2), () => print("ended"));
final completer = Completer()..complete(job);
print(completer.isCompleted); // fal....true!?!?!?
await job;
print(completer.isCompleted); // true, of course.
}