Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View toranb's full-sized avatar

Toran Billups toranb

View GitHub Profile
@toranb
toranb / fizzbuzz.ex
Last active January 30, 2024 07:40
fizzbuzz with Axon (collaboration with Ian Warshak)
defmodule Mlearning do
@moduledoc false
def mods(x) do
[rem(x, 3), rem(x, 5)]
end
def fizzbuzz(n) do
cond do
rem(n, 15) == 0 -> [0, 0, 1, 0]
@toranb
toranb / leex_to_heex.ex
Last active July 27, 2022 21:56
inline conditional css comparison of leex and heex
##### phx live view 0.16 with leex
def render(assigns) do
~L"""
<button class="<%= if @foo && @bar && @baz do %>text-blue-600<% else %>text-red-600<% end %>">hello</button>
"""
end
##### phx live view 0.16 with heex -inline edition
@toranb
toranb / app.js
Created July 4, 2020 15:07 — forked from nicolasblanco/app.js
Stripe.js elements integration inside a Elixir LiveView (Bootstrap styles)
// Code handling the card payment
const payWithCard = function (stripe, card, clientSecret, liveView) {
stripe
.confirmCardPayment(clientSecret, {
payment_method: {
card: card,
},
})
.then(function (result) {
if (result.error) {
@toranb
toranb / jedi-for-python.txt
Created January 15, 2019 14:39
Python for go-to-definition
pip install jedi
then -in your vimrc
```
nmap <Leader>j :call InvokeJumpToByType()<CR>
function! InvokeJumpToByType()
let filetype=&ft
if filetype == 'python'
exe ':call jedi#goto_definitions()'
@toranb
toranb / postgres.txt
Created December 23, 2018 14:05
First time install of postgres on macOS Mojave
1) Install homebrew if you haven't already
https://brew.sh/
2) Install postgres
brew install postgresql
3) Start the database
@toranb
toranb / wait-for-redux.js
Last active November 1, 2018 21:13
custom ember helper to wait for redux to be in a given state
import { Promise } from 'rsvp';
import { next } from '@ember/runloop';
import { registerWaiter } from '@ember/test';
import { getContext } from '@ember/test-helpers';
export async function waitForRedux(key, value) {
return new Promise(async (resolve) => {
let counter = 1;
registerWaiter(() => counter === 0);
const { owner } = getContext();
@toranb
toranb / hydrate_state_from_filesystem.ex
Created October 28, 2018 17:39
Simple GenServer callback to create a map w/ {key, value} pairs by looking at the filesystem
@impl GenServer
def handle_call({:all}, _timeout, _state) do
{:ok, files} = :file.list_dir(@database)
state =
Enum.map(files, fn (key) ->
value =
case File.read(file_name(@database, key)) do
{:ok, contents} -> :erlang.binary_to_term(contents)
_ -> nil
end
@toranb
toranb / verbose-some.exs
Created October 14, 2018 17:39
My first implementation of some in elixir
defmodule MyEnum do
def some([], _func), do: []
def some([head | tail], func) do
case func.(head) do
true ->
true
false ->
case some(tail, func) do
true ->
true
@toranb
toranb / functions-in-elixir.exs
Created September 27, 2018 12:40
functions defs can have multiple implementations in elixir
defmodule Foo do
def my_func({name}) do IO.puts("#{name}") end
def my_func({name, number}) do IO.puts("#{name} #{number}") end
end
@toranb
toranb / pinned-values-as-params.exs
Last active September 27, 2018 13:04
showing how pinned values and pattern matching work in elixir
defmodule Robot do
def speak(name, phrase) do
fn
(^name) -> "hello! #{name}, #{phrase}"
(_) -> "sorry ... don't know this name"
end
end
end