Skip to content

Instantly share code, notes, and snippets.

Avatar

William Normand Faheetah

View GitHub Profile
@Faheetah
Faheetah / README.md
Created Mar 15, 2022
Add Tailwind to Phoenix
View README.md

Make all the changes described in these files. Delete assets/css/phoenix.css as it is no longer used.

  • config/dev.exs
  • assets/package.json
  • assets/postcss.config.js
  • assets/tailwind.config.js
  • assets/css/app.css
  • mix.exs
View gist:d4d3a4f4fae82d79002bbc51b3b5ed90
# set a loop for running commands, will run unit tests on enter
while read; do go test ./...; done
# set a loop that runs once a second (like watch), can be used in cases watch doesn't fit as well
while sleep 1; do ls; done
# do something with the previous command
pwd
ls $(!!) # equivalent to ls $(pwd)
@Faheetah
Faheetah / README.md
Last active Sep 29, 2021
Golang: pass in a struct and a map and retrieve values
View README.md

This is handy for cases such as needing to dynamically assign structs, for example when use with marshalling or when assigning the results of a SQL query into a struct, without duplicating code for each type. This is a naive implementation as it takes absolutely no error checking into account. It is not production safe and is meant to demonstrate how to iterate through and populate a struct.

Example usage

type Foo struct {
	Int        int
	String     string
	Unassigned string
}
@Faheetah
Faheetah / struct_parser.exs
Last active Jun 23, 2021
An example of parsing C with Elixir nimble_parsec, specifically Libvirt XDRs
View struct_parser.exs
defmodule StructParser do
import NimbleParsec
@space 0x0020
@tab 0x009
whitespace = utf8_char([@space, @tab, ?\n])
ignore_whitespace =
lookahead(whitespace)
@Faheetah
Faheetah / test_parser.ex
Last active Jun 16, 2021
Parsing a complex key = value structure with optional trailing comments
View test_parser.ex
defmodule Test.Parser do
import NimbleParsec
# iex(9)> Test.Parser.parse """
# ...(9)> FOO = 1 (some foo)
# ...(9)> BAR = 2 (some
# ...(9)> bar)
# ...(9)> FAZ = 3 (some faz)
# ...(9)> """
# {:ok, ["FOO", 1, "somefoo", "BAR", 2, "somebar", "FAZ", 3, "somefaz"], "\n",
@Faheetah
Faheetah / README.md
Last active May 1, 2021
Elixir Phoenix TLDR
View README.md

Generate an App

Requires phx.new, install with mix archive.install hex phx_new

mix phx.new app

Common flags:

@Faheetah
Faheetah / README.md
Created May 1, 2021
Log commands to a .history file for repos
View README.md

A problem with repositories is sometimes you do not remember what commands you ran to bootstrap the application, or what generators were used. A history file similar to .bash_history would be handy for a per-project basis. The h command above provides a wrapper around this functionality. It should be portable across any system with bash.

Run a command and write out the command to the git project's .history file, this logs $@ to .history

$ h mix phx.new project_name
$ h mix deps.get
$ h mix ecto.create
$ cat .history
mix phx.new project_name
@Faheetah
Faheetah / brute_force_calculate_stars.exs
Created Mar 23, 2021
How many permutations of rows can make 51 stars, max of 25 per row, max of 10 rows, can alternate
View brute_force_calculate_stars.exs
defmodule FlagStars do
def calculate() do
Enum.reduce(1..25, [], fn first, acc ->
acc ++ Enum.reduce(1..25, [], fn second, acc ->
acc ++ Enum.reduce(1..10, [], fn rows, acc ->
acc ++ cond do
(rows * first) == 51 -> [{first, rows}]
((div(rows, 2) + rem(rows, 2)) * first) + (div(rows, 2) * second) == 51 -> [{first, second, rows}]
true -> []
end
@Faheetah
Faheetah / decorators.py
Last active Nov 30, 2018
Python Decorator Foo
View decorators.py
# I always get confused with decorators because of all the nesting and where all of the arguments go
# Reference of using decoratores with arguments and with classes
## Very Simple Decorator
def foo(f):
# Inner function that must be returned
def decorator():
print('decorator')
# Will be bar() from the below wrapped call