Skip to content

Instantly share code, notes, and snippets.

View darvld's full-sized avatar

Dario Valdespino darvld

View GitHub Profile
@darvld
darvld / hill_climbing.py
Last active March 22, 2023 04:31
Hill climbing (steepest ascent) implementation in Python
# Simple hill-climbing algorithm using the steepest-ascent variant
def perform_step(current: list, goal: list) -> (list, int):
# Keep track of the best candidate so far (steepest-ascent)
successor = current
successor_weight = -1
for i in range(0, len(current) - 1):
# Generate a new state and calculate the weight
candidate = swapped(current, i, i + 1)
candidate_weight = calculate_weight(candidate, goal)
@darvld
darvld / DialogResult.kt
Created April 10, 2022 17:52
DialogResult helper for Jetpack/Desktop Compose
package io.github.darvld.utils
/**Base class for components representing the result of the user's interaction with a dialog.
*
* To trigger the dialog and obtain the result, use [await].
*
* Note that, since this class provides no UI implementation, [complete] must be manually called from UI code. This is
* typically achieved by showing a dialog when [isAwaiting] is `true`, and using a callback to invoke [complete].*/
interface DialogResult<T> {
/**Whether the dialog is currently pending user interaction.
/*
* Copyright 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@darvld
darvld / CircularReveal.kt
Created October 3, 2021 23:03
A circular reveal effect modifier for Jetpack Compose.
package cu.spin.catalog.ui.components
import android.annotation.SuppressLint
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.updateTransition
import androidx.compose.runtime.State
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.draw.drawWithCache
import androidx.compose.ui.geometry.Offset
@darvld
darvld / LazyListAnimation.kt
Last active April 24, 2022 19:01
This gist contains a simple implementation of animated item transitions for use with `LazyListScope` (`LazyColumn`/`LazyRow`).
package io.github.darvld.utils
import androidx.compose.animation.*
import androidx.compose.animation.core.ExperimentalTransitionApi
import androidx.compose.animation.core.MutableTransitionState
import androidx.compose.foundation.lazy.LazyListScope
import androidx.compose.foundation.lazy.items
import androidx.compose.runtime.*
import androidx.recyclerview.widget.AsyncListDiffer
import androidx.recyclerview.widget.DiffUtil