Re: Basic string modification question

2014-04-03 Thread Andy Smith
Thanks Josef, I shall digest that reply very closely. I had actually initially tried assoc on the string as you described. (You guys are great. This is why Im really enjoying learning clojure.) -- You received this message because you are subscribed to the Google Groups "Clojure" group. To pos

Re: Basic string modification question

2014-04-03 Thread Andy Smith
; > Jozef > > > On Wed, Apr 2, 2014 at 7:49 PM, Andy Smith > > > wrote: > >> just a shame its not in the core, no? >> >> >> On Wednesday, 2 April 2014 18:06:03 UTC+1, A. Webb wrote: >>> >>> Using subs (no need for join) is the way

Re: Basic string modification question

2014-04-02 Thread Andy Smith
just a shame its not in the core, no? On Wednesday, 2 April 2014 18:06:03 UTC+1, A. Webb wrote: > > Using subs (no need for join) is the way I would go, just define > > > (defn replace-at [s n c] (str (subs s 0 n) c (subs s (inc n > > (replace-at "hello" 1 "a") ;=> "hallo" > > and ca

Re: Basic string modification question

2014-04-02 Thread Andy Smith
I guess my point is that if the java function is so good, then why doesnt the clojure library thinly wrap it, so that your code remains portable clojure? On Wednesday, 2 April 2014 12:50:00 UTC+1, guns wrote: > > On Wed 2 Apr 2014 at 04:06:40AM -0700, Andy Smith wrote: > > &

Re: Basic string modification question

2014-04-02 Thread Andy Smith
the first isnt pure clojure wo I would probably try to avoid this... e.g. what if I want to port to clojureCLR? The second 'looks' quite a roundabout way of simply manipulating a string? How would the following compare for performance? (defn replace-substring [s r start len] (str (subs s 0 star

Basic string modification question

2014-04-02 Thread Andy Smith
Hi, I see there are a lot of functions strings, but nothing that allows me to create a new string with a character replaced at a given index? Am I meant to use subs and join to do this? It seems a bit long-winded? I wonder why there isnt a helper function out of the box to do this (or is there?

Re: updating the last member

2014-03-27 Thread Andy Smith
I was hoping there would be something like the :last keyword out of the box? -- 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 Note that posts from new members are moderated - please be patie

Re: updating the last member

2014-03-27 Thread Andy Smith
P.P.S My assumption that I could use lists fails because lists arent associative so the following doesnt work either : (let [xs '(1 2 3)] (assoc xs 0 2)) -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@g

Re: updating the last member

2014-03-27 Thread Andy Smith
P.S. in the above, I am assuming that the length of the leaf vector [1 2 3] is not known : i.e. I cant call (assoc-in [1 [1 2 [1 2 3]]] [1 2 2] 5) -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googleg

updating the last member

2014-03-27 Thread Andy Smith
Hi, (1) I'm sure there must be a simpler way to update the last element of a vector other than dec and count : (let [xs [1 2 3]] (assoc xs (dec (count xs)) 4)) This gets messy when updating vectors in nested data structures : (update-in [1 [1 2 [1 2 3]]] [1 2] #(assoc % (dec (count %)) 5))

creating a map

2014-03-26 Thread Andy Smith
Hi all, I was wondering why this doesn't create a map 1 -> 2 : (into {} (partition 2 2 "12")) Must be yet another misunderstanding of mine. Thanks Andy -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@g

Re: apply to quoted form

2014-03-22 Thread Andy Smith
After the above discussion, I suspect that the point of this 4clojure question is that using eval or resolve opens up the possibility for a user of the function to inject arbitrary code into the 'Universal Computation Engine', whereas using a map necessarily restricts the operations to the 'all

Re: apply to quoted form

2014-03-22 Thread Andy Smith
my bad, the above example is wrong it is meant to return a function of the environment mappings : (fn !! [c] (fn [m] (let [ [f & as] (map #(if (sequential? %) ((!! %) m) (let [x (m %)] (if x x %))) c)] (apply ({'/ / '+ + '- - '* *} f) as --

Re: apply to quoted form

2014-03-22 Thread Andy Smith
Secondly, 4clojure tells me use of resolve is bad as well : i.e. the following is my latest code (fn evaluate [c m] (let [ [f & as] (map #(if (sequential? %) (evaluate % m) (let [x (m %)] (if x x %))) c)] (apply (resolve f) as))) -- You receive

Re: apply to quoted form

2014-03-22 Thread Andy Smith
Ah I think I get it. 'x is a symbol pointing to a var which is a box that contains some value (i.e. which could be a function) i.e. the following def creates a var, puts 1 into it, then creates a symbol 'x and finally adds it to the user namespace : user=> (def x 1) #'user/x we can see it in t

Re: apply to quoted form

2014-03-21 Thread Andy Smith
that is 'problem 121 from 4clojure' http://www.4clojure.com/problem/121 -- 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 Note that posts from new members are moderated - please be patient

apply to quoted form

2014-03-21 Thread Andy Smith
I came across the following issue when doing problem user=> (let [f '(+ 1 1)] (apply (first f) (rest f))) 1 I could use eval but eval is bad (apparently)... Investigating this further I can see that : user=> (let [f '(+ 1 1)] (type (first f))) clojure.lang.Symbol wheras : user=> (type +) clo

Re: threading operators

2014-03-21 Thread Andy Smith
gt; out of date makes this sort of thing harder to find): > > (as-> "/tmp" x > (foo x) > (bar 1 2 x) > (baz x 3 4) > (quux 5 x 6)) > > > > On Fri, Mar 21, 2014 at 7:08 AM, Paul L. Snyder > > > wrote: > >> >>

Re: How did you learn Clojure?

2014-03-21 Thread Andy Smith
Im in the process of learning, and I found that the clojuretv talks and presentations on youtube were useful. Also the video and sample code for Rich Hickey's Ants demo is an excellent intro to how concurrency works in clojure : https://www.youtube.com/watch?v=dGVqrGmwOAw https://gist.github.co

Re: threading operators

2014-03-21 Thread Andy Smith
Im wondering if it is worthwhile to create a macro to thread together arbitrary forms (x, f, g, h) injecting the result into different positions into the list as required? (thread-together (-> x f ->> g -> h)) -- You received this message because you are subscribed to the Google Groups "Clo

Re: threading operators

2014-03-21 Thread Andy Smith
yes I saw that, but it only works from thread-first to thread-last? i.e. this works (-> x (->> f g h)) but I think the following would fail : (->> x (-> f g h)) -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to c

Re: local mutable state

2014-03-21 Thread Andy Smith
Excellent point... thanks for the insight On Thursday, 20 March 2014 20:34:47 UTC, tbc++ wrote: > > Not to mention that this isn't local mutation. You are handing the atom to > a closure that then gets wrapped by lazy-seq and returned. So the atom may > actually sit around for some time. > --

Re: local mutable state

2014-03-21 Thread Andy Smith
Im also interested as to why the mutable state approach would be less performant? In the single thread case the locks would be optimized out right? -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googleg

threading operators

2014-03-21 Thread Andy Smith
I have a chain of operations where i want to use a mixture of -> and ->> (i.e. some functions expect the previous result to be fed into the second item in its sexpr and some expecting it as the last item of its sexpr. What is the neatest way to chain such functions together. perhaps I should wri

Re: local mutable state

2014-03-20 Thread Andy Smith
Also from the proteus link, i read 'these variables cannot escape the local scope; if passed into a function or closed over', which is a problem with code such as in my example I suppose. I wasnt trying to avoid the functional approach but just being devils advocate. Thanks for your comments.

local mutable state

2014-03-20 Thread Andy Smith
Is is very bad form to use local mutable state to solve problems like : https://www.4clojure.com/problem/114 i.e. (fn [n f xs] (let [c (atom 0)] (take-while #(if (f %) (> n (swap! c inc)) true) xs))) If so, what is the strongest reason to reject this kind of code? Since its a local atom it

Re: reduced

2014-03-19 Thread Andy Smith
ah ok... i worked around this using a restriction on the input collection, but i think it is less concise : (fn [x b] (if (zero? x) [0] (reduce #(let [r (quot x %2) d (mod r b)] (conj %1 d)) '() (take-while #(<= % x) (iterate (partial * b) 1) -- You received this message because you are s

reduced

2014-03-19 Thread Andy Smith
Hi, Ive been looking at http://www.4clojure.com/problem/137 and I have a solution : (fn [x b] (reduce #(let [r (quot x %2) d (mod r b)] (if (zero? r) (reduced %1) (conj %1 d))) '() (iterate (partial * b) 1))) but the 4clojure website is failing with : java.lang.RuntimeException: Unable to re

Re: clojure lazy-seq caching?

2014-03-04 Thread Andy Smith
Does this mean that in a single threaded application lazy sequences suffer the overhead of locks? I thought one of the features of clojure is that it tries to avoid locks as much as possible. Andy -- You received this message because you are subscribed to the Google Groups "Clojure" group. To

Re: use/require/import and quoted form

2014-03-02 Thread Andy Smith
Oh I see, so we are using exactly the right datatype for the purpose... thanks :o) -- 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 Note that posts from new members are moderated - please b

clojure lazy-seq caching?

2014-03-02 Thread Andy Smith
Hi, I understand that lazy-seq caches the computed result as each element is read. However, how can is this made to be thread-safe? Andy -- 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 N

LazySeq.seq()

2014-02-28 Thread Andy Smith
Hi, Looking at the codefor LazySeq.seq() I was wondering why a 'while' loop is used rather than an 'if' and a recursive call to ls.seq(), which would be cleaner code I guess? Am I right to suspect this simply an

Re: map and lazy sequence

2014-02-28 Thread Andy Smith
doh!, thanks for the above. I actually did read this a few weeks ago but totally forgot about it. :o/ -- 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 Note that posts from new members are m

map and lazy sequence

2014-02-28 Thread Andy Smith
Hi, Can someone correct my misunderstanding here. I was lead to believe that map produced a lazy sequence, so why do I get three printed trace lines in the following code : user=> (take 1 (map #(do (println (str "trace:" %)) %) [1 2 3])) (trace:1 trace:2 trace:3 1) Thanks for your help Andy

Re: use/require/import and quoted form

2014-02-16 Thread Andy Smith
(require 'clojure.string) Im just wondering why the language chose to use the quote form, rather than a string or a keyword e.g. (require "clojure.string") (require :clojure.string) There obviously must be a good reason why. -- You received this message because you are subscribed to the G

use/require/import and quoted form

2014-02-06 Thread Andy Smith
Hi, I was wondering why use/require and import take quoted forms as their arguments, other alternatives could be strings or keywords, so what is special about the choice of quoted form here? Andy -- You received this message because you are subscribed to the Google Groups "Clojure" group. To

diagram of clojure interfaces and objects

2014-02-05 Thread Andy Smith
Hi, Does anyone know of a class diagram of all the java interfaces and classes that exist in clojure, including their inheritance relationships. Im trying to get a handle on all the many collection/sequence interfaces, but ideally I would like to visually grok the whole fundamental clojure obje

Re: Why do I get stackoverflow error?

2014-02-04 Thread Andy Smith
Similarly this works for my non-recursive effort, which I think is more concise : (fn [n] (nth (iterate (fn [r] (map (partial reduce +) (doall (partition 2 1 (concat [0N] r [0]) [1]) (dec n))) -- You received this message because you are subscribed to the Google Groups "Clojure" group. To

Re: Why do I get stackoverflow error?

2014-02-04 Thread Andy Smith
Ok thanks, thats really helpful. The second link suggests using doall, which seems to do the trick : ((fn pascal ([n] (pascal n [1M])) ([n row] (if (= n 1) row (recur (dec n) (map (partial reduce +) (doall (partition 2 1 (concat [0] row [0] 500) However you do lose the laziness, but he

Re: Why do I get stackoverflow error?

2014-02-03 Thread Andy Smith
Correction problem #97 I mean On Monday, 3 February 2014 21:19:36 UTC, Andy Smith wrote: > > Hi, > > I am working through the 4clojure questions, I have a few different > solutions to problem 87 but all of them run out of stack space. I tried to > convert to using recur bu

Why do I get stackoverflow error?

2014-02-03 Thread Andy Smith
Hi, I am working through the 4clojure questions, I have a few different solutions to problem 87 but all of them run out of stack space. I tried to convert to using recur but I still have the problem. Why does this fail for large n? ((fn pascal ([n] (pascal n [1])) ([n row] (if (= n 1) row (rec

Re: let bindings

2014-01-21 Thread Andy Smith
Excellent post, thank you for that. -- -- 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 Note that posts from new members are moderated - please be patient with your first post. To unsubscr

Re: let bindings

2014-01-20 Thread Andy Smith
typo : was=way -- -- 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 Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group,

let bindings

2014-01-20 Thread Andy Smith
Hi, (let bindings form) is a special form. As I understand it, let can be reformulated in terms of functions e.g. (let [x 2] (* x 20)) equivalent to ((fn [x] (* x 20)) 2) (let [x 3 y (* x x)] (- y x)) equivalent to ((fn [x] ((fn [x y] (- y x)) x (* x x))) 3) So if we can always reformulate i

Re: I need a vector not a list?

2013-12-02 Thread Andy Smith
Great point... -- -- 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 Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group,

Re: I need a vector not a list?

2013-12-01 Thread Andy Smith
indirection to avoid the need to copy elements of the original collection? On Saturday, 30 November 2013 21:31:34 UTC, Jim foo.bar wrote: > > On Sat, 30 Nov 2013 13:15:33 -0800 (PST) > Andy Smith > wrote: > > > but > > my question is really about the more general cas

Re: I need a vector not a list?

2013-12-01 Thread Andy Smith
object. > > Try this => (mapv #(* 2 %) [1 2 3]) > Not this => (apply vector (map (partial * 2) [1 2 3])) > > > 01.12.2013, 06:15, "Andy Smith" >: > > Hi, > > I am trying to understand the manipulation of vectors from an efficiency > point

I need a vector not a list?

2013-11-30 Thread Andy Smith
Hi, I am trying to understand the manipulation of vectors from an efficiency point of view. For example if I want to reverse a vector I can do the following (apply vector (reverse [1 2 3])) My understanding is that reverse will create a new list object (3 2 1) then this will be used to const

hash comparison

2013-11-28 Thread Andy Smith
Hi, I heard Rich Hickey talking about how identity in clojure is synonymous with value-based tests of equality. To make this efficient he describes that objects store cached hashes that are used to speed up these tests of equality, so clojure isnt comparing every data member of a complex data

Re: expand a form

2013-11-28 Thread Andy Smith
thanks for your helpful suggestions. -- -- 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 Note that posts from new members are moderated - please be patient with your first post. To unsubsc

Re: expand a form

2013-11-25 Thread Andy Smith
n silly example, even though this kind of macro is not needed: > > (def addone [v] > (+ v 1) > > (defmacro testmacro [init] >(list 'addone init)) > > (macroexpand '(testmacro 10)) > > expands to > > (addone 10) > > Thanks > Guru > > &

Re: expand a form

2013-11-25 Thread Andy Smith
It doesnt seem to expand function calls though right? On Monday, 25 November 2013 12:55:27 UTC, Andy Smith wrote: > > Hi, > > I am new to clojure and I was wondering if there is a macro I can use to > fully expand all symbols and macros in a form, without performing the final

expand a form

2013-11-25 Thread Andy Smith
Hi, I am new to clojure and I was wondering if there is a macro I can use to fully expand all symbols and macros in a form, without performing the final evaluation of the built in functions? Thanks Andy -- -- You received this message because you are subscribed to the Google Groups "Clojure

Re: running command when lein repl starts

2013-11-19 Thread Andy Smith
oops, you are right but if I paste into my project.clj I get a different error when I run lein repl andy@Aspire-V3-571:~/projects/clojure/test$ lein repl Exception in thread "main" java.lang.IllegalArgumentException: No value supplied for key: [:init (use (quote clojure.math.numeric-tower))] (N

Re: running command when lein repl starts

2013-11-19 Thread Andy Smith
yes, Ive seen that but it doesnt seem to help me greatly. Just out of curiosity how do you generally setup your repl so it already includes these kind of common libraries? I dont really want to be typing lots of 'use' commands into the repl every time i start it. Is using leiningen the wrong wa

Re: running command when lein repl starts

2013-11-19 Thread Andy Smith
Ok valid point, but I still get the same kind of errors? -- -- 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 Note that posts from new members are moderated - please be patient with your fi

running command when lein repl starts

2013-11-19 Thread Andy Smith
Hi, How can I get a command such as '(use 'clojure.math.numeric-tower)' to run when I start the repl with 'lein repl' ? I currently have my leiningen project setup as the following : (defproject test "1.0.0-SNAPSHOT" :description "FIXME: write description" :dependencies [[org.clojure/clojur

configure leiningen to use library when repl starts

2013-11-19 Thread Andy Smith
Hi, total newbie here and have come unstuck with the repl configuration. How can I configure my leiningen project's clj file to call '(use 'clojure.math.numeric-tower)' when the repl is started with 'lein repl'? I have tried adding the command to the repl-options but I get a java exception wh