Skip to content

Instantly share code, notes, and snippets.

View acabreragnz's full-sized avatar

Toni Cabrera acabreragnz

  • Bixlabs
  • Montevideo, Uruguay
View GitHub Profile
@acabreragnz
acabreragnz / crypto_system.ex
Last active November 10, 2017 16:35
Caesar Cipher, using a different key for each different word (key_seed for word in first place, key_seed + 1 for word in second_place, ...). Also we have a force brute function to find the possible keys
defmodule CryptoSystem do
def encrypt(text, key_seed) do
words = String.split(text, " ");
words
|> Enum.with_index()
|> Enum.map(&encrypt_word(&1, key_seed))
|> Enum.join(" ")
end
@acabreragnz
acabreragnz / rps.ex
Last active October 25, 2017 01:33
Rock Scissors Paper game in Elixir! (remember to wrap this inside a module)
@weapons ~w(rock scissors paper)a
def rps(user_choice) when is_binary(user_choice), do: is_valid_choice(String.to_atom(user_choice))
def rps(user_choice) when is_atom(user_choice), do: is_valid_choice(user_choice)
def rps(user_choice), do: {:lost, "Invalid type!"}
defp is_valid_choice(user_choice) when user_choice in @weapons, do: time_to_play(user_choice)
defp is_valid_choice(user_choice), do: {:lost, "Invalid choice!"}
defp time_to_play(user_choice) do
@acabreragnz
acabreragnz / select_distance_knex.js
Last active September 30, 2020 16:37
This module extends the knex query builder with selectDistance method, the method adds a virtual column to the current query (qb) with the alias as, in which resides the calculated distance from the geoPoint to the entity.location column. It also accepts some options parameters like the unit (km | mi) and the decimalPrecision of the result. Deps…
var knex;
/**
* This module extends the knex query builder with selectDistance method,
* The method adds a virtual column to the current query (qb) with the alias as, in which
* resides the calculated distance from the geoPoint to the entity.location column.
* It also accepts some options parameters like the unit (km | mi) and the decimalPrecision of the result.
* Deps: Postgis
* @example
@acabreragnz
acabreragnz / load-hours.js
Created November 22, 2016 15:22
All the hours in a day in 12-hour clock format with a defined inverval
/**
* All the hours in a day in 12-hour clock format with a defined inverval
*
* @example
* // returns [
* '12:00 AM',
* '12:15 AM',
* '12:30 AM',
* '12:45 AM',
* '01:00 AM',