Skip to content

Instantly share code, notes, and snippets.

View Atternatt's full-sized avatar

Marc Moreno Ferrer Atternatt

View GitHub Profile
//region DSL
class ExecutionCancelationException(val failure: Any?) : RuntimeException()
interface ExecutionScope<in F> {
fun fail(f: F): Nothing = throw ExecutionCancelationException(f)
}
sealed interface Result<out F, out R>
data class Success<out R>(val result: R) : Result<Nothing, R>
//Does this compile?
inline fun <A, B> A.andThen(block: (A) -> Unit) {
Runnable {
block(this)
}
}
@Atternatt
Atternatt / either_and_async.kt
Created April 16, 2021 22:32
Combination of async coroutine and Arrows Either object
override suspend fun execute(query: PostsQuery): Either<Failure, List<Post>> {
val operation = if (query.forceRefresh) MainSyncOperation else CacheSyncOperation
return either {
val posts = !repository.getAll(query = query, operation = operation)
posts
.filter { it.featuredImage != null } //we just want posts with featured images
.map { post ->
withContext(coroutineDispatcher) {
async {
val host: String? = try {
@Test
@DisplayName("When Exporting only one file then rsync#sync should be called only once")
fun exportServerCallRsyncSync() {
collectorExporter.export(mockedFile, serverAddress, DirectExecutor, "").get()
verify(sync, times(1)).sync(absolutePathMock, serverAddress, "")
}
@Atternatt
Atternatt / CollectorExporter.kt
Last active July 9, 2019 18:47
Rsync colector exporter class made in kotlin
class CollectorExporter constructor(context: Context,
private val executor: AppExecutor,
private val sync: Sync = SyncClient(context)) {
fun export(analyticsFolder: File, metadataFolder: File, collectorNetworkAddress: String, executor: ListeningExecutorService = this.executor, uploadFolder: String):
ListenableFuture<Unit> {
return executor.submit(Callable {
sync.sync(analyticsFolder.absolutePath, collectorNetworkAddress, uploadFolder)
@Atternatt
Atternatt / ArgumentDelegateUsage.kt
Last active August 30, 2018 17:08
Bundle Delegate
class UsageFragment : Fragment() {
private val numberWhatever by Argument("ARG_PROPERTY", 0)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//same behaviour tham BundleParam but withArguments in fragments
print("$numberWhatever!")
}
val day = today
val todayOnWeekInTheFuture = today + 1.week
//the power of infix
val isBefore = day isBefore todayOnWeekInTheFuture
day.formatedDateText("dd/MM/yyyy")
val oneJan2020at10Oclock = Dates.of(2020, 1, 1,10)
@Atternatt
Atternatt / NearestNeighbors.kt
Last active September 21, 2017 04:59
Recommendation algorithms
fun List<Number>.getNearestNeighbors(posibleNeighbors: List<List<Number>>, numberOfNeighbors: Int): List<List<Double>> {
return posibleNeighbors.mapIndexed { index, posibleNeighbor -> index to this.pearsonCorrelationWith(posibleNeighbor) }
.sortedBy { it.second }
.take(numberOfNeighbors)
.map { posibleNeighbors[it.first].map { it.toDouble() } }
}
@Atternatt
Atternatt / BackgroundColorAnimation.kt
Last active September 21, 2017 04:58
Property delegation Examples
fun backgroundColorAnimator(): ReadWriteProperty<View, Int> =
object : ReadWriteProperty<View, Int> {
override fun getValue(thisRef: View, property: KProperty<*>): Int {
return (thisRef.background as? ColorDrawable)?.color ?: 0
}
override fun setValue(thisRef: View, property: KProperty<*>, value: Int) {
ValueAnimator.ofObject(ArgbEvaluator(), getValue(thisRef, property), value).apply {
@Atternatt
Atternatt / DelegatesAndExtensions.kt
Last active September 21, 2017 04:57
DelegatesAndExtensions
fun alphaWithBounceAnimator(): ReadWriteProperty<ImageView, Int> =
object : ReadWriteProperty<ImageView, Int> {
override fun getValue(thisRef: ImageView, property: KProperty<*>): Int {
return thisRef.imageAlpha
}
override fun setValue(thisRef: ImageView, property: KProperty<*>, value: Int) {
val animX = ObjectAnimator.ofFloat(thisRef, "scaleX", 1f, 2.5f, 1f)