why does this work?
This is just something contrived I was playing around with and I know it is silly. I just have a couple of questions about it. (defn create-a [firstName lastName] (defn getFirstName [] firstName) (defn getLastName [] lastName) (fn [operator & operands] (apply operator operands))) (def t (create-a "Kurt" "Z")) ;How or why does this next line work? What is "getFirstName" in this context? (t getFirstName) ;I'm not surprised this doesn't work. (t 'getFirstName) ;And I'm not surprised this doesn't work. (defn getLastName "blah") (t getLastName) Kurt --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: why does this work?
Hi Achim, I think I understand now. > It's important to note that, regardless of the lexical context, def(n) > creates global/top-level definitions (contrary to scheme, i believe). > (defn create-a [firstName lastName] (defn getFirstName [] firstName) (defn getLastName [] lastName) (fn [operator & operands] (apply operator operands))) (def t (create-a "Kurt" "Z")) (def r (create-a "Bob" "hope")) (t getFirstName) --> returns "Bob" instead of "Kurt" And this is the case because despite where defn is declared, it is a top level definition in the current name space. Thus calling "def r" above redefines getFirstName and getLastName. Thanks for clearing this up. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: why does this work?
Thanks Chouser, No, I really didn't have any intentions with this example. I was merely exploring around. In hindsight it is also obvious why this works. (defn foo[] (defn bar [x] (* x 2)) nil) (foo) (bar 2)---> returns 4 I just didn't realize initially what was going on... that defn binds globally. And yeah, this could be abused to allow some kind of weird mutability. I was't going for that. :) What I originally wrote was this (with camel case corrected) (defn create-a [first-name last-name] (let [fnc-map {'type (fn [] "person") 'get-first-name (fn [] first-name) 'get-last-name (fn [] last-name) 'name-length (fn [] (+ (count first-name) (count last-name)))}] (fn [fnc-symbol & args] (apply (fnc-map fnc-symbol) args ((create-a "Kurt" "Z") 'get-first-name) (def bob-vance (create-a "Bob" "Vance")) (bob-vance 'name-length) (bob-vance 'get-first-name) I wasn't completely satisfied with the example because it seemed I could make it a bit more abstract. Before I went down the path of writing a macro, I ended up stumbling on some strangeness with defn. If you are curious as to my intentions, I have been watching the SICP lecture series. And I was a bit intrigued in alternative ways of implenting simple "types" in a lisp instead of having a lookup table. Kurt --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
time complexity for immutable priority queue?
After helping tutor some students earlier in the week on the subject of priority queues, I ended up implementing it in Clojure as a mutable data structure. It was straight forward, but curiosity struck and I implemented the priority queue as an immutable data structure. I'm pretty sure that 'ffirst 'first 'count and 'rest have a constant time complexity, but it is a little hard to wrap my mind around this code. Is it really constant time? ;- immutiable priority queue - (defn priority-cons [priority data queue] (defstruct element :priority :data) (let [elem (struct element priority data) queue-first (if (nil? queue) nil (queue 'ffirst)) queue-rest (cond (nil? queue) nil (nil? elem) (queue 'rest) (>= priority (queue-first :priority)) queue :else (priority-cons priority data (queue 'rest)))] (fn [op] (cond (= op 'count) (cond (nil? queue) 1 (nil? elem) (queue 'count) :else (inc (queue 'count))) (= op 'first) (cond (nil? queue) data (nil? elem) nil (>= priority (queue-first :priority)) data :else (queue-first :data)) (= op 'ffirst) (cond (nil? queue) elem (nil? elem) nil (>= priority (queue-first :priority)) elem :else queue-first) (= op 'rest) queue-rest ;--- testing code (defn print-priority-queue [queue] (loop [queue queue] (if (nil? queue) nil (do (println (queue 'first)) (recur (queue 'rest)) (def a (priority-cons 10 "hello" nil)) (print-priority-queue a) (a 'count) (def b (priority-cons 20 "hello2" a)) (print-priority-queue b) (b 'count) (def c (priority-cons 15 "hello-m" b)) (print-priority-queue c) (c 'count) ((c 'rest) 'count) (((c 'rest) 'rest) 'first) (((c 'rest) 'rest) 'count) (((c 'rest) 'rest) 'rest) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: time complexity for immutable priority queue?
(if (= (qcount big-q) 1) nil (do (qremove big-q) (recur) ;useful for testing mutable priority queue (defn print-forward [element] (loop [element element count 0] (if (or (nil? element) (> count 8)) nil (do (let [print-element #(print (%1 :priority) (%1 :data)) prev-element @(element :previous-element) next-element @(element :next-element)] (print count ": ") (print-element element) (println) (print " --- prev --- ") (if (nil? prev-element) nil (print-element prev-element)) (println) (print " --- next --- ") (if (nil? next-element) nil (print-element next-element)) (println)) (recur @(element :next-element) (inc count)) On Mar 1, 3:36 am, Achim Passen wrote: > Hi, > > that's a nice example of using closures to store data! > > (f)first and rest look like constant time to me. count is linear time, > but could easily be made constant time by storing the counts instead > of recursing. > > Insertion is linear time though, plus it recurses, resulting in stack > sizes in the order of the length of the queue. > > Here's a version that's roughly equivalent (including the recursion > problem), but uses maps instead of closures: > > (defn prio-insert [queue elem prio] > (if (> prio (:prio queue (Integer/MIN_VALUE))) > {:first elem > :prio prio > :count (inc (:count queue 0)) > :rest queue} > (let [new-rest (prio-insert (:rest queue) elem prio)] > (assoc queue > :rest new-rest > :count (inc (:count new-rest)) > > user=> (def pq (prio-insert nil 3 4)) > #'user/pq > user=> pq > {:first 3, :prio 4, :count 1, :rest nil} > user=> (def pq (prio-insert pq 2 10)) > #'user/pq > user=> pq > {:first 2, :prio 10, :count 2, :rest {:first 3, :prio 4, :count > 1, :rest nil}} > user=> (def pq (prio-insert pq 4 1)) > #'user/pq > user=> pq > {:first 2, :prio 10, :count 3, :rest {:first 3, :prio 4, :count > 2, :rest {:first 4, :prio 1, :count 1, :rest nil}}} > user=> (:first (:rest pq)) > 3 > > Kind regards, > achim > > Am 01.03.2009 um 06:31 schrieb zoglma...@gmail.com: > > > > > After helping tutor some students earlier in the week on the subject > > of priority queues, I ended up implementing it in Clojure as a mutable > > data structure. It was straight forward, but curiosity struck and I > > implemented the priority queue as an immutable data structure. I'm > > pretty sure that 'ffirst 'first 'count and 'rest have a constant time > > complexity, but it is a little hard to wrap my mind around this code. > > Is it really constant time? > > > ;- immutiable priority queue - > > > (defn priority-cons [priority data queue] > > (defstruct element :priority :data) > > (let [elem (struct element priority data) > > queue-first > > (if (nil? queue) > > nil > > (queue 'ffirst)) > > queue-rest > > (cond > > (nil? queue) > > nil > > (nil? elem) > > (queue 'rest) > > (>= priority (queue-first :priority)) > > queue > > :else > > (priority-cons priority data (queue 'rest)))] > > (fn [op] > > (cond (= op 'count) > > (cond > > (nil? queue) > > 1 > > (nil? elem) > > (queue 'count) > > :else > > (inc (queue 'count))) > > (= op 'first) > > (cond > > (nil? queue) > > data > > (nil? elem) > > nil > > (>= priority (queue-first :priority)) > > data > > :else > > (queue-first :data)) > > (= op 'ffirst) > > (cond > > (nil? queue) > > elem > > (nil? elem) > > nil > > (>= priority (queue-first :priority)) > > elem > > :else > > queue-first) > > (= op 'rest) > > queue-rest > > > > > ;--- testing code > > > (defn print-priority-queue [queue] > > (loop [queue queue] > > (if (nil? queue) > > ni
contrib mmap/duck_streams for binary data
While playing around and implementing straight up Humman compression, I wrote a handful of utilities to conveinently play with byte and bit streams because I didn't see anything too helpful in the mmap and duck_stream files. What I wrote would need to be changed to better work with the existing code and to increase performance, but does anyone think that it would be helpful to add these things for playing with binary data into contrib? ; to exercise everything -- use this to copy a file -- obviously change the files (time (to-file "/Users/kaz/Desktop/Programming Clojure-copy.pdf" (bit- to-byte-stream (byte-to-bit-stream (to-byte-stream "/Users/kaz/Desktop/ Programming Clojure.pdf") ; to play with Huffman compress/decompression (compress-file "/Users/kaz/Desktop/onlisp.pdf" "/Users/kaz/Desktop/ onlisp.pdf.compress") (uncompress-file "/Users/kaz/Desktop/onlisp.pdf.compress" "/Users/kaz/ Desktop/onlisp2.pdf") ; ;-- setup files to act like streams at the bit level - ; (defn array-to-list [arr list-size] (loop [index 0 accum []] (if (or (= index (alength arr)) (= index list-size)) accum (recur (inc index) (conj accum (aget arr index)) (defn list-to-array [l array-type arr-size] (let [arr-size (min arr-size (count l)) arr (make-array array-type arr-size)] (loop [index 0 l l] (if (= index arr-size) arr (do (aset arr index (first l)) (recur (inc index) (rest l))) (defn num-to-bits [num size] (loop [num num size size accum nil] (if (= size 0) accum (let [next-n (bit-shift-right num 1) next-bit (bit-xor num (bit-shift-left next-n 1))] (recur next-n (dec size) (cons next-bit accum)) (import '(java.io FileInputStream FileOutputStream)) (defn to-byte-stream [filename] (let [bufsize 65536 is (FileInputStream. filename) read-buf (fn [] (let [buf (make-array (Byte/TYPE) bufsize) num-read (.read is buf 0 bufsize)] (if (= num-read -1) (do (.close is) nil) (array-to-list buf num-read read-all (fn read-all [] (let [next-buf (read-buf)] (if (nil? next-buf) nil (lazy-cat next-buf (read-all)] (read-all))) (defn byte-to-bit-stream [l] (if (nil? l) nil (lazy-cat (num-to-bits (first l) 8) (byte-to-bit-stream (rest l) (defn bits-to-num [l num-bit-size] (let [cnt (count (take num-bit-size l))] (if (< cnt num-bit-size) (bits-to-num (lazy-cat l (take (- num-bit-size cnt) (repeat 0))) num-bit-size) (loop [l l size num-bit-size accum 0] (if (= size 0) (list accum l) (recur (rest l) (dec size) (bit-or (bit-shift-left accum 1) (first l (defn bit-to-byte-stream [l] (if (nil? l) nil (let [[next-byte rst] (bits-to-num l 8)] (lazy-cons next-byte (bit-to-byte-stream rst) (defn to-file [filename byte-stream] (let [os (FileOutputStream. filename)] (loop [bytes byte-stream bytes-written 0] (if (nil? bytes) (do (.close os) bytes-written) (let [[bytes rest-bytes] (split-at 65536 bytes) buf (list-to-array (map byte bytes) (Byte/TYPE) 65536) wrote-now (alength buf)] (do (.write os buf 0 wrote-now) (recur rest-bytes (+ bytes-written wrote-now ; ;-- Using byte/bit streams to do huffman - ; (defn get-occurances [l] (loop [l l accum {}] (if (empty? l) accum (let [next (first l) current-count (if (nil? (accum next)) 0 (accum next)) next-accum (assoc accum next (inc current-count))] (recur (rest l) next-accum) (defn tree-has-children? [tree] (and (not (nil? tree)) (or (not (nil? (tree :left-child))) (not (nil? (tree :right-child)) (defn tree-has-lchild? [tree] (and (not (nil? tree)) (not (nil? (tree :left-child) (defn tree-has-rchild? [tree] (and (not (nil? tree)) (not (nil? (tree :right-child) (defn tree-count [tree] (if (nil? tree) 0 (+ 1 (if (tree-has-lchild? tree) (tree-count (tree :left-child)) 0) (if (tree-has-rchild? tree) (tree-count (tree :right-child)) 0 (defn get-hufftree [occurances] (let [build (fn build [occurances] (let [sorted-occurances (sort-by (fn [[tree val num-occur]] num- occur) occurances)]