Re: A generalization of iterate

2015-09-28 Thread Yoshinori Kohyama
Hi nchurch and all.

Your idea to generalize `iterate` looks very cool for me.
I wrote another implementation not to consume stack.

(defn generate' [f h & r]
  (cons h
(lazy-seq
  (apply generate' f
(reverse (cons (apply f h r) (reverse r)))

will work, if it is o.k. that we can assume at least one argument.

Yoshinori Kohyama

-- 
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/d/optout.


Re: Lazy Sequence Results in Stack Overflow

2015-09-27 Thread Yoshinori Kohyama
Hi, Charles and all.

Here is my definition of prime numbers:
https://gist.github.com/kohyama/8e599b2e765ad4256f32

HTH.

Yoshinori Kohyama

-- 
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/d/optout.


Re: To refer an auto-gensymed symbol in an internal unquoted expression?

2013-11-27 Thread Yoshinori Kohyama
Meikel and people seeing this topic,

Meikels' works fine with fix of a typo: gensym("a") -> (gensym "a")

Code below to make it easier to distinguish a gensymed symbol  from its var 
name.

user=> ((fn [d] (let [a (gensym "foo")] `(fn [~a] ~(if d `(drop ~d ~a) 
a 3)
(clojure.core/fn [foo20] (clojure.core/drop 3 foo20))

user=> ((fn [d] (let [a (gensym "foo")] `(fn [~a] ~(if d `(drop ~d ~a) 
a nil)
(clojure.core/fn [foo25] foo25)

Thank you.

Best Regards,
Yoshinori Kohyama

-- 
-- 
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: To refer an auto-gensymed symbol in an internal unquoted expression?

2013-11-27 Thread Yoshinori Kohyama
Meikel,

I see! Thanks a lot.

Best regards,
Yoshinori Kohyama

-- 
-- 
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.


To refer an auto-gensymed symbol in an internal unquoted expression?

2013-11-27 Thread Yoshinori Kohyama
Hello clojurians.

Someone, please teach me how I can refer an auto-gensymed symbol of an 
external syntax-quoted expression from an internal unquoted expression?

A simplified example:

user=> ((fn [d] `(fn [a#] ~(if d `(drop ~d a#) `a#))) 3)
(clojure.core/fn [a__**__auto__] (clojure.core/drop 3 a__**
__auto__))

user=> ((fn [d] `(fn [a#] ~(if d `(drop ~d a#) `a#))) nil)
(clojure.core/fn [a__**__auto__] a__**__auto__)

But what I want is an expression generating
  (clojure.core/fn [a__**__auto__] (clojure.core/drop 3 a__**
__auto__))
if 'd' is 3, and
  (clojure.core/fn [a__**__auto__] a__**__auto__)
if 'd' is nil.

I know that I can do this with (fn [d] (if d `(fn [a#] (drop ~d a#)) `(fn 
[a#] a#))).
But this is a simplified example and I need the outermost syntax-quote for 
some reasons in my real situation.
What I want is an expression which I can use instead of `(drop ~d a#) and 
`a# in the first expression in the example above.

Thank you in advance.

Regards,
Yoshinori Kohyama

-- 
-- 
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: How to generalize these functions

2013-10-02 Thread Yoshinori Kohyama
Smart! That's just what I want to do. 

Thanks, Leif and the author of Prismimatic.

Best Regards,
Yoshinori Kohyama

-- 
-- 
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.


How to generalize these functions

2013-10-01 Thread Yoshinori Kohyama
Hi clojurians.

I have functions for nested maps.
They obviously have a same structure.
Does anyone teach me a way to generalize them with a macro, a function or 
an existing API?

(defn flatten-map [m kf vf]
  (into {}
((fn g [kv n]
   (if (map? n)
   (apply concat
 (keep (fn [[k v]] (g (conj kv k) v)) n))
   (if-let [kfkv (kf kv)]
 (if-let [vfn (vf n)]
   [[kfkv vfn]]
 [] m)))

(defn deep-merge [a b]
  (reduce (fn [a [kv n]] (assoc-in a kv n))
  a
  ((fn g [kv n]
 (if (map? n)
 (apply concat
   (map (fn [[k v]] (g (conj kv k) v)) n))
 [[kv n]]))
   [] b)))

(defn mapf [m f & args]
  (reduce (fn [a [kv n]] (assoc-in a kv n))
  {}
  ((fn g [kv n]
 (if (map? n)
 (apply concat
   (keep (fn [[k v]] (g (conj kv k) v)) n))
 (if-let [fna (apply f n args)]
   [[kv fna]])))
   [] m)))

https://gist.github.com/kohyama/6789280 (with examples as tests)

Regards,
Yoshinori Kohyama

-- 
-- 
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: To read and write bytes via Socket

2013-08-28 Thread Yoshinori Kohyama
Resolved.

I overlooked that OutputStream has
  void write(byte[] b, int off, int len)
.

With write(byte[] ...) of OutputStream instead of write(char[] ...) of 
BufferedWritersends I can send the bytes that I want to send.

Code:
  (require '[clojure.java.io :refer :all])
  (with-open [s (java.net.Socket. "*ip.of.test.server*" *port_of_test_server
*)]
(let [^java.io.OutputStream os (.getOutputStream s)
  ^bytes obuf (byte-array (map #(byte (if (< % 128) % (- % 256)))
   [0 1 127 128 255]))]
  (.write os obuf 0 5)
  (.flush os)))

sent [0x00, 0x01, 0x7f, 0x80, 0xff] to my test server.

Thank you, Armando and all.

Regards,
Yoshinori Kohyama


-- 
-- 
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: To read and write bytes via Socket

2013-08-28 Thread Yoshinori Kohyama
Armando, thank you for your reply.

Writing single value with
  void write(int c)
sends same 2 bytes for values larger than 127 as using
  void write(char[] cbuf, int off, int len)
of BufferedReader 
http://docs.oracle.com/javase/jp/6/api/java/io/BufferedWriter.html

Code:
  (require '[clojure.java.io :refer :all])
  (with-open [s (java.net.Socket. "*ip.of.test.server*" *port_of_test_server
*)]
(let [^java.io.BufferedWriter wtr (writer s)]
  (doseq [i [0 1 127 128 255]]
(.write wtr i))
  (.flush wtr)))
sends same bytes as code in my previous post.

I can't find other appropriate methods of BuffererdReader, Reader and 
OutputStreamReader
  http://docs.oracle.com/javase/jp/6/api/java/io/Writer.html
  http://docs.oracle.com/javase/jp/6/api/java/io/OutputStreamWriter.html
than
  void write(int c)
  void write(char[] cbuf)
  void write(char[] cbuf, int off, int len)
.

Does anyone have any information?
Thanks in advance.

Yoshinori Kohyama

-- 
-- 
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.


To read and write bytes via Socket

2013-08-28 Thread Yoshinori Kohyama
Hello all.

Please help me to read and write bytes via Socket.

For example, assumed that I want to send 5 bytes [0x00, 0x01, 0x7f, 0x80, 
0xff] to a TCP server.

I did:
(require '[clojure.java.io :refer :all])
(with-open [s (java.net.Socket. "*ip.of.test.server*" *port_of_test_server*
)]
  (let [^java.io.BufferedWriter wtr (writer s)
^chars obuf (char-array (map char [0 1 127 128 255]))]
(.write wtr obuf 0 5)
(.flush wtr

Then, my test server received bytes: [0x00, 0x01, 0x7f, 0xc2, 0x80, 0xc3, 
0xbf].

* I think I should use a char array so that read() requires 'char[]'.
* What is the valid char value to send a byte 0x80?
* How can I make the char value from 0x80 int?
* How are things about read.
* Does character encoding environment affect? (I use -Dfile.encoding=UTF-8)

Thank you in advance.

Yoshinori Kohyama

-- 
-- 
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: Can I refer all but specified symbols with 'ns' macro?

2013-08-01 Thread Yoshinori Kohyama
Carlo,

Works fine. Thank you!

Y. Kohyama

-- 
-- 
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.




Can I refer all but specified symbols with 'ns' macro?

2013-08-01 Thread Yoshinori Kohyama
Hi group.

Assumed that I want to refer 'baz, 'qux and etc and don't want to refer 
'quux of 'bar namespace within my 'foo namespace.

With 'ns' macro, I can require a namespace and refer all public symbols in 
it.

  (ns foo (:require [bar :refer :all]))

I can refer only some specific symbols.

  (ns foo (:require [bar :refer (baz qux)]))

Can I refer all but specific symbols?

I'm doing

  (ns foo (:require bar))
  (refer 'bar :exclude '(quux))

or

  (ns foo)
  (require 'bar)
  (refer 'bar :exclude '(quux))

for now.

Thanks in advance.

Y. Kohyama

-- 
-- 
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: Does this abstraction have any existing name?

2013-07-26 Thread Yoshinori Kohyama
Thanks Ben, for very useful informations!

2013年7月27日土曜日 13時31分41秒 UTC+9 Ben:
>
> if you want to use that fmap (or, I'd think, the fmaps provided by either 
> morph or fluokitten) with uneven depths, you'd have to wrap them in a 
> defrecord or deftype, I'd expect.
>

I see.

2013年7月27日土曜日 13時37分28秒 UTC+9 Ben:
>
> user=> (defmethod f/fmap Object [f v] (f v)) ;;; "Identity" functor
> #
> user=> (defmethod f/fmap clojure.lang.IPersistentMap [f v] (into {} (map 
> (fn [[k v]] [k (f/fmap f v)]) v)))
> #
> user=> (f/fmap inc {:a {:y 1} :b {:x 2 :z {:c [1 2 3]}}})
> {:a {:y 2}, :b {:z {:c [2 3 4]}, :x 3}}
>
> But I wouldn't recommend it.
>

One way.  It can be used, I think.

Thanks again.

Regards,
Y.Kohyama

-- 
-- 
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: Does this abstraction have any existing name?

2013-07-26 Thread Yoshinori Kohyama
Thank you, Jason.

The 'map-leaves' does the same as my 'mapf' and simpler.
Its name and implementation is very sophisticated with other functions doing
similar but different things.
Thanks the information.

Regards,
Y.Kohyama

2013年7月27日土曜日 13時31分28秒 UTC+9 Jason Wolfe:
>
> This is 'map-leaves' in our open-source lib plumbing.map (besides the 
> option for additional args):
>
> https://github.com/Prismatic/plumbing/blob/master/src/plumbing/map.clj
>
> Cheers, Jason
>

-- 
-- 
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: Does this abstraction have any existing name?

2013-07-26 Thread Yoshinori Kohyama
Thank you, Ben.

If I think the map as a tree, then as a functor, what I do is 'fmap' (in 
Haskell or some languages),
,as you say.
Thanks for saying that.

Does Algo supply things around Functor, Applicative and Monad?
I'm going to look at Algo.

Or anybody knows any other libraries or implementations around these things 
in Clojure?

Regards,
Y.Kohyama


2013年7月27日土曜日 13時06分23秒 UTC+9 Ben:
>
> If the map is a tree with values at the leaves, then it's a functor and 
> applying a function to the values is fmap.
>
>
> On Fri, Jul 26, 2013 at 6:02 PM, Dave Sann 
> > wrote:
>
>> if you are thinking of the nested map as a sort of tree - which you seem 
>> to be doing - then map-leaves or something similar might convey the intent.
>>
>> On Saturday, 27 July 2013 04:30:30 UTC+10, Jay Fields wrote:
>>>
>>> I'm not sure I'd call this more readable, but it's another option - 
>>> using clojure.walk
>>>
>>> (defn deep-update-vals
>>>   [m f & args]
>>>   (let [f (fn [[k v]] (if (map? v) [k v] [k (apply f v args)]))]
>>> (clojure.walk/postwalk (fn [x] (if (map? x) (into {} (map f x)) x)) 
>>> m)))
>>>
>>>
>>> On Fri, Jul 26, 2013 at 2:06 PM, Jay Fields  wrote:
>>>
>>>> I defined update-vals in jry: https://github.com/**
>>>> jaycfields/jry/blob/master/**src/clojure/jry.clj#L74-L75<https://github.com/jaycfields/jry/blob/master/src/clojure/jry.clj#L74-L75>
>>>>
>>>> It doesn't traverse nested maps, but I haven't ever needed that ability 
>>>> either.
>>>>
>>>> 1) I've never seen a name for that.
>>>> 2) not in core. I'm sure it's been written 50 times in various helper 
>>>> libs.
>>>> 3) I'd probably write it like below, but I'm not convinced it's any 
>>>> better.
>>>>
>>>> (defn deep-update-vals
>>>>   [m f & args]
>>>>   (if (map? m)
>>>> (reduce-kv #(assoc %1 %2 (apply deep-update-vals %3 f args)) {} m)
>>>> (apply f m args)))
>>>>
>>>>
>>>> On Fri, Jul 26, 2013 at 1:31 PM, Yoshinori Kohyama 
>>>> wrote:
>>>>
>>>>> Thank you Gary.
>>>>> There's no reason why this need to be a macro.
>>>>> It has rewritten as a function.
>>>>>
>>>>> And I'd still like to hear any response about the same three questions.
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> (require '[clojure.test :refer (with-test is run-tests)])
>>>>>
>>>>>
>>>>>
>>>>>  
>>>>> (with-test
>>>>>   (defn mapf [f m & args]
>>>>>
>>>>>
>>>>>
>>>>> ((fn g [n]
>>>>>(if (map? n)
>>>>>
>>>>>
>>>>>
>>>>>(into {} (map (fn [[k v]] [k (g v)]) n))
>>>>>
>>>>>
>>>>>
>>>>>(apply f n args)))
>>>>>
>>>>>  m))
>>>>>  
>>>>>   (is (= (mapf #(* % %) {:a {:b 3 :c 4} :d 5})
>>>>>
>>>>>
>>>>>
>>>>>  {:a {:b 9 :c 16} :d 25}))
>>>>>
>>>>>
>>>>>
>>>>>   (is (= (mapf #(+ (* %1 %1) %2) {:a {:b 3 :c 4} :d 5} 1)
>>>>>
>>>>>
>>>>>
>>>>>  {:a {:b 10 :c 17} :d 26})))
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>  -- 
>>>>> -- 
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "Clojure" group.
>>>>> To post to this group, send email to clo...@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+u...@**googlegroups.com
>>>>>
>>>>> For more options, visit this group at
>>>>> http://groups.google.com/**group/clojure?hl=en<http://groups.google.com/group/clojure?hl=en>
>>>>> --- 
>>>>> You received this message because you are subscribed to the Google 
>>>>> Groups "Clojure" gr

Re: Does this abstraction have any existing name?

2013-07-26 Thread Yoshinori Kohyama
Thank you, Dave.

Yes, I'm thinking of a nested map as a sort of tree.
Then, what I do may be called 'map-leaves', as you say.

Is there an existing function named like that?
Or did you say that just as 'an abstraction name'?

Y.Kohyama

2013年7月27日土曜日 10時02分17秒 UTC+9 Dave Sann:
>
> if you are thinking of the nested map as a sort of tree - which you seem 
> to be doing - then map-leaves or something similar might convey the intent.
>

-- 
-- 
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: Does this abstraction have any existing name?

2013-07-26 Thread Yoshinori Kohyama
Thanks, Jay.
Your reply helps me much.

I often think which is better,
  (into {} (map (fn [[k v]] [k (f v)]) m))
and
  (reduce (fn [a [k v]] (assoc a k (f v))) m m)
or
  (reduce-kv (fn [a k v] (assoc a k (f v))) m m)
, where f is a function, m is a map.

Any body have any opinion?

Thanks.
Y. Kohyama

-- 
-- 
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: Does this abstraction have any existing name?

2013-07-26 Thread Yoshinori Kohyama


Thank you Gary.
There's no reason why this need to be a macro.
It has rewritten as a function.

And I'd still like to hear any response about the same three questions.

(require '[clojure.test :refer (with-test is run-tests)])
 
(with-test
  (defn mapf [f m & args]
((fn g [n]
   (if (map? n)
   (into {} (map (fn [[k v]] [k (g v)]) n))
   (apply f n args)))
 m))
 
  (is (= (mapf #(* % %) {:a {:b 3 :c 4} :d 5})
 {:a {:b 9 :c 16} :d 25}))
  (is (= (mapf #(+ (* %1 %1) %2) {:a {:b 3 :c 4} :d 5} 1)
 {:a {:b 10 :c 17} :d 26})))

-- 
-- 
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.




Does this abstraction have any existing name?

2013-07-26 Thread Yoshinori Kohyama


Hello group.

I wrote a tiny macro to apply a function to values in lowest levels in a map 
with arguments.
Three questions:

   - Does this abstraction have any existing name?
   - Is there any existing function/macro to do similar things?
   - Would anyone improve this implementation?


(require '[clojure.test :refer (with-test is are run-tests)])
 
(with-test
  (defmacro mapf [f m & args]
`((fn f# [n#]
(if (map? n#)
(into {} (map (fn [[k# v#]] [k# (f# v#)]) n#))
(~f n# ~@args)))
  ~m))
 
  (is (= (mapf #(* % %) {:a {:b 3 :c 4} :d 5})
 {:a {:b 9 :c 16} :d 25}))
  (is (= (mapf #(+ (* %1 %1) %2) {:a {:b 3 :c 4} :d 5} 1)
 {:a {:b 10 :c 17} :d 26})))

https://gist.github.com/kohyama/6089899

Thank you in advance.

Y.Kohyama

-- 
-- 
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: group-by replacement when an item has many groups?

2013-07-08 Thread Yoshinori Kohyama
Hi Colin,

One more solution, with example data in the process commented

*(let [f #(range 1 (inc %))*
*  coll '(1 2 3)]*
*  (->>*
**(for [*x coll*; x = 1, 2, 3
*  y *(*f x*)]*  *; y = 1   (where x = 1),
**; 1, 2(where x = 2),
**; 1, 2, 3 (where x = 3)
*  *[*y x*])**; [y, x] = [1, 1], [1, 2], [2, 2], [1, 3], [2, 
3], [3, 3]
**(reduce   *  *; for example where
*  *(fn [*a *[*y x*]]* *;   a = {1 [1 2 3], 2 [2]}, [y x] = [2 3]
**; => (a y []) = [2]
**;(conj ... x) = [2 3]
*(assoc a y (conj (a y []) x)))*
*  *{})))* *;(assoc a y ...) = {1 [1 2 3], 2 [2 3]}

Hope this helps.

Y.Kohyama

-- 
-- 
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: Sequential conditional transforms of an argument

2013-07-07 Thread Yoshinori Kohyama
Hi Laurent,

How about a macro like below?

  (defmacro tt
([x] x)
([x ts tr & more] `(tt (if (~ts ~x) (~tr ~x) ~x) ~@more)))

To use this,

  (tt x test1 transform1 test2 transform2 test3 transform3)

This doesn't work even number of arguments as you see.

HTH,
Y.Kohyama

-- 
-- 
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: Interleaving

2013-06-27 Thread Yoshinori Kohyama
One more solution.

user=> (mapcat (fn [[x y] z] [x y z]) (partition 2 '(:x1 :y1 :x2 :y2 :x3 
:y3)) '(:z1 :z2 :z3))
(:x1 :y1 :z1 :x2 :y2 :z2 :x3 :y3 :z3)

Y. Kohyama

-- 
-- 
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: alter a map in a vector (reference)

2013-06-11 Thread Yoshinori Kohyama
Solutions seem to depend on whether you have two or more mail addresses in 
a ':emails' section or not.
If you have, you should identify the mail address of which you want to 
increment the value.
Anyway, try below:

(dosync
  (alter result
(fn [v]
  (mapv second
(update-in (into {} (map #(vector (:id %) %) v))
  [1 :emails "a...@mail.com"]
  inc)

HTH,
with regards,
Yoshinori Kohyama

-- 
-- 
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: How things are going about expansion, class creation and loading of procedures

2012-12-11 Thread Yoshinori Kohyama
Mr. @athos0220 gave me a great help via twitter.

https://twitter.com/athos0220/status/278439357126418432 (in Japanese)
http://ideone.com/8mBm5N
https://gist.github.com/4255602

Now I can write macros without class files.

Thank you.

With regards,

Yoshinori Kohyama

-- 
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

Re: Proposal/request: Give clojure.core/conj a unary implementation

2012-12-10 Thread Yoshinori Kohyama
+1

2012年11月4日日曜日 7時27分24秒 UTC+9 CGAT:
>
> It would be nice if clojure.core/conj had a unary implementation
> 
>([coll] coll)
>
> The motivating use case is when one is conjoining sequences of
> items to a collection all at once:
>
>(apply conj coll seqable)
>
> such as   (apply conj  #{1 2 3}  [2 4 6 8 10]). 
> Currently (1.4.0), this will raise an arity exception  
> if the seqable object is empty:
>
>(apply conj #{1 2 3} [])
>
> necessitating an awkward empty? check when,
> for instance, the sequence is computed or passed in.
>
> It seems to me that making unary conj the identity is both
> a natural interpretation and essentially cost free, while
> making this use case much more convenient.
> Moreover, it is consistent with clojure.core/disj for sets
> which does act like this
>
>   (apply disj #{1 2 3} []) ->  #{1 2 3}
>
> and has an identity unary implementation.
>
> Comments?
>
>
>

-- 
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

Re: How to add an URL into the classpath?

2012-12-10 Thread Yoshinori Kohyama
Hi Vladimir and Alex,

Thank you for very useful informations.

With regards,

Yoshinori Kohyama

-- 
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

How things are going about expansion, class creation and loading of procedures

2012-12-10 Thread Yoshinori Kohyama
Hello all.

I thank developers of and on clojure always for giving me great programming 
experience.

Now I want to reduce overhead of class-loading because I run applications 
in embedded computers with slow storage.

How can I check

* when each procedure is expanded,
* if a class file is created for each procedure,
* which version of procedure is used and
* when an inline version of procedure is used, if the class file is loaded?

https://gist.github.com/4255602

Please teach me any information around these things.

Best regards,

Yoshinori Kohyama

-- 
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

Re:

2012-07-31 Thread Yoshinori Kohyama
Hi Nicolas,

Thank you for teaching!
I often use very long sequences and often want to terminate in middle of 
folding operations of them.
With reduce, code come to be simple and abstract, but I couldn't terminate 
them.
So I used to use loops or wrapping functions using loop.

Throwing a throwable object is a good alternative for me.

Thank you again.

Regards,
Yoshinori Kohyama

-- 
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

Re:

2012-07-30 Thread Yoshinori Kohyama
Hi Nicolas,

The technique, using throw an Exception when succeeded in searching, 
strikes me!
Not idiomatic but very practical.
It's like a break in a loop of imperatives.
I may use it somewhere.
Thank you.

Regards,
Yoshinori Kohyama

-- 
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

Re: How to add an URL into the classpath?

2012-07-29 Thread Yoshinori Kohyama
I forgot to show foo/core.clj

$ cat samples/src/foo/core.clj 
(ns foo.core)

(defn -main [& args]
  (loop []
(println "Foo: " (apply str (interpose " " args)))
(Thread/sleep 1000)
(recur)))

-- 
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

Re: How to add an URL into the classpath?

2012-07-29 Thread Yoshinori Kohyama
Hello clojurians,

It seems not to be able to control the whole classpath of my runtime.
But my aim, compiling with a path given at runtime and execute it, has been 
achieved,
with
  (.setContextClassLoader (Thread/currentThread)
(DynamicClassLoader. (.getContextClassLoader (Thread/currentThread)))

Details reported below.

Regards,
Yoshinori Kohyama



$ cat project.clj 
(defproject dce "0.0.1"
  :description "Dynamic Compling And Execution"
  :dependencies [[org.clojure/clojure "1.3.0"]]
  :main dce.core)

$ cat src/dce/core.clj 
(ns dce.core
  (:import java.net.URL clojure.lang.DynamicClassLoader)
  (:gen-class))

(defn -main [abs-path target-ns & args]
  (let [ccl (.getContextClassLoader (Thread/currentThread))
dcl (if (instance? DynamicClassLoader ccl) ccl
(let [l (DynamicClassLoader. ccl)]
  (.setContextClassLoader (Thread/currentThread) l)
  l))]
(.addURL dcl (URL. (str "file://" abs-path "/src/")))
(.addURL dcl (URL. (str "file://" abs-path "/classes/")))
(binding [*compile-path* (str abs-path "/classes")]
  (compile (symbol target-ns)))
(def f (future (apply (resolve (symbol (str target-ns "/-main"))) 
args)))
(Thread/sleep 5000)
(future-cancel f)))

$ tree samples
samples
├── classes
└── src
└── foo
└── core.clj

3 directories, 1 file

$ lein repl
REPL started; server listening on localhost port 5997
dce.core=> (-main (.getCanonicalPath (java.io.File. "samples")) "foo.core" 
"arg1" "arg2")
Foo:  arg1 arg2
Foo:  arg1 arg2
Foo:  arg1 arg2
Foo:  arg1 arg2
Foo:  arg1 arg2
Foo:  arg1 arg2
true
dce.core=> ^D

$ tree samplessamples
├── classes
│   └── foo
│   ├── core$_main.class
│   ├── core$loading__4505__auto__.class
│   └── core__init.class
└── src
└── foo
└── core.clj

4 directories, 4 files

$ lein uberjar
...
Created ... dce/dce-0.0.1-standalone.jar

$ rm -Rf samples/classes/*
$ tree samples
samples
├── classes
└── src
└── foo
└── core.clj

3 directories, 1 file

$ java -jar dce-0.0.1-standalone.jar `pwd`/samples foo.core arg1 arg2
Foo:  arg1 arg2
Foo:  arg1 arg2
Foo:  arg1 arg2
Foo:  arg1 arg2
Foo:  arg1 arg2
^C

$ tree samples
samples
├── classes
│   └── foo
│   ├── core$_main.class
│   ├── core$loading__4505__auto__.class
│   └── core__init.class
└── src
└── foo
└── core.clj

4 directories, 4 files

-- 
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

Re: How to add an URL into the classpath?

2012-07-29 Thread Yoshinori Kohyama
Hi Sierra,

Thank you for your kind and quick answers to my questions.
I see.
I'll read the page you referred.

Regards,
Yoshinori Kohyama

-- 
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

Re:

2012-07-29 Thread Yoshinori Kohyama
Hi John,

'partition' will be useful for you, as Moritz pointed out.

(partition 2 1 [1 2 3 4]) -> ((1 2) (2 3) (3 4))
(partition 2 1 [1 2 2 4]) -> ((1 2) (2 2) (2 4))
(partition 2 1 [1 2 2 2]) -> ((1 2) (2 2) (2 2))

(some #(= % [2 2]) (partition 2 1 [1 2 3 4])) -> nil
(some #(= % [2 2]) (partition 2 1 [1 2 2 4])) -> true
(some #(= % [2 2]) (partition 2 1 [1 2 2 2])) -> true

(filter #(= % [2 2]) (partition 2 1 [1 2 3 4])) -> ()
(filter #(= % [2 2]) (partition 2 1 [1 2 2 4])) -> ((2 2))
(filter #(= % [2 2]) (partition 2 1 [1 2 2 2])) -> ((2 2) (2 2))

I'm sorry I can't recognize whether you need a pair of 2s or two pairs of 
2s.

If you need one or more pairs of 2s, do
(defn has22 [coll] (boolean (some #(= % [2 2]) (partition 2 1 coll
(has22 [1 2 3 4]) -> false
(has22 [1 2 2 4]) -> true
(has22 [1 2 2 2]) -> true

If you need two or more pairs of 2s, do
(defn has222 [coll] (< 1 (count (filter #(= % [2 2]) (partition 2 1 
coll)
(has222 [1 2 3 4]) -> false
(has222 [1 2 2 4]) -> false
(has222 [1 2 2 2]) -> true

Regards,
Yoshinori Kohyama

-- 
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

Re: How to add an URL into the classpath?

2012-07-29 Thread Yoshinori Kohyama
Hello programmers,

Hi, Sierra.  Thank you replying.

O.K. I can not always contol the classloader of the whole JVM.

Then, how can I get the dynamic classloader for my current code/thread?
Or can't I?

Regards,
Yoshinori Kohyama

-- 
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

How to add an URL into the classpath?

2012-07-27 Thread Yoshinori Kohyama
Hello clojurians,

Adding an URL to the classpath dynamically in REPL succeeded.

  $ java -cp clojure-1.3.0.jar clojure.main
  Clojure 1.3.0
  user=> (def ccl (.getContextClassLoader (Thread/currentThread)))
  #'user/ccl
  user=> (class ccl)
  clojure.lang.DynamicClassLoader
  user=> (.addURL ccl (java.net.URL. "file:///some/path/"))
  nil

But the program below fails.

  $ cat addcp.clj 
  (def ccl (.getContextClassLoader (Thread/currentThread)))
  (println (class ccl))
  (.addURL ccl (java.net.URL. "file:///some/path/"))
  $ java -cp clojure-1.3.0.jar clojure.main addcp.clj 
  sun.misc.Launcher$AppClassLoader
  Exception in thread "main" java.lang.IllegalArgumentException: No 
matching method found: addURL for class sun.misc.Launcher$AppClassLoader
at clojure.lang.Reflector.invokeMatchingMethod(Reflector.java:52)
at clojure.lang.Reflector.invokeInstanceMethod(Reflector.java:30)
at user$eval3.invoke(addcp.clj:3)
at clojure.lang.Compiler.eval(Compiler.java:6465)
at clojure.lang.Compiler.load(Compiler.java:6902)
at clojure.lang.Compiler.loadFile(Compiler.java:6863)
at clojure.main$load_script.invoke(main.clj:282)
at clojure.main$script_opt.invoke(main.clj:342)
at clojure.main$main.doInvoke(main.clj:426)
at clojure.lang.RestFn.invoke(RestFn.java:408)
at clojure.lang.Var.invoke(Var.java:401)
at clojure.lang.AFn.applyToHelper(AFn.java:161)
at clojure.lang.Var.applyTo(Var.java:518)
at clojure.main.main(main.java:37)

While in REPL the loader gotten with .getContextClassLoader is an instance 
of clojure.lang.DynamicClassLoader,
the latter is an sun.misc.Launcher$AppClassLoader.

How can I get an clojure.lang.DynamicClassLoader?
Or am I wrong with something?

Please teach me any information about this.

Regards,
Yoshinori Kohyama

-- 
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

Re: so why it has to be so complicated / Longest Increasing Sub-Seq

2012-06-13 Thread Yoshinori Kohyama
Hi Andy and all,

Here's mine with trying to separate steps.
You can see each steps with commenting out later steps.

  (fn [coll]
(->> coll
  ; 1st. Make tails of coll.
  (#(take-while (comp not nil?) (iterate next %)))
  ; 2nd. Take only consecutive elements from the head for each list.
  (map (fn [[c & cs]]
 (loop [[x & xs] cs acc [c] z c]
   (if (= (inc z) x) (recur xs (conj acc x) x) acc
  ; 3rd. Take only vectors having 2 or more elements.
  (filter #(< 1 (count %)))
  ; 4th. Take the longest
  (reduce #(if (< (count %2) (count %)) % %2) [])
))

https://gist.github.com/2928234

Regards,
Yoshinori Kohyama

-- 
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

Re: How about 'nth' accepts maps?

2012-06-13 Thread Yoshinori Kohyama
Hi forum,

I found that (empty? my-brain) -> true.
'seq' suffices.

  user=>  (def sm (sorted-map 0 {:name "Alice"} 1 {:name "Bob"} 2 {:name 
"Charlie"}))
  #'user/sm
  user=> (loop [[k v :as x] & xs] (seq sm) acc []]
(if x
(recur xs (conj acc (assoc v :id k)))
acc))
  [{:name "Alice", :id 0} {:name "Bob", :id 1} {:name "Charlie", :id 2}]

I'm sorry for all the fuss.

By the by, to get the same result as the example above, what to do is
  (apply vector (map (fn [[k v]] (assoc v :id k)) sm))

Regards,
Yoshinori Kohyama

-- 
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

Re: How about 'nth' accepts maps?

2012-06-12 Thread Yoshinori Kohyama
Hi all,

Thank you for commenting, Chris and Meikel.

In my Clojure 1.3.0 REPL,
  (def sm (sorted-map 0 {:name "Alice"} 1 {:name "Bob"} 2 {:name 
"Charlie"}))
  (loop [c sm acc []]
(if-let [[k v] (first c)]
  (recur (next c) (conj acc (assoc v :id k)))
  acc))
returns
  [{:name "Alice", :id 0} {:name "Bob", :id 1} {:name "Charlie", :id 2}]
. And
  (nthnext sm 1)
returns
  ([1 {:name "Bob"}] [2 {:name "Charlie"}])
.
'first', 'next' and 'nthnext' look to accept a map for me.
Do I misunderstand something?

I only want to write an example above like:
  (loop [[k v :as x] & xs] sm acc []]
(if c (recur xs (conj acc (assoc v :id k))) acc))
.
And it can be done only if 'nth' accepts a map, by implementing 'nth' using 
'seq', 'first', 'next', 'loop' and 'recur' like 'nthnext' or 'my-nth' in 
the first post.
https://github.com/clojure/clojure/blob/d0c380d9809fd242bec688c7134e900f0bbedcac/src/clj/clojure/core.clj#L2747

Note that the example above is done without a loop, I know, and  I forced 
to use a loop for a demonstrative purpose.

Regards,
Yoshinori Kohyama

-- 
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

How about 'nth' accepts maps?

2012-06-12 Thread Yoshinori Kohyama
Hello forum,

Given

(def m (sorted-map 1 :a 2 :b 3 :c 4 :d 5 :e))

,

(nth m 0)

throws 'UnsupportedOperationException nth', while

(first m) ; -> [1 :a]
(next m)  ; -> ([2 :b] [3 :c] [4 :d] [5 :e])
(nthnext m 1) ; -> ([2 :b] [3 :c] [4 :d] [5 :e])
.

How do you think about nth accepts maps

(defn my-nth [coll index]
  (loop [n index xs (seq coll)]
(if-let [x (first xs)]

  (if (zero? n) x (recur (dec n) (next xs))

(my-nth m 0) ; -> [1 :a]

like the implementation of nthnext?

Of cource, this should be used for collections only which don't supported 
nth directly, I think.

A form

(let [[a b c & d :as e] [1 2 3 4 5]]
  [a b c d e]) 

returns

[1 2 3 (4 5) [1 2 3 4 5]]
, but

(let [[a b c & d :as e] (sorted-map 1 :a 2 :b 3 :c 4 :d 5 :e)]
  [a b c d e])
throws 'UnsupportedOperationException nth'.

What do you think if it returned

  [[1 :a] [2 :b] [3 :c] {4 :d 5 :e} {1 :a 2 :b 3 :c 4 :d 5 :e}]

.


Or isn't it good that you can write operations for a map like

(loop [[[k v :as x] & xs] m acc '()]
  (if x
  (recur xs (cons acc (some-operation-for k v)))
  acc))

?


Please let me hear opinions.


Regards,

Yoshinori Kohyama

-- 
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

Re: Why isn't there a fold-right?

2012-06-11 Thread Yoshinori Kohyama
Hi all,

I'm sorry such my frequent posts.

How stupid have I been!
The function 'f' doesn't vary in the chain of operations.
We only have to keep 1st operands of 'f' in reversed order in a list.
What I have to do is applying 'f' in the reversing order when stopping 
condition comes,
instead of reversing operands at the beggining of calculations and apply 
'f's to them.

I've updated,
1) The initial topic *fold-right* and its family *reduce-right:* 
https://gist.github.com/2893987
2) An abstraction for folding-from-right-loops *r-reduce:* 
https://gist.github.com/2896939
3) Related function *unfold*: https://gist.github.com/2901487

They no longer need a large heap.

Regards,
Yoshinori Kohyama

-- 
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

Re: Why isn't there a fold-right?

2012-06-11 Thread Yoshinori Kohyama
Hi all,

To fold operations from right to left,
accumulate what calculations should be done in a list with *cons* in order 
(Thanks Tassilo) and
*comp*ose accumulated functions manually (Thanks Meikel, almost equivalent 
to using
Tassilo's *comp** I think) without reverse.

First, the initial topic *fold-right* and its family *reduce-right:*
 https://gist.github.com/2893987
Next, my abstraction for folding-from-right-loops *r-reduce:*
 https://gist.github.com/2896939
Last, related function *unfold*: https://gist.github.com/2901487

Have changed not to need large stack.
Have become very faster.
Still need little bit of heap.

Regards,
Yoshinori Kohyama

-- 
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

Re: Why isn't there a fold-right?

2012-06-11 Thread Yoshinori Kohyama
Hi Tassilo and all.

I'm testing some code.
It seems that the clojure runtime remembers some results of calculation.
For an exact comparison, please do
1) Exec java clojure.main
2) Exec one version of a function
3) Quit java clojure.main
4) Exec java clojure.main
5) Exec another version of the function
instead of sequencial tests of multiple versions of a function in one 
runtime.
Or do sequencial tests of multiple versions of a function in the reverse 
order.

Regards,
Yoshinori Kohyama

-- 
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

Re: Why isn't there a fold-right?

2012-06-11 Thread Yoshinori Kohyama
Hi Meikel,

Thank you for pointing it out that 'comp' calls 'reverse' internally.
I've been calling 'reverse' twice.
Appreciate you so much.

I'll try 'comp'ing the accumulated functions manually without 'reverse'.

Regards,
Yoshinori Kohyama

-- 
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

Re: Why isn't there a fold-right?

2012-06-11 Thread Yoshinori Kohyama
Hello Tassilo and all again,

Thank you for your kind words to me and useful consideration.

However, you have to reverse the list 
> of functions, which destroys the theoretical benefit. 
>

Exactly! 
 

> But still my reverse-the-collection-and-then-reduce-it approach is still 
> much faster, although it has to iterate the collection twice. 

 
It seems to make things faster using a '(transient [])'.
Thank you.
 

> I'm not exactly sure why, but I'd guess the allocation overhead for 2 
> million partial functions is rather expensive. 
>

Allocation of partial functions is expensive as you say.
May it be that 'conj'ing an element at the last of a collection still takes 
much cost than 'cons'ing
an element at the head of it and reversing?

I'll keep thinking and trying.

Regards,
Yoshinori Kohyama

 

-- 
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

Re: Why isn't there a fold-right?

2012-06-10 Thread Yoshinori Kohyama
Hi all.

Sorry for mere my continued report.
Instead of reversing operands, I tried accumulating operators and reversing 
and applying it when stopping.
https://gist.github.com/2896939
It doesn't consume stack. (but heap...)

Regards,
Yoshinori Kohyama

-- 
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

Re: Why isn't there a fold-right?

2012-06-10 Thread Yoshinori Kohyama
Hi O. Masanori and all,

Thank you so much for your kindly explanation.

Sure that 'fold-right' itself is strict as you say.

But I think that various right-folded-type loops, which can be stopped 
middle of a list, can be lazy.
The continuation, which should be calculated in the later steps, should be 
determined when the list is searched to that point from head of it.

https://gist.github.com/2894687
https://gist.github.com/2896939

Does anyone have an idea not to eat large stack with an abstraction for 
these loops?

-- 
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

Re: Why isn't there a fold-right?

2012-06-10 Thread Yoshinori Kohyama
Hello Tassilo and All,

Thank you for your comment.
Yes, it is sorry that my 'fold-right' eat large stack.
Using 'reverse' is good.

But I think 'reverse' is a special case of 'fold-right'.
(fold-right #(concat %2 (list %)) '() '(:a :b :c :d))
; -> (:d :c :b :a)
If clojure's implementation of reverse don't consume stack, how does it do?

https://github.com/clojure/clojure/blob/d0c380d9809fd242bec688c7134e900f0bbedcac/src/clj/clojure/core.clj#L883
The source of 'reverse' says it uses 'reduce1' and commented "Not lazy".

https://github.com/clojure/clojure/blob/d0c380d9809fd242bec688c7134e900f0bbedcac/src/clj/clojure/core.clj#L865
The source of 'reduce1' says it uses 'chunk-seq?', '.reduce', 'chunk-first' 
and 'chunk-next'.
What are these?

And is there any lazy solution?


Regards,
Yoshinori Kohyama

-- 
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

Re: Why isn't there a fold-right?

2012-06-08 Thread Yoshinori Kohyama
Hello caffe and Yang Dong.

Here is my fold-right.

(defn fold-right [f s coll]
  ((reduce (fn [acc x] #(acc (f x %))) identity coll) s))
  
; (fold-right - 1 '(3 1 4))
; -> ((fn [y3]
;   ((fn [y2]
; ((fn [y1] (identity (- 3 y1)))
;(- 1 y2)))
;  (- 4 y3)))
;   1)
; -> (- 3 (- 1 (- 4 1)))
; -> 5

; (fold-right #(concat %2 (list %)) '() '(:a :b :c :d))
; -> (:d :c :b :a)

https://gist.github.com/2893987
https://algobit.atlassian.net/wiki/display/PL/Comparison

2010年11月6日土曜日 11時03分20秒 UTC+9 Yang Dong:
>
> Maybe because Clojure has a vector, and conj conjoins new elements to 
> the end of the vector, so there's mere little use of fold-right. But, 
> fold-right is an abstraction tool, missing it in the core is kind of 
> pity. 
>

-- 
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