Clojure lazy sequences and lab 2 (part 1)


(ns lazy)
(use 'clojure.repl) ; to be able to look at function source code

;; Clojure documentation:
(println [
 ;         (source map)
 ;        (doc map)
 ;         (meta map)
 ;         (dir clojure.repl)
])


;;; Clojure sequences and operations on them 
;;; Start learning about laziness and efficiency 

;;; Two main things: 
;; Clojure is translated into Java bytecode 
;; (Just about) everything in clojure is a sequence


;; Lists and vectors are sequences we already know 
;; Other Clojure collections (sequences): maps, sets 
;; Lazy sequences
;; Java seq-able elements: Java collections, arrays, strings,
;; Other things: I/O streams, regular expression matches,
;; and more!

(println [
          (class '(1 2 3)) "\n"
          (class [1 2 3])  "\n"
          (class (rest [1 2 3])) "\n"
          (class (rest '(1 2 3)))
])


;; Operations applicable to all sequences: cons, first, rest

;; more general operations: conj
(println [
          (conj [6 7] 8)
          (conj [6 7] 8 9)
          (conj '(6 7) 8) ; Note the difference in order!!!
          (conj '(6 7) 8 9)
          (nth [4 5 6 7 8] 1)
          (nth [4 5 6 7 8] 3)
])

;; Lab 2 Problem 1:
;; 1. explore a function "into" (it works on any two collections)
;; use the documentation, then show its work on lists and vectors
;; 2. explore a function "subvec" that gives a subvector. 
;; what happens when you pass one parameter to it? Explain clearly,
;; give examples. 

(println [
          :name ; a keyword. Often used for indexing. 
])

;; maps: pairs, the first element is treated as a key, the 
;; second one as a value
(def phone-book {:Sam "589-9999" :Chris "585-1234"})
 
(println phone-book)

(println [
          (phone-book :Sam)
          (:Sam phone-book)
          (keys phone-book)
          (vals phone-book)  
          (contains? phone-book :Sam)
          (contains? phone-book :Kim)
])

;; Lab 2 Problem 2:
;; figure out how to add a new person and phone number to the 
;; phone book, how to change a phone number for an existing person,
;; and how to remove an entry from a phone book
;; Keep in mind that collections are immutable so you will get
;; a new phone book rather than modify an existing one
;; Hint: use functions assoc and dissoc

;; Lazy sequences.

(println [
          "Class of range: \n"
          (class (range 1 30 2)) 
          "\nDoc for range function\n"
          (meta range)
])


(println [
          (nth (range 1 100000000 4) 16) "\n"
          (nth (range) 5) "\n" ;; Taking the 5-th element of an inifnite sequence
          (doall (range 1 20 4)) "\n" ;; forcing a lazy sequence
          (nth (doall (range 1 10000000 4)) 16) ;; note the execution time!
])

;; Lab 2 Problem 3:
;; Study functions map, filter, rest, concat. Check if they return 
;; lazy sequences. Take two functions that return lazy sequences and
;; create a sequence of that would've taken a very long time (or would've 
;; caused a heap overflow) it weren't lazy, but is fast and memory efficient 
;; because of lazy sequences. Note: a composition of lazy functions is lazy, 
;; use ranges as a strating point.  


UMM CSci 4651