Skip to content

Instantly share code, notes, and snippets.

View bananaumai's full-sized avatar
🙄

bnn bananaumai

🙄
  • MODE, Inc.
  • Tokyo
View GitHub Profile
@bananaumai
bananaumai / Main.kt
Last active March 24, 2022 23:55
Coroutines delay and Future get
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.util.concurrent.CompletableFuture
fun main() {
val future = CompletableFuture<Unit>()
doSuspend {
@bananaumai
bananaumai / throttle.kt
Created July 31, 2020 02:26
Throttling flow
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
fun <T> Flow<T>.throttleByTime(wait: Long): Flow<List<T>> = flow {
val holder = emptyList<T>().toMutableList()
var nextTime = 0L
collect {
val curTime = System.currentTimeMillis()
@bananaumai
bananaumai / ActivityMain.java
Created January 17, 2020 05:50
QuickStart MODE Gateway SDK for Android
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ModeGatewayClient modeGatewayClient = initModeGateway();
Button eventButton = findViewById(R.id.event_button);
@bananaumai
bananaumai / AccelerationFlow.kt
Created December 3, 2019 09:37
Android Accelerometer to event (with calibration steps)
@ExperimentalCoroutinesApi
private fun accelerationFlow(context: Context): Flow<Acceleration> = channelFlow {
val sensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager
val listener = object : SensorEventListener {
private val sampleNumber = 20
private val samples: MutableList<Acceleration.Coordinate> = mutableListOf()
private var offset: Acceleration.Coordinate? = null
@bananaumai
bananaumai / MainActivity.kt
Created October 11, 2019 14:42
Sensor Flow with lifecycleScope
@ExperimentalCoroutinesApi
class MainActivity : AppCompatActivity() {
private var working = false
private var job: Job? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button = findViewById<Button>(R.id.button)
@bananaumai
bananaumai / fib.rs
Last active March 24, 2019 15:02
rust code example - fibonacci generator
fn fib(n: usize) -> Vec<u64> {
let mut fibs = Vec::new();
while fibs.len() < n {
let current = match fibs.len() {
0 => 0,
1 => 1,
_ => {
fibs.iter().rev().take(2).sum()
}
@bananaumai
bananaumai / prime.rs
Last active March 24, 2019 15:01
rust code example - find prime numbers
use std::collections::HashMap;
fn primes(n: usize) -> Vec<usize> {
if n < 2 {
return vec![];
}
if n == 2 {
return vec![2];
}
o :: Ord a => a -> a -> Bool
o x y = x >= y
e :: Eq a => a -> a -> Bool
e x y = x == y
f :: Ord a => (a -> a -> Bool) -> a -> a -> String
f cmp x y = if cmp x y then "YES" else "NO"
main :: IO ()
@bananaumai
bananaumai / file0.js
Last active October 12, 2017 02:38
Slack Realtime Message APIをRx.Observableにする ref: http://qiita.com/bananaumai/items/fbcc0cb86208cce5d379
var Rx = require('rx');
var RtmClient = require('@slack/client').RtmClient;
var CLIENT_EVENTS = require('@slack/client').CLIENT_EVENTS;
var botToken = process.env.SLACK_BOT_TOKEN;
var rtm = new RtmClient(botToken);