Skip to content

Instantly share code, notes, and snippets.

@jferrao
jferrao / iban.kt
Created March 31, 2018 08:41
IBAN validator
import java.math.BigInteger
class Iban(private val iban: String) {
companion object {
/* List updated to release 73, January 2017, of IBAN Registry (75 countries) */
private const val countryCodes =
"AD24 AE23 AL28 AT20 AZ28 BA20 BE16 BG22 BH22 BR29 BY28 CH21 CR22 CY28 CZ24 DE22 " +
"DK18 DO28 EE20 ES24 FI18 FO18 FR27 GB22 GE22 GI23 GL18 GR27 GT28 HR21 HU28 IE22 " +
"IL23 IQ23 IS26 IT27 JO30 KW30 KZ20 LB28 LC32 LI21 LT20 LU20 LV21 MC27 MD24 ME22 " +
@jferrao
jferrao / haversine.kt
Last active August 8, 2024 08:51
Kotlin implementation of the Haversine formula
class Geo(private val lat: Double, private val lon: Double) {
companion object {
const val earthRadiusKm: Double = 6372.8
}
/**
* Haversine formula. Giving great-circle distances between two points on a sphere from their longitudes and latitudes.
* It is a special case of a more general formula in spherical trigonometry, the law of haversines, relating the
* sides and angles of spherical "triangles".
@jferrao
jferrao / HttpRequest.playground.swift
Last active November 8, 2015 23:17
Sandbox test for a GET HTTP request on OSX
import Foundation
import XCPlayground
func httpGet(request: NSURLRequest!, callback: (NSURLResponse?, String, String?) -> Void) {
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request) {
(data, response, error) -> Void in
if error != nil {
callback(response, "", error!.localizedDescription)
} else {
@jferrao
jferrao / hgpullall.sh
Created June 25, 2015 22:32
Mercurial pull from all subdirectories
#!/bin/bash
basedir=$(pwd -P)
for subdir in $(find . -maxdepth 1 -type d );
do
dir="$basedir"/$(basename "$subdir")
# Check if sub-directory is a Mercurial repository
if [ -n "$(hg --cwd $dir root)" ];
then
@jferrao
jferrao / vm.sh
Last active August 29, 2015 14:03
Linux and MacOS X alias script to VirtualBox commands
#!/bin/bash
## Linux and MacOS X alias script for VirtualBox console commands
## to start, stop, query status and list Virtual Machines
##
## Usage: vm [vm_name] <command> <argv>...
##
## VM commands:
## start Starts the VM
## stop Stops a running VM using ACPI shutdown
@jferrao
jferrao / PnQueue.php
Last active October 1, 2021 18:57
Simple priority queue implementation in PHP
<?php
/**
* A really simple priority queue implementation in PHP.
*
* @author João Ferrão
* @link https://github.com/jferrao
*/
class PnQueue
{
@jferrao
jferrao / MySoapClient.php
Last active November 23, 2021 07:00
PHP SoapClient extension to persist any non http or non port 80 calls.
<?php
/**
* Allows the PHP SoapClient to persist any non http or non port 80 calls.
*
* When SoapClient in WSDL mode with a scheme or a port others then http and 80, the first
* connection goes through correctly but all subsequent ones revert back to http and no
* port in the URI. This class fixes that and allows the SoapClient to use the same scheme
* and port as the ones defined when a new instance of the class is created.
*