On Github redlibrarian / code4lib-clojure
#include <stdio.h> int main(){ int age; printf("Please enter your age: "); scanf("%d", &age); age += 5; printf("In five years you will be %d years old.\n", age); return 0; }
>> Please enter your age: 10 >> In five years you will be 15 years old.
class Student def initialize(name, age) @name = name @age = age end def years_old puts "I am #{age} years old." end end class School def initialize @students = [] names = ["Belinda", "Jane", "Charlotte", "Gina"] names.each do |name| students << Student.new(name, 15) end end def students @students.join(",") end def student_ages for student in @students student.years_old end end end
(defn is-prime? [n] (every? false? (map #(= 0 (mod n %)) (range 2 (Math/sqrt n)))))Ruby:
def is_prime?(num) return false if num <= 1 Math.sqrt(num).to_i.downto(2).each {|i| return false if num % i == 0} true end
In practice, a first-class object can be...
In imperative programming, scalar data types (integers, characters, etc) are usually first-class
In object-oriented programming, objects are first-class
In functional programming, functions are first-class
So, in imperative programming, we manipulate scalar data, in OO we manipulate objects, and in functional programming we manipulate functions
Wait a minute, what's a function?
A function is a procedure that returns a value
(defn double [x] (+ x x)) >> (double 5) >> 10
(defn first-word [sentence] (first (clojure.string/split sentence #" "))) >> (first-word "Now is the winter of our discontent") >> "Now"
Code and data are expressed as functions
Functions and values are equivalent and interchangeable
Data objects never change (no variables)
Given that (defn ten [] (double 5)) and 10 are equivalent and interchangeable, then it's clear that our code and our data are expressed in exactly the same way.
This has major implications for the way we think about and work with our data.
calendar.xml
International Lefthanders DayAugust13Rover's birthdayOctober12Groundhog DayFebruary2Kamehameha DayJune11
(use '[clojure.xml :only (parse)]) (def xml-doc (parse (java.io.File. "calendar.xml"))) (keys xml-doc) >> (:tag :attrs :content) (:tag xml-doc) >> :calendar (count (:content xml-doc)) >> 4 (:content (first (:content (first (:content xml-doc))))) >> ["International Lefthanders Day"] (type xml-doc) >> clojure.lang.PersistentStructMap
Example taken from http://gettingclojure.wikidot.com/cookbook:xml-html (slightly modified)