Skip to content

Instantly share code, notes, and snippets.

View OnorioCatenacci's full-sized avatar

Onorio Catenacci OnorioCatenacci

View GitHub Profile

Two Kinds Of Elixir Constants

What Do I Mean By 'Constant'?

First it feels appropriate to say that when I say constant I have a definite technical meaning in mind. In a mathematical sense a constant like pi or e is simply a value that's fixed on the number line. In the sense that software developers use the word "constant" we usually mean something which is an extension of the notion of a mathematical constant--a fixed value. It could be a number or a string or even a more complex data structure; the point is that in the course of the normal execution of the binary it won't change.

Here I'm using the term constant to name a value that needs to be referenced in two or more modules in Elixir which none of them will change. The value will be defined in one module and then read from others. There are two reasons to do this approach:

  1. It makes the code more readable
  2. It keeps the developer from repeating his/her self.
@OnorioCatenacci
OnorioCatenacci / TicTacToeGame.tla
Created November 3, 2020 21:16
Initial state of TicTacToe Game
--------------------------- MODULE TicTacToeGame ---------------------------
VARIABLE boardPositions, turn
tokens == {"X","O","V","-"} (* V == "Vacant", - == "Doesn't matter" *)
row == <<tokens, tokens, tokens>>
grid == <<row, row, row>>
col_1_Win(token) == <<
<<token, "-", "-">>,
<<token, "-", "-">>,
<<token, "-", "-">>
#Two small, quick date utility functions. Convert a wxDateTime (from wxErlang) to a YYYYMMDD string. NB: assumes year is post 1000 CE.
defp wxDatetime_to_string({{year,month,day},{_hour,_minute,_second}}), do: "#{year}#{zero_padding(month,2)}#{zero_padding(day,2)}"
#Pad a string out with a specified number of leading zeroes.
defp zero_padding(string,len), do: String.rjust(to_string(string), len, ?0)
Good Fonts:
Font Squirrel: http://www.fontsquirrel.com/
Urban Fonts: http://www.urbanfonts.com/
Google Web Fonts: http://www.google.com/fonts/
Color Schemes:
Colour Lovers: http://www.colourlovers.com/
Color Scheme Designer: http://colorschemedesigner.com/
Image Color Picker: http://imagecolorpicker.com/
@OnorioCatenacci
OnorioCatenacci / WinClip.ex
Created August 1, 2014 19:27
How To Write Data To Windows Clipboard
defmodule WinClip do
def write_to_clipboard(string_to_write) do
p = Port.open({:spawn,"clip"},[:stderr_to_stdout, :stream, :binary, :exit_status])
Port.command(p,string_to_write)
Port.close(p)
end
end
@OnorioCatenacci
OnorioCatenacci / stack.ex
Last active August 29, 2015 14:00
A Naive Implementation Of A Stack
defmodule Stack do
defstruct name: "", value: 0
def init() do
_s = []
end
def push(name, value, s) do
s_new = [%Stack{name: name, value: value}, s]
{:ok,s_new}
@OnorioCatenacci
OnorioCatenacci / LiteralWithFormatString.fs
Created October 10, 2013 18:49
Trying to use [<Literal>] with Format String
[<Literal>]
let formatString = "format of %s"
let s = sprintf formatString "test"
@OnorioCatenacci
OnorioCatenacci / fortunecookie.fsx
Created October 10, 2013 12:56
Connect F# To Access Via Odbc
open System
open System.Data.Odbc
open System.Windows.Forms
let connectToAccess filename =
let connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;FileDSN=" + filename + ";User Id=admin;Password=;"
new OdbcConnection(connectionString)
let connectToDb() =
@OnorioCatenacci
OnorioCatenacci / DigitScript.fsx
Last active December 4, 2022 08:25
Machine Learning Dojo
// This F# dojo is directly inspired by the
// Digit Recognizer competition from Kaggle.com:
// http://www.kaggle.com/c/digit-recognizer
// The datasets below are simply shorter versions of
// the training dataset from Kaggle.
// The goal of the dojo will be to
// create a classifier that uses training data
// to recognize hand-written digits, and
// evaluate the quality of our classifier
@OnorioCatenacci
OnorioCatenacci / CheckForConn.fs
Last active December 18, 2015 10:39
F# Code To Check For Connection Type With Xamarin.Android
open Android.App
open Android.Content
open Android.Net
let IsConnectionAvailable (app:Activity) connTypeToCheckFor =
let n = app.GetSystemService(Context.ConnectivityService)
let cm =
match n with