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

0 votes
ago 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

0 votes
ago by

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?

...