Re: side-effect only function with map-like syntax (again)

2014-01-28 Thread Mars0i
Jozef,

Thanks--good advice about names.

Well, I don't know whether useful is the right word.  domap (nee mapc) 
obviously wouldn't add any functionality.  (Neither does 'map', I suppose, 
since we have 'for'.)  

I seem to be unusual in my affection for this construct outside of purely 
functional contexts--otherwise Clojure would already have it.  

For years I've been using 'mapc' or 'mapcar' for side effects in Common 
Lisp, or 'map' in Perl, etc.  Maybe I do tend prefer to stay away from 
variadic functions, too, other things being equal.  

Anyway, no problem--I can define domap for myself if I want, even if it 
doesn't have enough support to be part of the language.

Here's a cleaned-up, simplified version of something I wrote the other day:

(defn unmask!
  "Given a core.matrix vector representing a node mask, and an index into 
that vector, set the indexed element of the mask to 1."
  [mask idx]
(mx/mset! mask idx 1.0)) ; fyi: mset! modifies a vector element or 
matrix element

(defn add-nodes-to-network!
  [node-mask ...]
...
   (let [idxs-to-unmask (construct-seq-of-indexes-to-unmask  ...)
 unmask-node! (partial unmask! node-mask) ] 
 (domap unmask-node! idxs-to-unmask)))
 
There are lots of other ways to do this.  I'm sure many that are better 
relative to one goal or another.  To me the last line seems natural, and 
easy to understand.  Just my personal preference, though.



On Tuesday, January 28, 2014 9:27:13 AM UTC-6, Jozef Wagner wrote:
>
> Regarding names, I would not place a bang after it (!), as there is 
> nothing in that function itself that forbids its use inside transactions. 
> Functions which primary purpose is to produce side effects tend to begin 
> with 'do', so my suggestion is to name it 'domap'. 
>
> I am however still not convinced of its usefulness beyond very few 
> specific cases. If you often need to call some side-effecty function for 
> all items in the collection, consider extending that function so it accepts 
> a collection as an argument, or make it a variadic fn.
>
> JW
>
> On Tuesday, January 28, 2014 4:12:35 PM UTC+1, Mars0i wrote:
>>
>> On Tuesday, January 28, 2014 7:29:06 AM UTC-6, Stefan Kamphausen wrote:
>>>
>>> Does wrapping your map expression in a dorun do what you want?
>>>
>>
>> Yes, it does.  But that's more verbose, and as Jozef Wagner notes, it 
>> creates seqs unnecessarily.
>>
>> I like Jozef's solution.  However, I think a proper definition of mapc 
>> may have to be more complicated, in order to allow for multiple sequence 
>> arguments after the function argument.  Maybe as a macro that calls doseq 
>> in the end.
>>
>> If nothing like this exists, I'd call it "map!", since "mapc" is just an 
>> odd legacy name from Common Lisp.  The "!" doesn't mean that map! is 
>> guaranteed to have side-effects, but "!" doesn't usually guarantee that, 
>> anyway.   (I delight in CL's idiosyncrasies, but appreciate Clojure's more 
>> systematic elegance.)
>>
>

-- 
-- 
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, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: side-effect only function with map-like syntax (again)

2014-01-28 Thread Jozef Wagner
Regarding names, I would not place a bang after it (!), as there is nothing 
in that function itself that forbids its use inside transactions. Functions 
which primary purpose is to produce side effects tend to begin with 'do', 
so my suggestion is to name it 'domap'. 

I am however still not convinced of its usefulness beyond very few specific 
cases. If you often need to call some side-effecty function for all items 
in the collection, consider extending that function so it accepts a 
collection as an argument, or make it a variadic fn.

JW

On Tuesday, January 28, 2014 4:12:35 PM UTC+1, Mars0i wrote:
>
> On Tuesday, January 28, 2014 7:29:06 AM UTC-6, Stefan Kamphausen wrote:
>>
>> Does wrapping your map expression in a dorun do what you want?
>>
>
> Yes, it does.  But that's more verbose, and as Jozef Wagner notes, it 
> creates seqs unnecessarily.
>
> I like Jozef's solution.  However, I think a proper definition of mapc may 
> have to be more complicated, in order to allow for multiple sequence 
> arguments after the function argument.  Maybe as a macro that calls doseq 
> in the end.
>
> If nothing like this exists, I'd call it "map!", since "mapc" is just an 
> odd legacy name from Common Lisp.  The "!" doesn't mean that map! is 
> guaranteed to have side-effects, but "!" doesn't usually guarantee that, 
> anyway.   (I delight in CL's idiosyncrasies, but appreciate Clojure's more 
> systematic elegance.)
>

-- 
-- 
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, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: side-effect only function with map-like syntax (again)

2014-01-28 Thread Mars0i
On Tuesday, January 28, 2014 7:29:06 AM UTC-6, Stefan Kamphausen wrote:
>
> Does wrapping your map expression in a dorun do what you want?
>

Yes, it does.  But that's more verbose, and as Jozef Wagner notes, it 
creates seqs unnecessarily.

I like Jozef's solution.  However, I think a proper definition of mapc may 
have to be more complicated, in order to allow for multiple sequence 
arguments after the function argument.  Maybe as a macro that calls doseq 
in the end.

