Skip to content

Instantly share code, notes, and snippets.

fun minesweeper(matrix: MutableList<MutableList<Boolean>>): MutableList<MutableList<Int>> {
val board = Array(matrix.size) { Array(matrix[0].size) { 0 } }
for (row in matrix.indices)
for (colum in matrix[0].indices) {
val surroundingCells = getSurroundingCells(row, colum)
val mines = calculateNumberFor(surroundingCells, matrix)
board[row][colum] = mines
Log.d("minesweeper for $row,$colum", mines.toString())
}
return board.map { it.toMutableList() }.toMutableList()
package com.syphyr.rezoom.ui.theme.widgetStyles
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
@Composable
fun AppText(fontSize: FontSize = FontSize.Medium, fontFamily: FontFamily = FontFamily.Body, text: String, color: Color = Color.Unspecified) {
/**
* Most of the code in the RxJ example was to create an observable which emits items with a random delay.
* This is more concisely done using flows.
*/
val flow1 = (1..10).asFlow().onEach {
Log.d(TAG,"$it - generated")
delay(randomDelay())
}
override fun onCreate(savedInstanceState: Bundle?) {
override fun onCreate(savedInstanceState: Bundle?) {
randomDelayedValues()
.debounce(3,TimeUnit.SECONDS)
.subscribe({ Log.d("DebounceTest", "$it emission") }, {}, { Log.d("DebounceTest", "Completed") })
}
//This observable will emit values spaced by random delays
fun randomDelayedValues()= Observable.create<Int> { emitter ->
var number = 1
timer(randomDelay()) {
val flow1 = (1..10).asFlow().onEach { delay((1..5).random() * 1000L) }
val flow2 = (1..10).asFlow().onEach { delay((1..5).random() * 1000L) 1
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val combinedFlow = flow1.combine(flow2){
one,two->
"$one $two"
}
lifecycleScope.launchWhenCreated {
fun stream1() = Observable.intervalRange( 1,10,0,(1..5).random().toLong(), TimeUnit.SECONDS)
fun stream2() = Observable.intervalRange( 1,10,0,(1..5).random().toLong(), TimeUnit.SECONDS)
fun main()
{
Observable.combineLatest(stream1(), stream2()) { one, two->
"$one and $two"
}.observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe({
Log.d(TAG, "$it - emitted")
}, {
val flow1=(1..10).asFlow().onEach { delay((1..5).random() * 1000L) }
val flow2=(1..10).asFlow().onEach { delay((1..5).random() * 1000L) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val combinedFlow = flow1.zip(flow2) { one, two ->
"$one $two"
}
lifecycleScope.launchWhenCreated {
fun stream1() = Observable.intervalRange( 1,10,0,(1..5).random().toLong(), TimeUnit.SECONDS)
fun stream2() = Observable.intervalRange( 1,10,0,(1..5).random().toLong(), TimeUnit.SECONDS)
fun main()
{
Observable.zip(stream1(), stream2()) { one, two ->
"$one and $two"
}.observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe({
Log.d(TAG, "$it - emitted")
@mahmed1987
mahmed1987 / gist:837293177a6ac1bb6a517173d13090b7
Last active November 23, 2021 07:45
A coroutine with only the essentials
val scope = CoroutineScope(Dispatchers.IO) // losely akin to subscribeOn
fun onCreate()
{
scope.launch { // launch is one of the coroutine builder
val students = getStudentsFromApi() // this is a suspend function
withContext(Dispatchers.Main) // losely akin to observeOn - But keep in mind there is no concept of "observation" in Coroutines
{
//We are on the main thread here
showToast("Received ${students.size} students")
}
@mahmed1987
mahmed1987 / gist:628946fa823c2d7522160c76877a7136
Last active November 22, 2021 10:29
The structure of a coroutine
(the scope).launch{
delay(1000L)
print("Hey , I just ran off the main. Technically I ran where the scope asked me to run")
}