Skip to content

Instantly share code, notes, and snippets.

View sfeatherstone's full-sized avatar

Simon Featherstone sfeatherstone

View GitHub Profile
{
"accountIDVStatus":"PASS",
"creditReportInfo":{
"score":547,
"scoreBand":4,
"clientRef":"CS-SED-655426-708782",
"status":"MATCH",
"maxScoreValue":700,
"minScoreValue":0,
"monthsSinceLastDefaulted":-1,
@sfeatherstone
sfeatherstone / ArrayCat.kt
Last active June 21, 2018 13:39
Exploring different ways to add/concatenate two arrays in Kotlin
//Copy using arraycopy
fun catTwoIntArrays1(array1 :IntArray, array2 :IntArray) : IntArray {
val newArray = IntArray(array1.size + array2.size)
System.arraycopy(array1, 0, newArray, 0 , array1.size)
System.arraycopy(array2, 0, newArray, array1.size , array2.size)
return newArray
}
//Copy using for loops
fun catTwoIntArrays2(array1 :IntArray, array2 :IntArray) : IntArray {
//Copy using arraycopy
fun catTwoIntArrays1(array1 :IntArray, array2 :IntArray) : IntArray {
val newArray = IntArray(array1.size + array2.size)
System.arraycopy(array1, 0, newArray, 0 , array1.size)
System.arraycopy(array2, 0, newArray, array1.size , array2.size)
return newArray
}
//Copy using for loops
fun catTwoIntArrays2(array1 :IntArray, array2 :IntArray) : IntArray {
val newArray = IntArray(array1.size + array2.size )
for ((index, value) in array1.withIndex()) {
newArray[index] = value
}
for ((index, value) in array2.withIndex()) {
newArray[index + array1.size] = value
//Copy using the lamda in the constructor
fun catTwoIntArrays3(array1 :IntArray, array2 :IntArray) : IntArray {
return IntArray(array1.size + array2.size,
{ if (it<array1.size) array1[it] else array2[it-array1.size]})
}
//Copy using the lamda in the constructor
inline fun <reified T> catTwoArrays3(array1 :Array<T>, array2 :Array<T>) : Array<T> {
return Array<T>(array1.size + array2.size,
{ if (it<array1.size) array1[it] else array2[it-array1.size]})
}
//Create a class that mimics an immutable Array<>
class CatTwoArrays<T>(internal val array1 : Array<T>, internal val array2 : Array<T>) {
public operator fun get(index: Int): T {
return if (index < array1.size) array1[index] else array2[index - array1.size]
}
public val size: Int = array1.size + array2.size
internal inner class CatTwoArraysIterator() : Iterator<T> {
internal var position = 0
//Silly simple way of making new array
fun catTwoIntArraysSimple(array1 :IntArray, array2 :IntArray) = array1 + array2
inline fun <reified T> catTwoArraysSimple(array1 :Array<T>, array2 :Array<T>) = array1 + array2
@sfeatherstone
sfeatherstone / kt
Last active March 26, 2019 08:32
val member evaluation
class Test {
private fun returnSomething(str: String):String {
println("evaluating $str")
return str
}
val testEquals = returnSomething("testEquals")
val testLazy by lazy { returnSomething("testLazy") }
val testGetBrackets: String
get() { return returnSomething("testGetBrackets") }
@sfeatherstone
sfeatherstone / kt
Last active April 16, 2019 14:56
Parallel execution using RxJava
fun performCheck(context: Context, callback: Callback, defaultEntryPoint: Boolean = false) {
compositeDisposable.add(job(1).subscribeOn(Schedulers.io())
.mergeWith(job(2).subscribeOn(Schedulers.io()))
.mergeWith(job(3).subscribeOn(Schedulers.io()))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ callback.pass()
Timber.d("Job OnComplete")
},
{ callback.fail(Callback.FailReason.LoggedOut)