This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # A function to make a person Hash. | |
| def make_person(first_name, second_name, age) | |
| person = | |
| {"first_name" => first_name, | |
| "last_name" => second_name, | |
| "age" => age} | |
| return person | |
| end | |
| # Returns true if person1 is older than person2, otherwise false. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Write a function that returns true if youngest_person() works correctly. | |
| # *Then*, write youngest_person() and check that it's correct using your test. | |
| # youngest_person() should take an Array of people and return the youngest one. | |
| # | |
| # Hint: Consider using max or min, which are defined on the ruby docs site. | |
| # http://ruby-doc.org/core-1.9.3/Array.html | |
| def youngest_person(people) | |
| # ??? <-- Only do this after writing the test and making sure it starts out failing! | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Week 07 | |
| # Homework from last week... | |
| # | |
| # Write a function that returns true if youngest_person() works correctly. | |
| # *Then*, write youngest_person() and check that it's correct using your test. | |
| # youngest_person() should take an Array of people and return the youngest one. | |
| # | |
| # Hint: Consider using max or min, which are defined on the ruby docs site. | |
| # http://ruby-doc.org/core-1.9.3/Array.html |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (ns wc.core | |
| (:gen-class) | |
| (:use compojure.core) | |
| (:require [compojure.route :as route] ; Remember to import compojure "1.1.5" | |
| [ring.middleware.params :as parameters] ; in your project.clj | |
| [clojure.string :as string])) | |
| ; \space is a space character | |
| (def is-space? | |
| (fn [character] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ; Last time we covered two main things. Using Vim to bring the REPL inside your editor, | |
| ; and various ways to compose functions. | |
| ; We'd already passed functions in as arguments to other functions... | |
| (map inc [1 2 3]) | |
| (filter even? [1 2 3]) | |
| (reduce + [1 2 3]) | |
| ; We then saw an example of "wrapping" a function to form a new one. | |
| (def safe-inc (fnil inc 0)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Week 08 | |
| # Last week we built our own assert_equals to help our tests. | |
| def assert_equals(expected, actual, failure_message) | |
| if expected == actual | |
| "Ok!" | |
| else | |
| "Expected '" + expected.to_s + "', but found '" + actual.to_s + "': " + failure_message | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Week 09 | |
| # Last week we built our first class. | |
| # | |
| # Remember, in Ruby, we usually group data and functions together into | |
| # things we call "objects". | |
| # | |
| # An object, is a specific example of a "class". For example, Fred might | |
| # be an object from the class Person. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Last week we worked with our own classes. | |
| # | |
| # Remember, in Ruby, we usually group data and functions together into | |
| # things we call "objects". | |
| # | |
| # An object, is a specific example of a "class". For example, Fred might | |
| # be an object from the class Person. | |
| # | |
| # A group of people might be an object from the class Family. | |
| # |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Family | |
| def initialize(persons) | |
| @people = persons | |
| end | |
| def youngest() | |
| @people.min do |person1, person2| | |
| person1.age - person2.age | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Person | |
| def initialize(first_name, last_name, age) | |
| @first_name = first_name | |
| @last_name = last_name | |
| @age = age | |
| end | |
| def name() | |
| @first_name + " " + @last_name | |
| end |