gildor
08/23/2018, 10:22 AMbeholder
08/26/2018, 6:19 PM@Suppress("NOTHING_TO_INLINE")
inline infix fun Int.hasBits(bits: Int): Boolean = (this and bits) != 0
How would you name it?christophsturm
08/29/2018, 1:47 PMobject Jackson : ObjectMapper() {
init {
enable(SerializationFeature.INDENT_OUTPUT)
....
}
}
beholder
09/06/2018, 12:32 PMinline fun <reified T> Any._as() = this as T
Or it's just noise?tipsy
09/13/2018, 11:25 PMprivate val converters = mapOf<Class<*>, (String) -> Any>(
Int::class.java to { s -> s.toInt() },
Integer::class.java to { s -> s.toInt() },
Double::class.java to { s -> s.toDouble() },
Long::class.java to { s -> s.toLong() },
Date::class.java to { s -> Date(s) }
)
fun <T> getAs(clazz: Class<T>, converter: ((String) -> T)? = null): T {
val c = converter ?: converters[clazz] ?: throw IllegalArgumentException("Can't auto-cast to $clazz. Add a converter as a second argument.")
return try {
c.invoke(value)
} catch (e: Exception) {
...
} as T
}
Daniel Tam
09/19/2018, 5:44 AMSudhir Singh Khanger
09/25/2018, 4:59 PMclass CustomApplication: Application() {
companion object {
private var INSTANCE: CustomApplication? = null
@JvmStatic
fun get(): CustomApplication = INSTANCE!!
}
override fun onCreate() {
super.onCreate()
INSTANCE = this
}
}
Or
class CustomApplication : Application() {
companion object {
lateinit var instance: CustomApplication
private set
}
override fun onCreate() {
super.onCreate()
instance = this
}
}
Not sure if this is more appropriate for #android. Which one would you prefer for creating an Android Application class? And why?tipsy
09/29/2018, 6:46 PMValidator
class, which can be turned into a TypedValidator
class by calling asInt
, asDouble
, etc: https://github.com/tipsy/javalin/blob/master/src/main/java/io/javalin/validation/Validator.kt
the problem is that i have some duplicated functionality that i'd like to get rid of, but i can't think of any (simple) way of doing thatSam
10/05/2018, 3:59 PMdialog = with( AlertDialog.Builder( this ) ) {
setTitle( "foo" )
setMessage( "bar" )
setCancelable(true)
create()
}.also {
it.show()
}
or
dialog = AlertDialog.Builder( this ).run {
setTitle( "foo" )
setMessage( "bar" )
setCancelable(true)
create()
}.also {
it.show()
}
Joris PZ
10/13/2018, 7:13 PM.run()
method to start its internal loop:
class MyActor {
suspend fun run() {
while(true) {
select<Unit> {
one.onReceive { ... }
two.onReceive { ... }
}
}
}
}
Now, I am wondering about two things. First, where do I define the necessary channels? Do I do it in some configuration code and inject them as properties through a constructor, like so:
class MyActor(private val one: ReceiveChannel<Query>, private val two: ReceiveChannel<Command>)
Or do I define them in the actor itself, and expose them for outside clients to use?
class MyActor {
private val _one = Channel<Query>()
val one: SendChannel<Query>
get() = _one
}
My second question is related - is it a good idea (or even idiomatic) to use Channels to define the API to my actor? Or is it better/more common to offer message functions hiding the channels for the client, like
suspend fun query(query: Query) {
one.send(query)
}
Thanks for any feedback!arve
10/18/2018, 8:43 AMdata class Record(val first: String, val second: String, val third: String)
val input = listOf(
Record("a", "aa", "aaa"),
Record("a", "aa", "aab"),
Record("a", "ab", "aba"),
Record("b", "ba", "baa"),
Record("b", "ba", "bab"),
Record("b", "bb", "bba"),
Record("b", "bb", "bbb")
)
val expected = mapOf(
"a" to mapOf(
"aa" to listOf("aaa", "aab"),
"ab" to listOf("aba")
),
"b" to mapOf(
"ba" to listOf("baa", "bab"),
"bb" to listOf("bba", "bbb")
)
)
How would you approach it? Currently I'm doing
input.groupBy({ it.first })
.mapValues {
it.value.groupBy({ it.second }, { it.third })
}
, but nesting the `groupBy`s feels less than ideal, especially if you consider more than 2 levels.. Any suggestions for better approaches?tipsy
10/26/2018, 2:07 PMwhen (targetHandler) {
is HandlerCollection -> targetHandler.apply { addHandler(defaultHandlers) }
is HandlerWrapper -> targetHandler.apply { handler = defaultHandlers}
}
and
(targetHandler as? HandlerCollection)?.apply { addHandler(defaultHandlers) }
(targetHandler as? HandlerWrapper)?.apply { handler = defaultHandlers }
oday
10/31/2018, 3:04 PMval newFragment: Fragment? = if (fragment is CarSearchFragment? && fragment == null) {
CarSearchFragment()
} else if (fragment is CarValuationFragment? && fragment == null) {
CarValuationFragment()
} else if (fragment is HotDealsFragment? && fragment == null) {
HotDealsFragment()
} else if (fragment is FavoritesFragment? && fragment == null) {
FavoritesFragment()
} else {
fragment
}
kingsley
11/04/2018, 4:50 PMif an entry isn’t in the base map, it won’t exist in the final oneYes. Which is why I iterated through
mapOverride
instead. This is a union of both maps with the assumption that they only contain sets/other values and a key always points to values of similar type (or null) in both maps. Entries in mapOverride
are always preferred except for sets where both will get merged
I suppose that is what you’re trying to achieve?Omar Mohamed
11/04/2018, 4:54 PMmersan
11/04/2018, 11:01 PMfun <T, B> distinct(body: (T) -> B): (T) -> B {
val lastInput = AtomicReference<T>()
val lastReturn = AtomicReference<B>()
return {
if (lastInput.get() != it) {
lastInput.set(it)
lastReturn.set(body(it))
}
lastReturn.get()
}
}
oday
11/13/2018, 2:36 PMursus
11/17/2018, 5:39 PMwhen state == Activated
when action == MuteMic
state = ActivatedMuteMicPending
effect(Effect.DoMuteMic)
state = Activated
sealed class State {
object Activated,
object ActivatedMuteMicPending
}
im not sure if this is good designBMG
11/20/2018, 12:41 PMval updatedDataElementsOne = removeExtraElements(info)
val updatedDataElementsTwo = updateExistingElements(updatedDataElementsOne)
return populateEmptyElements(updatedDataElementsTwo)
zokipirlo
11/21/2018, 12:48 PMDaniel
11/22/2018, 3:15 PMobject TwoLikelyVariablesMatcher : CondensingMatcher {
override fun matches(operands: Terms): Boolean {
operands.filterIsInstance<AnyVariable>()
.groupBy { it.unsignedName }
.forEach { (_, variablesWithSameName) ->
if(variablesWithSameName.size > 1) return true
}
return false
}
override fun matches(source: Terms, target: Terms): Boolean {
TODO("not implemented")
}
}
@Test
fun `A two likely variables matcher matches with operands containing exactly two likely variables`() {
// GIVEN
val operands = termsOf(
PositiveVariable("x"),
mock(),
PositiveVariable("y"),
mock(),
NegativeVariable("x"))
// THEN
assertTrue(TwoLikelyVariablesMatcher.matches(operands))
}
@Test
fun `A two likely variables matcher doesn't match with operands containing zero likely variables`() {
// GIVEN
val operands = termsOf(
PositiveVariable("x"),
PositiveVariable("y"),
PositiveVariable("z"))
// THEN
assertFalse(TwoLikelyVariablesMatcher.matches(operands))
}
What it does is the following: I have a list of mathematical terms and I want to filter out two variables which have the same unsigned Name (for example x
or y
.rezenebe
11/23/2018, 3:53 AMdMusicb
11/30/2018, 6:25 PMcompletedAlert?.let { updatedAlerts += it }
// or
if (completedAlert != null) updatedAlerts += completedAlert
Slackbot
12/09/2018, 5:35 PMjurajsolarml
12/18/2018, 1:53 PMDalinar
12/26/2018, 9:54 AMCompletableFuture
correctly
if I hammer (just from a single thread) asyncDumpReport()
then am I correct to assume that there is no chance of dumpReport()
firing until the previous call to dumpReport()
has completed?
`
/**
* calls [dumpReport] asynchronously, _if there is not currently such a call in progress_
*/
private fun asyncDumpReport() {
val p = promise
if (p == null || p.getNow(false))
promise = CompletableFuture.supplyAsync {
try {
dumpReport()
} finally {}
true
}
}
private var promise: CompletableFuture<Boolean>? = null
`
digitalsanctum
01/03/2019, 11:53 PMlistOf(
CoroutineScope(<http://Dispatchers.IO|Dispatchers.IO>).launch { ddbTools.deleteTableInRegions(destinationTable, destinationRegions) },
CoroutineScope(<http://Dispatchers.IO|Dispatchers.IO>).launch { jobService.deleteJobTable() },
CoroutineScope(<http://Dispatchers.IO|Dispatchers.IO>).launch { jobService.deleteCheckpointTable() }).joinAll()
breandan
01/11/2019, 11:38 PMghosalmartin
01/15/2019, 4:31 PMedwardwongtl
01/16/2019, 6:04 AMclass SimpleViewHolder<T>(view: View) : RecyclerView.ViewHolder(view)
class RxAdapter<T>(
@LayoutRes private val layoutId: Int,
private val binding: (View, T) -> Unit
) : ListAdapter<T, SimpleViewHolder<T>>(DiffCallback<T>()) {
private val signal by lazy { PublishSubject.create<Pair<Int, T>>() }
val itemClicks by lazy { signal as Observable<Pair<Int, T>> }
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SimpleViewHolder<T> =
SimpleViewHolder(parent.inflate(layoutId, false))
override fun onBindViewHolder(holder: SimpleViewHolder<T>, position: Int) {
binding(holder.itemView, getItem(position))
holder.itemView.setOnClickListener { signal.onNext(holder.adapterPosition to getItem(holder.adapterPosition)) }
}
private class DiffCallback<T> : DiffUtil.ItemCallback<T>() {
override fun areItemsTheSame(oldItem: T, newItem: T): Boolean = oldItem == newItem
override fun areContentsTheSame(oldItem: T, newItem: T): Boolean = oldItem == newItem
}
}
Forgive me for combining Rx into it, as it suits my need.edwardwongtl
01/16/2019, 6:04 AMclass SimpleViewHolder<T>(view: View) : RecyclerView.ViewHolder(view)
class RxAdapter<T>(
@LayoutRes private val layoutId: Int,
private val binding: (View, T) -> Unit
) : ListAdapter<T, SimpleViewHolder<T>>(DiffCallback<T>()) {
private val signal by lazy { PublishSubject.create<Pair<Int, T>>() }
val itemClicks by lazy { signal as Observable<Pair<Int, T>> }
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SimpleViewHolder<T> =
SimpleViewHolder(parent.inflate(layoutId, false))
override fun onBindViewHolder(holder: SimpleViewHolder<T>, position: Int) {
binding(holder.itemView, getItem(position))
holder.itemView.setOnClickListener { signal.onNext(holder.adapterPosition to getItem(holder.adapterPosition)) }
}
private class DiffCallback<T> : DiffUtil.ItemCallback<T>() {
override fun areItemsTheSame(oldItem: T, newItem: T): Boolean = oldItem == newItem
override fun areContentsTheSame(oldItem: T, newItem: T): Boolean = oldItem == newItem
}
}
Forgive me for combining Rx into it, as it suits my need.arekolek
01/16/2019, 8:46 AMDiffCallback
doesn’t handle modifying an itemedwardwongtl
01/16/2019, 8:47 AMarekolek
01/16/2019, 8:48 AMDiffCallback
doesn’t handle it, think of what happens if an item changes one field