Skip to content

Instantly share code, notes, and snippets.

View ggdio's full-sized avatar
:octocat:
Learning something new

Guilherme Dio ggdio

:octocat:
Learning something new
View GitHub Profile
@pavlov99
pavlov99 / haversine.scala
Created December 19, 2016 07:52
Spherical distance calcualtion based on latitude and longitude with Apache Spark
// Based on following links:
// http://andrew.hedges.name/experiments/haversine/
// http://www.movable-type.co.uk/scripts/latlong.html
df
.withColumn("a", pow(sin(toRadians($"destination_latitude" - $"origin_latitude") / 2), 2) + cos(toRadians($"origin_latitude")) * cos(toRadians($"destination_latitude")) * pow(sin(toRadians($"destination_longitude" - $"origin_longitude") / 2), 2))
.withColumn("distance", atan2(sqrt($"a"), sqrt(-$"a" + 1)) * 2 * 6371)
>>>
+--------------+-------------------+-------------+----------------+---------------+----------------+--------------------+---------------------+--------------------+------------------+
|origin_airport|destination_airport| origin_city|destination_city|origin_latitude|origin_longitude|destination_latitude|destination_longitude| a| distance|
@ggdio
ggdio / ConcurrentUUIDGenerator.java
Created July 30, 2019 01:27
A concurrent random UUID generator
public class ConcurrentUUIDGenerator {
public static UUID randomUUID() {
final Random rnd = ThreadLocalRandom.current();
long mostSig = rnd.nextLong();
long leastSig = rnd.nextLong();
// Identify this as a version 4 UUID, that is one based on a random value.
mostSig &= 0xffffffffffff0fffL;
mostSig |= 0x0000000000004000L;