Skip to content

Instantly share code, notes, and snippets.

@kevbuchanan
kevbuchanan / health_controller.ex
Last active August 4, 2023 16:06
Phoenix Health Endpoint
defmodule MyAppWeb.HealthController do
use MyAppWeb, :controller
alias MyAppWeb.Heartbeat
def root(conn, _) do
send_resp(conn, :no_content, "")
end
def show(conn, _) do
@kevbuchanan
kevbuchanan / normalize.rb
Created September 14, 2016 00:19
TS normalize
def normalize(import_statement, file_path)
import_from = import_statement.scan(/(?<=from ').*(?=')/)[0]
from_path = file_path.split('/').reverse
to_path = import_from.split('/')
new_path = []
while !to_path.empty?
case to_path.last
when '.'
new_path << to_path.pop
when '..'
@kevbuchanan
kevbuchanan / bowling.swift
Created April 8, 2016 19:26
Bowling Swift
class Frame {
var one: Int
var two: Int?
var extras: [Int]
init(one: Int, two: Int?) {
self.one = one
self.two = two
self.extras = [Int]()
}
@kevbuchanan
kevbuchanan / y.clj
Last active February 12, 2016 21:53
Y Combinators
(def y
(fn [improver]
((fn [gen] (improver (fn [v] ((gen gen) v))))
(fn [gen] (improver (fn [v] ((gen gen) v)))))))
(def fact-improver
(fn [partial]
(fn [n]
(if (= n 0)
1
@kevbuchanan
kevbuchanan / constraint.rb
Created February 2, 2016 03:20
Constraint Solver WIP
class Var < Struct.new(:name, :domain)
def self.gen(domains)
domain = domains.first.product(*domains[1..-1])
Var.new("__gen__#{domain.object_id}", domain)
end
def empty?
domain.empty?
end
@kevbuchanan
kevbuchanan / memo.rb
Created July 13, 2015 21:07
Ruby Memoize
module Memoize
module DefMemo
def defmemo(name, &block)
define_method(name) do |*args|
__memo__[name].fetch(args) do |key|
__memo__[name][key] = instance_exec(*args, &block)
end
end
end
end
@kevbuchanan
kevbuchanan / proc.rb
Last active August 29, 2015 14:18
Ruby Optional Proc
def call_maybe(x)
if block_given?
yield x
else
x
end
end
call_maybe(2) { |x| x + 1 }
call_maybe(2)
@kevbuchanan
kevbuchanan / result.rb
Created February 25, 2015 15:27
Result Object
class Result
def self.status(status, result)
new(status, result)
end
def initialize(status, result)
@status = status
@result = result
end
@kevbuchanan
kevbuchanan / redirect.sh
Created December 12, 2014 16:16
Redirecting stderr
./test.rb 2>errors.log
@kevbuchanan
kevbuchanan / if-not-empty-let.clj
Created January 15, 2014 01:05
if-not-empty-let
(ns if-not-empty-let.core
(:require [clojure.test :refer [deftest is run-tests]]))
(defmacro if-not-empty-let
([binding then] `(if-not-empty-let ~binding ~then nil))
([binding then else]
(let [form (first binding)
sequence (binding 1)]
`(let [temp# ~sequence]
(if (not (empty? temp#))