Skip to content

Instantly share code, notes, and snippets.

@LNA
LNA / single_responsibility_principle.rb
Last active August 29, 2015 14:18
SOLID For #CodeNewbie
# Method should do just one thing at a time
# How can we flag methods that are doing more than one thing?
# Words like and & or should never be in a method. These words indicate that
# the method is doing more than one thing.
# bad
def bake_and_eat(cookies)
cookies.bake
cookies.eat
@LNA
LNA / large_query.rb
Last active August 29, 2015 14:18
Search values of Large Query
students = YogaStudent.find(:all, :limit => 10)
# will return an array with 10 random yoga student records
ashtanga_students = students.select { |student| student[:xml].match(/ashtanga/) }
# will look only at the xml keys to match ashtanga in the values.
@LNA
LNA / distinct_query.sql
Last active August 29, 2015 14:18
Query An Attribute PostgreSQL
select distinct(column_name) from table_name order by column_name ASC;
@LNA
LNA / roman-num-final.clj
Last active August 29, 2015 14:17
Roman Numeral Kata in Clojure
; The big change in logic happens at 5. You can use nested if statements to solve the kata up to 5.
; After that, its best to define the roman numerals and their numbers in pairs, and pull from that.
(def pairs [[10 "X"][9 "IX"][5 "V"] [4 "IV"] [1 "I"]])
(defn roman [n]
(loop [n n
result ""
pairs pairs]
(if (< n 1)
(mapcat [1 2 3] [4 5 6])
=> Gives an error. Mapcat wants a function and a coll, not two colls.
(maptcat str [4 5 6])
=> Gives (\4 \5 \6). It applies the function its given to the coll its given. Then it concats the result.
(concat [4 5 6]) gives [4 5 6]. So really, mapcat is a great shortcut for accomplishing two things at once.
@LNA
LNA / concat-and-con.clj
Last active August 29, 2015 14:17
Concat & Conj
(concat [1 2 3] 4)
=> gives an error. Remember, concat wants to squish together 2 differnt collections of data.
(conj [1 2 3] 4)
=> gives [1 2 3 4]. Sucessfully adds an item to a collection. Added it to the end because the coll is a vector.
(concat [1 2 3] [4])
private class CarBuilder {
private int year;
private String make;
public CarBuilder setYear(int year) {
this.year = year;
return this;
}
public CarBuilder setMake(String make) {
@LNA
LNA / new_format.json
Last active August 29, 2015 14:15
Changes with Petrograph
{"assignments":[{"slug":"robot-name","track":"clojure","files":{"robot.clj":"(ns robot)\r\n\r\n(def num-letters 26)\r\n\r\n(def name-seed\r\n (atom [0 0]))\r\n\r\n(defn- inc-name-seed\r\n []\r\n (swap! name-seed\r\n (fn [[letter number]]\r\n (if (\u003e= number 999)\r\n [(inc letter) 0]\r\n [letter (inc number)]))))\r\n\r\n(defn- letter-at\r\n [n]\r\n (char (+ n (int \\A))))\r\n\r\n(defn- generate-name\r\n []\r\n (let [[letter number] @name-seed\r\n result (str (letter-at (quot number num-letters))\r\n (letter-at (mod number num-letters))\r\n (format \"%03d\" number))]\r\n (inc-name-seed)\r\n result))\r\n\r\n(defn robot\r\n []\r\n (atom (generate-name)))\r\n\r\n(defn robot-name\r\n [robot]\r\n @robot)\r\n\r\n(defn reset-name\r\n [robot]\r\n (reset! robot (generate-name)))\r"}},{"slug":"scrabble-score","track":"go","files":{"scrabble_score.go":"package scrabble_score\n\nimport \"unicode\"\n\nvar scores = m
@LNA
LNA / file_response.java
Last active August 29, 2015 14:15
How should I test this?
public class FileRouter implements iResponse {
// omitted code
@Override
public byte[] buildResponseBody(Map<String, String> request) throws IOException {
String fileOneString = request.get("DIRECTORY") + request.get("URI");
Path file = Paths.get(fileOneString);
byte[] body = Files.readAllBytes(filePath);
return body;
}
@LNA
LNA / auth.txt
Created February 19, 2015 14:39
Auth Request
GET /logs HTTP/1.1
Authorization: Basic YWRtaW46aHVudGVyMg==
Connection: close
Host: localhost:5000
The request: {Type=GET}