String manipulation in Clojure
I am new to Clojure. I have a java hashmap which has integer as keys and array list as values. The map is: {1=[Delhi, Goa, Mumbai], 2=[hello, world, the, world, is, awesome]}. I want to replace world with night and hello with good. I am doing something like this.
Clojure code:
(doseq [[k v] m]
(when (= k 2)
(doseq [s v]
(if (= s "world")
(def a(clojure.string/replace(s #"world" "night"))))
(if (= s "hello")
(def b(clojure.string/replace(s #"hello" "good")))))))
This doesn't work perfectly. Also when I try to print the value of a and b, it shows
#object[clojure.lang.Var$Unbound 0x31791dc5 Unbound: #'com.example.core/a] and #object[clojure.lang.Var$Unbound 0x31791dc5 Unbound: #'com.example.core/b].
How can I see the value of a i.e. night and b i.e. good. Is there any other way of string manipulation? Any help will be really appreciated.
java clojure
add a comment |
I am new to Clojure. I have a java hashmap which has integer as keys and array list as values. The map is: {1=[Delhi, Goa, Mumbai], 2=[hello, world, the, world, is, awesome]}. I want to replace world with night and hello with good. I am doing something like this.
Clojure code:
(doseq [[k v] m]
(when (= k 2)
(doseq [s v]
(if (= s "world")
(def a(clojure.string/replace(s #"world" "night"))))
(if (= s "hello")
(def b(clojure.string/replace(s #"hello" "good")))))))
This doesn't work perfectly. Also when I try to print the value of a and b, it shows
#object[clojure.lang.Var$Unbound 0x31791dc5 Unbound: #'com.example.core/a] and #object[clojure.lang.Var$Unbound 0x31791dc5 Unbound: #'com.example.core/b].
How can I see the value of a i.e. night and b i.e. good. Is there any other way of string manipulation? Any help will be really appreciated.
java clojure
6
You should start by reading a book like Clojure for the Brave and True, or another online resource. You're trying to write Clojure like it's Java, and that is going to make your life painful.
– Carcigenicate
Dec 31 '18 at 14:40
1
Only def in toplevel. Use let instead.
– cfrick
Dec 31 '18 at 16:15
add a comment |
I am new to Clojure. I have a java hashmap which has integer as keys and array list as values. The map is: {1=[Delhi, Goa, Mumbai], 2=[hello, world, the, world, is, awesome]}. I want to replace world with night and hello with good. I am doing something like this.
Clojure code:
(doseq [[k v] m]
(when (= k 2)
(doseq [s v]
(if (= s "world")
(def a(clojure.string/replace(s #"world" "night"))))
(if (= s "hello")
(def b(clojure.string/replace(s #"hello" "good")))))))
This doesn't work perfectly. Also when I try to print the value of a and b, it shows
#object[clojure.lang.Var$Unbound 0x31791dc5 Unbound: #'com.example.core/a] and #object[clojure.lang.Var$Unbound 0x31791dc5 Unbound: #'com.example.core/b].
How can I see the value of a i.e. night and b i.e. good. Is there any other way of string manipulation? Any help will be really appreciated.
java clojure
I am new to Clojure. I have a java hashmap which has integer as keys and array list as values. The map is: {1=[Delhi, Goa, Mumbai], 2=[hello, world, the, world, is, awesome]}. I want to replace world with night and hello with good. I am doing something like this.
Clojure code:
(doseq [[k v] m]
(when (= k 2)
(doseq [s v]
(if (= s "world")
(def a(clojure.string/replace(s #"world" "night"))))
(if (= s "hello")
(def b(clojure.string/replace(s #"hello" "good")))))))
This doesn't work perfectly. Also when I try to print the value of a and b, it shows
#object[clojure.lang.Var$Unbound 0x31791dc5 Unbound: #'com.example.core/a] and #object[clojure.lang.Var$Unbound 0x31791dc5 Unbound: #'com.example.core/b].
How can I see the value of a i.e. night and b i.e. good. Is there any other way of string manipulation? Any help will be really appreciated.
java clojure
java clojure
edited Dec 31 '18 at 16:26
cfrick
18.3k23552
18.3k23552
asked Dec 31 '18 at 14:37
ditriditri
63
63
6
You should start by reading a book like Clojure for the Brave and True, or another online resource. You're trying to write Clojure like it's Java, and that is going to make your life painful.
– Carcigenicate
Dec 31 '18 at 14:40
1
Only def in toplevel. Use let instead.
– cfrick
Dec 31 '18 at 16:15
add a comment |
6
You should start by reading a book like Clojure for the Brave and True, or another online resource. You're trying to write Clojure like it's Java, and that is going to make your life painful.
– Carcigenicate
Dec 31 '18 at 14:40
1
Only def in toplevel. Use let instead.
– cfrick
Dec 31 '18 at 16:15
6
6
You should start by reading a book like Clojure for the Brave and True, or another online resource. You're trying to write Clojure like it's Java, and that is going to make your life painful.
– Carcigenicate
Dec 31 '18 at 14:40
You should start by reading a book like Clojure for the Brave and True, or another online resource. You're trying to write Clojure like it's Java, and that is going to make your life painful.
– Carcigenicate
Dec 31 '18 at 14:40
1
1
Only def in toplevel. Use let instead.
– cfrick
Dec 31 '18 at 16:15
Only def in toplevel. Use let instead.
– cfrick
Dec 31 '18 at 16:15
add a comment |
2 Answers
2
active
oldest
votes
combination of update
and replace
would do the trick:
user> (def data {1 ["Delhi" "Goa" "Mumbai"]
2 ["hello" "world" "the" "world" "is" "awesome"]})
user> (update data 2 #(replace {"world" "night" "hello" "good"} %))
;;=> {1 ["Delhi" "Goa" "Mumbai"], 2 ["good" "night" "the" "night" "is" "awesome"]}
add a comment |
There's a couple of different ways to approach this. Let's say you've got
(def data {1 ["hello" "world"]})
The easy way
(def modified (assoc data 1 ["good" "night"]))
;; {1 ["good" "night"]}
This just returns a new data structure with the value at 1 swapped for a new vector of the appropriate elements. But that only works for fairly simple stuff where you know exactly what's in the vector.
The less easy way
(def result
(into (sorted-map)
(map
(fn [[k v]]
(if (some #{"hello" "world"} v)
[k (assoc (assoc v (.indexOf v "hello") "good")
(.indexOf v "world")
"night")]
[k v]))
(seq data))))
(print result) ; {1 ["good" "night"]}
Here, seq
called on the map returns a lazy sequence of key/value tuples. We map over those with a function that checks for the values we want to replace and returns either a new key/value tuple with the words swapped at the appropriate indicies in the vector or the original if "hello"/"world" are not in the vector. Then into
turns it back into a map.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53988621%2fstring-manipulation-in-clojure%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
combination of update
and replace
would do the trick:
user> (def data {1 ["Delhi" "Goa" "Mumbai"]
2 ["hello" "world" "the" "world" "is" "awesome"]})
user> (update data 2 #(replace {"world" "night" "hello" "good"} %))
;;=> {1 ["Delhi" "Goa" "Mumbai"], 2 ["good" "night" "the" "night" "is" "awesome"]}
add a comment |
combination of update
and replace
would do the trick:
user> (def data {1 ["Delhi" "Goa" "Mumbai"]
2 ["hello" "world" "the" "world" "is" "awesome"]})
user> (update data 2 #(replace {"world" "night" "hello" "good"} %))
;;=> {1 ["Delhi" "Goa" "Mumbai"], 2 ["good" "night" "the" "night" "is" "awesome"]}
add a comment |
combination of update
and replace
would do the trick:
user> (def data {1 ["Delhi" "Goa" "Mumbai"]
2 ["hello" "world" "the" "world" "is" "awesome"]})
user> (update data 2 #(replace {"world" "night" "hello" "good"} %))
;;=> {1 ["Delhi" "Goa" "Mumbai"], 2 ["good" "night" "the" "night" "is" "awesome"]}
combination of update
and replace
would do the trick:
user> (def data {1 ["Delhi" "Goa" "Mumbai"]
2 ["hello" "world" "the" "world" "is" "awesome"]})
user> (update data 2 #(replace {"world" "night" "hello" "good"} %))
;;=> {1 ["Delhi" "Goa" "Mumbai"], 2 ["good" "night" "the" "night" "is" "awesome"]}
answered Jan 1 at 16:17
leetwinskileetwinski
10.6k1927
10.6k1927
add a comment |
add a comment |
There's a couple of different ways to approach this. Let's say you've got
(def data {1 ["hello" "world"]})
The easy way
(def modified (assoc data 1 ["good" "night"]))
;; {1 ["good" "night"]}
This just returns a new data structure with the value at 1 swapped for a new vector of the appropriate elements. But that only works for fairly simple stuff where you know exactly what's in the vector.
The less easy way
(def result
(into (sorted-map)
(map
(fn [[k v]]
(if (some #{"hello" "world"} v)
[k (assoc (assoc v (.indexOf v "hello") "good")
(.indexOf v "world")
"night")]
[k v]))
(seq data))))
(print result) ; {1 ["good" "night"]}
Here, seq
called on the map returns a lazy sequence of key/value tuples. We map over those with a function that checks for the values we want to replace and returns either a new key/value tuple with the words swapped at the appropriate indicies in the vector or the original if "hello"/"world" are not in the vector. Then into
turns it back into a map.
add a comment |
There's a couple of different ways to approach this. Let's say you've got
(def data {1 ["hello" "world"]})
The easy way
(def modified (assoc data 1 ["good" "night"]))
;; {1 ["good" "night"]}
This just returns a new data structure with the value at 1 swapped for a new vector of the appropriate elements. But that only works for fairly simple stuff where you know exactly what's in the vector.
The less easy way
(def result
(into (sorted-map)
(map
(fn [[k v]]
(if (some #{"hello" "world"} v)
[k (assoc (assoc v (.indexOf v "hello") "good")
(.indexOf v "world")
"night")]
[k v]))
(seq data))))
(print result) ; {1 ["good" "night"]}
Here, seq
called on the map returns a lazy sequence of key/value tuples. We map over those with a function that checks for the values we want to replace and returns either a new key/value tuple with the words swapped at the appropriate indicies in the vector or the original if "hello"/"world" are not in the vector. Then into
turns it back into a map.
add a comment |
There's a couple of different ways to approach this. Let's say you've got
(def data {1 ["hello" "world"]})
The easy way
(def modified (assoc data 1 ["good" "night"]))
;; {1 ["good" "night"]}
This just returns a new data structure with the value at 1 swapped for a new vector of the appropriate elements. But that only works for fairly simple stuff where you know exactly what's in the vector.
The less easy way
(def result
(into (sorted-map)
(map
(fn [[k v]]
(if (some #{"hello" "world"} v)
[k (assoc (assoc v (.indexOf v "hello") "good")
(.indexOf v "world")
"night")]
[k v]))
(seq data))))
(print result) ; {1 ["good" "night"]}
Here, seq
called on the map returns a lazy sequence of key/value tuples. We map over those with a function that checks for the values we want to replace and returns either a new key/value tuple with the words swapped at the appropriate indicies in the vector or the original if "hello"/"world" are not in the vector. Then into
turns it back into a map.
There's a couple of different ways to approach this. Let's say you've got
(def data {1 ["hello" "world"]})
The easy way
(def modified (assoc data 1 ["good" "night"]))
;; {1 ["good" "night"]}
This just returns a new data structure with the value at 1 swapped for a new vector of the appropriate elements. But that only works for fairly simple stuff where you know exactly what's in the vector.
The less easy way
(def result
(into (sorted-map)
(map
(fn [[k v]]
(if (some #{"hello" "world"} v)
[k (assoc (assoc v (.indexOf v "hello") "good")
(.indexOf v "world")
"night")]
[k v]))
(seq data))))
(print result) ; {1 ["good" "night"]}
Here, seq
called on the map returns a lazy sequence of key/value tuples. We map over those with a function that checks for the values we want to replace and returns either a new key/value tuple with the words swapped at the appropriate indicies in the vector or the original if "hello"/"world" are not in the vector. Then into
turns it back into a map.
edited Jan 1 at 13:36
answered Dec 31 '18 at 14:52
Jared SmithJared Smith
9,59632043
9,59632043
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53988621%2fstring-manipulation-in-clojure%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
6
You should start by reading a book like Clojure for the Brave and True, or another online resource. You're trying to write Clojure like it's Java, and that is going to make your life painful.
– Carcigenicate
Dec 31 '18 at 14:40
1
Only def in toplevel. Use let instead.
– cfrick
Dec 31 '18 at 16:15