Skip to content

Instantly share code, notes, and snippets.

View dyba's full-sized avatar

M. Daniel Dyba dyba

View GitHub Profile
@dyba
dyba / core.clj
Last active August 29, 2015 14:14
Excel's DAYS360 in Clojure
(ns days360.core
(:require [clj-time.core :as t]
[clj-time.format :as f]
[clj-time.predicates :as p]
[clj-time.periodic :as per]))
;; It works! Now to prettify it...
(defn days360
"Calculates the number of days between a start-date and end-date according to
a 360-day year."
@dyba
dyba / lol.clj
Last active August 29, 2015 14:17
LOL
;; We can go further and use automatic gensyms provided
;; by Clojure when use a hashtag after the symbol we
;; want gensymed
(defmacro nif
[expr pos zero neg]
`(let [e# ~expr]
(cond
(pos? e#) ~pos
(zero? e#) ~zero
;; WIP...
(defn quote-units
[units]
(let [syms (filter symbol? units)
vals (filter number? units)]
(interleave (map (fn [u] `'~u) syms) vals)))
(defmacro defunits
[quantity base-unit & units]
@dyba
dyba / boot.clj
Created September 25, 2015 23:03
(set-env!
:source-paths #{"javasrc"}
:resource-paths #{"src/pnmacmodel" "src/movr2"}
:repositories [["releases" {:url "http://es-ubu-archiva-d-2.pnmac.com:8080/repository/internal"
:creds :gpg}]
["snapshots" {:url "http://es-ubu-archiva-d-2.pnmac.com:8080/repository/snapshots"
:creds :gpg}]
["clojars" {:url "https://clojars.org"}]]
:dependencies '[[org.clojure/clojure "1.7.0"]
[com.taoensso/nippy "2.5.2"]
@dyba
dyba / karate_chop_2.rb
Created November 10, 2011 23:58
Karate Chop - Take Two
def chop(int, array_of_int)
return -1 if array_of_int.empty?
@max ||= array_of_int.size - 1
@min ||= 0
@mid ||= @max / 2 + @min
return @mid if int == array_of_int[@mid]
return -1 if @mid == @max
@dyba
dyba / recursions.clj
Created January 16, 2012 01:30
Clojure Koans - Stuck
(defn is-even? [n]
(if (= n 0)
(not false)
(recur (is-even? (dec n)))))
(meditations
"Recursion ends with a base case"
(= true (is-even? 0))
"And starts by moving toward that base case"
@dyba
dyba / recursions.clj
Created January 21, 2012 00:14
Clojure Koans - Factorial
;; Earlier attempt
(defn factorial [n]
(loop [n n
result n]
(if (<= n 1)
1
result)
(recur (dec n) (* result (dec n)))))
;; Working so far
@dyba
dyba / asa_cmds_rule_deletion.rb
Created January 27, 2012 16:18
Cisco ASA Firewall Rule Deletion Generator via RedSeal Device Cleanup Analysis Results
#
# PURPOSE:
# The purpose of this script is to generate a list of commands to delete unused access rules from a
# Cisco PIX or ASA firewall. This script is appropriate if you use the RedSeal application:
# http://www.redsealnetworks.com. Currently the results are printed to the screen only.
#
# INSTRUCTIONS:
# When you want to clean up unused rules on a firewall. Use the RedSeal application to run a cleanup:
# Tools > Manage Device Cleanup (Alt+T, C). When finished, export the results as a CSV file. Don't
# forget to include the .CSV extension. Run this script and type the name of the file with the
@dyba
dyba / application_helper.rb
Created August 1, 2012 03:04
Testing a helper method with a minimalist approach
module ApplicationHelper
def highlight_tag_if(condition, tag, &block)
if condition
content_tag tag, :class => 'hilite', &block
else
content_tag tag, &block
end
end
end
@dyba
dyba / movie.rb
Created August 6, 2012 05:41
Testing a class method
class Movie < ActiveRecord::Base
def self.all_ratings
# debugger
find_by_sql("SELECT DISTINCT rating FROM movies").map(&:rating)
end
end