Skip to content

Instantly share code, notes, and snippets.

View OnorioCatenacci's full-sized avatar

Onorio Catenacci OnorioCatenacci

View GitHub Profile
@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 / 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 / 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
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/
#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)
@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 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.