Skip to content

Instantly share code, notes, and snippets.

View matteo-grella's full-sized avatar
🔥
I enjoy programming my own inventions.

Matteo Grella matteo-grella

🔥
I enjoy programming my own inventions.
View GitHub Profile

Nice, compliant input boxes

Nice input box with a lot of styling based on sibling selectors and psuedo classes.

CSS only, and WCAG 2.0 AA compliant!

A Pen by Andrew Tunnecliffe on CodePen.

License.

@matteo-grella
matteo-grella / GoogleMapsGeocodingHelper.kt
Created November 2, 2017 18:37
A Kotlin wrapper for the Google Maps Geocoding API v3
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
@matteo-grella
matteo-grella / incrementalAverage.kt
Created March 28, 2018 21:25
Simple Method to Calculate the Incremental Average
fun incrementalAverage(previousAverage: Double, value: Double, iterationIndex: Int): Double {
return (value - previousAverage) / (iterationIndex + 1) + previousAverage
}
@matteo-grella
matteo-grella / ByteArrayFromInt.kt
Created February 25, 2019 09:33
Transform an Integer to a Byte Array
/**
* Transform an Integer to a Byte Array.
*
* @param value the Integer
*
* @return the Byte Array
*/
internal fun getByteArrayFromInt(value: Int): ByteArray {
val mask = 0xFF // binary 1111 1111
/*
Yoshua Bengio:
My preferred style of moving average is the following. Let's say you
have a series x_t and you want to estimate the mean m of previous
(recent) x's:
m <-- m + (2/t) (x_t - m)
Note that with (1/t) learning rate instead of (2/t) you get the exact
@matteo-grella
matteo-grella / SplitToNChar.kt
Last active May 8, 2019 21:27
Split text into substrings of N characters.
/**
* Split text into substrings of [size] characters.
*
* @param size the split size.
* @return an array of the split text.
*/
private fun String.splitToNChar(size: Int): List<String> {
val parts = ArrayList<String>()
val length = this.length
@matteo-grella
matteo-grella / NotEmptyOrNull.kt
Created May 12, 2019 22:05
Return the list if it is not empty, otherwise null
/**
* Return the list if it is not empty, otherwise null
*/
private fun <T>List<T>.notEmptyOrNull(): List<T>? = if (this.isEmpty()) null else this
@matteo-grella
matteo-grella / RMQUtils.kt
Created May 13, 2019 13:02
Create a RabbitMQ Connection
import com.rabbitmq.client.Connection
import com.rabbitmq.client.ConnectionFactory
/**
* Create a RabbitMQ Connection.
*
* @param host the host
* @param port the port
* @param username the username (can be null)
* @param password the username (can be null)
/**
* @param resource the path to the resource to load
*
* @return the content of the resource
*/
fun loadResource(resource: String): String =
try {
object {}.javaClass.getResource(resource).readText(Charsets.UTF_8)
} catch (all: Exception) {
throw RuntimeException("Failed to load resource=$resource!", all)
@matteo-grella
matteo-grella / Combinations.kt
Created May 18, 2019 12:31
Extension method that returns the combinations of the elements of a list
/**
* Returns the combinations of the elements in this list as a list of pairs.
*
* The returned list is empty if this collection contains less than two elements.
*
* @return the combinations of the elements
*/
fun <E>List<E>.combine(): List<Pair<E, E>> = this.foldIndexed(mutableListOf()) { i, acc, element ->
for (j in i + 1 until this.size) {