If nothing like this exists, I'd call it "map!", since "mapc" is just an 
odd legacy name from Common Lisp.  The "!" doesn't mean that map! is 
guaranteed to have side-effects, but "!" doesn't usually guarantee that, 
anyway.   (I delight in CL's idiosyncrasies, but appreciate Clojure's more 
systematic elegance.)

-- 
-- 
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, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: side-effect only function with map-like syntax (again)

2014-01-28 Thread Jozef Wagner
If side effects is all what is needed, I would skip the creation of lazy
seqs and do it with reduce:

user=> (defn mapc [f coll] (reduce (fn [_ v] (f v)) nil coll))
#'user/mapc
user=> (mapc println [1 2 3])
1
2
3
nil

JW


On Tue, Jan 28, 2014 at 2:29 PM, Stefan Kamphausen wrote:

> Does wrapping your map expression in a dorun do what you want?
>
> Stefan
>
> --
> --
> 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, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
-- 
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, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: side-effect only function with map-like syntax (again)

2014-01-28 Thread Stefan Kamphausen
Does wrapping your map expression in a dorun do what you want?

Stefan

-- 
-- 
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, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: side-effect only function with map-like syntax (again)

2014-01-28 Thread Mars0i
On Tuesday, January 28, 2014 1:17:58 AM UTC-6, Jan Herich wrote:
>
> How about doseq ? 
>

doseq can do the same work, and can do more, but it requires one to specify 
variable names to be used in each application of the function.  For some 
purposes a function like mapc seems clearer and more natural (according to 
personal taste, at least).  I like mapc for the same reason that I like map.

(In the example I gave, I used an inline function definition using # and %; 
in such examples doseq may be better, but that's only one kind of case.)

-- 
-- 
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, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: side-effect only function with map-like syntax (again)

2014-01-27 Thread Jan Herich
How about doseq  ? 

Dňa utorok, 28. januára 2014 5:28:04 UTC+1 Mars0i napísal(-a):
>
> Back in 2007 (
> https://groups.google.com/forum/#!searchin/clojure/mapc/clojure/x0PDu_D6mP0/3A7CZe-ZDWAJ),
>  
> Henk Boom raised the possibility of including in Clojure a function with 
> syntax like map, but semantics more like Common Lisp's mapc (or Scheme's 
> for-each, I believe).  It works just like Clojure map, but applies the 
> function to items in a sequence (sequences) only for side-effects.  A 
> sequence of results isn't generated, so there's no need for laziness.  
> There was discussion, and at one point Rich Hickey seemed to say that he 
> had added this kind of function, under the name 'scan'.  It looks like scan 
> was subsequently renamed to 'dorun' (http://clojure.org/old_news), but 
> dorun doesn't do what mapc does.  As far as I can tell, there is no such 
> function at present in Clojure.  Is that correct?
>
> There are other ways to get the same functionality, and it's easy to 
> define a simple version of mapc (or whatever it should be called).  
> However, I like the map syntax, even for side-effects, and don't want to 
> reinvent the wheel.  Even for small sequences, in which producing a 
> sequence as output isn't costly, using mapc tells readers of your code that 
> you're iterating over the list solely for side-effects.
>
> (defn mapc [f coll] (doseq [x coll] (f x)))
>
> For example, creating a mutable vector using core.matrix:
>
> => (def a (zero-vector :ndarray 5))
> #'popco.core.popco/a
> => a
> #
> => (mapc #(mset! a % -1) [0 2 4])
> nil
> => a
> #
>
>

-- 
-- 
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, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


side-effect only function with map-like syntax (again)

2014-01-27 Thread Mars0i
Back in 2007 
(https://groups.google.com/forum/#!searchin/clojure/mapc/clojure/x0PDu_D6mP0/3A7CZe-ZDWAJ),
 
Henk Boom raised the possibility of including in Clojure a function with 
syntax like map, but semantics more like Common Lisp's mapc (or Scheme's 
for-each, I believe).  It works just like Clojure map, but applies the 
function to items in a sequence (sequences) only for side-effects.  A 
sequence of results isn't generated, so there's no need for laziness.  
There was discussion, and at one point Rich Hickey seemed to say that he 
had added this kind of function, under the name 'scan'.  It looks like scan 
was subsequently renamed to 'dorun' (http://clojure.org/old_news), but 
dorun doesn't do what mapc does.  As far as I can tell, there is no such 
function at present in Clojure.  Is that correct?

There are other ways to get the same functionality, and it's easy to define 
a simple version of mapc (or whatever it should be called).  However, I 
like the map syntax, even for side-effects, and don't want to reinvent the 
wheel.  Even for small sequences, in which producing a sequence as output 
isn't costly, using mapc tells readers of your code that you're iterating 
over the list solely for side-effects.

(defn mapc [f coll] (doseq [x coll] (f x)))

For example, creating a mutable vector using core.matrix:

=> (def a (zero-vector :ndarray 5))
#'popco.core.popco/a
=> a
#
=> (mapc #(mset! a % -1) [0 2 4])
nil
=> a
#

-- 
-- 
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, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.