Welcome! Please see the About page for a little more info on how this works.

0 votes
in Collections by

Hi,

The challenge is to get the server name part of a collection of strings of fully qualified domain names.
From: ["server1.oo.com" "s2.ee.com"]
To: ["server1" "s2"]

I can do this to a single element as:

(subs "server1.oo.com" 0 (str/index-of "server1.oo.com" \.))

or

(subs "server1.oo.com" 0 (#(str/index-of % \.) "server1.oo.com"))

But nested #() functions seem not to be possible.

(#(subs % 0 (#(str/index-of % \.) "server1.oo.com")) "server1.oo.com")

returns an error.

When using the collection, something like this works:

(let 
  [s-col ["server1.oo.com" "s2.ee.com"]]   
  (map #(subs % 0 5) s-col))

But, how can I use the (str/index-of % \.) instead of that hard-coded 5?
All my attempts were unsuccessful.

1 Answer

+1 vote
by
selected by
 
Best answer

You don't need a nested function here:

(let
  [s-col ["server1.oo.com" "s2.ee.com"]]
  (map #(subs % 0 (str/index-of % \.)) s-col))
;;=> ("server1" "s2")

Assuming that's what you're trying to produce?

by
Yes that's it.  My experience in Clojure only has some days to show.  Thank you.
by
I'd also suggest extracting the anonymous function into a top level one:
```
(defn sub-domain [domain]
   (subs domain 0 (str/index-of domain \.)))
```
Then you'd end up with something like:
```
(let
  [domains ["server1.oo.com" "s2.ee.com"]]
  (map sub-domain domains))
;;=> ("server1" "s2")
```
...