Define and redefine a matrix in Clojure











up vote
1
down vote

favorite












I would like to define a matrix in memory using Clojure and then change it when I reach a route. From my understanding, I need to create this matrix globally and then change it when I reach that part of the code.



However, since I am new on Clojure, I am not sure how is the best approach to do it. Follow my code:



(def global-matrix nil)


(defn create-matrix
[{:keys [params] :as request}]
(println global-matrix)
(var-set global-matrix [[0 0 0] [0 0 0]])
(println global-matrix)
(ring-resp/response {:matrix global-matrix})))


I am getting a casting problem with this approach so not sure how is the best Clojure solution for this (maybe I am thinking in a not functional programming way).










share|improve this question




















  • 3




    Why are you trying to change global-matrix? Either it should be immutable and you locally work with modified copies of it, or if you really need mutation, you could make it an atom and reset! it. Clojure is highly functional leaning though, and most of the time, immutable structures are the most appropriate. I've never used Ring, so I can't make any suggestions regarding it unfortunately.
    – Carcigenicate
    Nov 7 at 20:47












  • I need to create a REST API to change this matrix everytime it is called. There is no way to persist this matrix in memory without defining as a variable right? I got it is immutable but how could I change it then? @Carcigenicate
    – andrehigher
    Nov 7 at 21:03






  • 1




    Make global-matrix an atom holding an immutable structure (like a vector of vectors), and swap! or reset! the atom to change it. atoms are mutable containers for immutable values.
    – Carcigenicate
    Nov 7 at 21:05








  • 1




    An atom is an effective way to do this. You would just do (def global-matrix (atom nil)) and then in your function, do (ring-resp/response {:matrix (reset! global-matrix [[0 0 0] [0 0 0]]}). This is one way to do it but not necessarily the best way. Instead of binding the matrix to a var, you could pass it in the request map and then do (ring-resp/response {:matrix (reset! (:matrix request) [[0 0 0][0 0 0]])})
    – Rulle
    Nov 7 at 21:14








  • 1




    Perfect! It is working fine now. Thanks guys!
    – andrehigher
    Nov 8 at 0:08















up vote
1
down vote

favorite












I would like to define a matrix in memory using Clojure and then change it when I reach a route. From my understanding, I need to create this matrix globally and then change it when I reach that part of the code.



However, since I am new on Clojure, I am not sure how is the best approach to do it. Follow my code:



(def global-matrix nil)


(defn create-matrix
[{:keys [params] :as request}]
(println global-matrix)
(var-set global-matrix [[0 0 0] [0 0 0]])
(println global-matrix)
(ring-resp/response {:matrix global-matrix})))


I am getting a casting problem with this approach so not sure how is the best Clojure solution for this (maybe I am thinking in a not functional programming way).










share|improve this question




















  • 3




    Why are you trying to change global-matrix? Either it should be immutable and you locally work with modified copies of it, or if you really need mutation, you could make it an atom and reset! it. Clojure is highly functional leaning though, and most of the time, immutable structures are the most appropriate. I've never used Ring, so I can't make any suggestions regarding it unfortunately.
    – Carcigenicate
    Nov 7 at 20:47












  • I need to create a REST API to change this matrix everytime it is called. There is no way to persist this matrix in memory without defining as a variable right? I got it is immutable but how could I change it then? @Carcigenicate
    – andrehigher
    Nov 7 at 21:03






  • 1




    Make global-matrix an atom holding an immutable structure (like a vector of vectors), and swap! or reset! the atom to change it. atoms are mutable containers for immutable values.
    – Carcigenicate
    Nov 7 at 21:05








  • 1




    An atom is an effective way to do this. You would just do (def global-matrix (atom nil)) and then in your function, do (ring-resp/response {:matrix (reset! global-matrix [[0 0 0] [0 0 0]]}). This is one way to do it but not necessarily the best way. Instead of binding the matrix to a var, you could pass it in the request map and then do (ring-resp/response {:matrix (reset! (:matrix request) [[0 0 0][0 0 0]])})
    – Rulle
    Nov 7 at 21:14








  • 1




    Perfect! It is working fine now. Thanks guys!
    – andrehigher
    Nov 8 at 0:08













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I would like to define a matrix in memory using Clojure and then change it when I reach a route. From my understanding, I need to create this matrix globally and then change it when I reach that part of the code.



However, since I am new on Clojure, I am not sure how is the best approach to do it. Follow my code:



(def global-matrix nil)


(defn create-matrix
[{:keys [params] :as request}]
(println global-matrix)
(var-set global-matrix [[0 0 0] [0 0 0]])
(println global-matrix)
(ring-resp/response {:matrix global-matrix})))


I am getting a casting problem with this approach so not sure how is the best Clojure solution for this (maybe I am thinking in a not functional programming way).










share|improve this question















I would like to define a matrix in memory using Clojure and then change it when I reach a route. From my understanding, I need to create this matrix globally and then change it when I reach that part of the code.



However, since I am new on Clojure, I am not sure how is the best approach to do it. Follow my code:



(def global-matrix nil)


(defn create-matrix
[{:keys [params] :as request}]
(println global-matrix)
(var-set global-matrix [[0 0 0] [0 0 0]])
(println global-matrix)
(ring-resp/response {:matrix global-matrix})))


I am getting a casting problem with this approach so not sure how is the best Clojure solution for this (maybe I am thinking in a not functional programming way).







matrix clojure






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 20:44









Carcigenicate

16.9k42956




16.9k42956










asked Nov 7 at 20:21









andrehigher

234




234








  • 3




    Why are you trying to change global-matrix? Either it should be immutable and you locally work with modified copies of it, or if you really need mutation, you could make it an atom and reset! it. Clojure is highly functional leaning though, and most of the time, immutable structures are the most appropriate. I've never used Ring, so I can't make any suggestions regarding it unfortunately.
    – Carcigenicate
    Nov 7 at 20:47












  • I need to create a REST API to change this matrix everytime it is called. There is no way to persist this matrix in memory without defining as a variable right? I got it is immutable but how could I change it then? @Carcigenicate
    – andrehigher
    Nov 7 at 21:03






  • 1




    Make global-matrix an atom holding an immutable structure (like a vector of vectors), and swap! or reset! the atom to change it. atoms are mutable containers for immutable values.
    – Carcigenicate
    Nov 7 at 21:05








  • 1




    An atom is an effective way to do this. You would just do (def global-matrix (atom nil)) and then in your function, do (ring-resp/response {:matrix (reset! global-matrix [[0 0 0] [0 0 0]]}). This is one way to do it but not necessarily the best way. Instead of binding the matrix to a var, you could pass it in the request map and then do (ring-resp/response {:matrix (reset! (:matrix request) [[0 0 0][0 0 0]])})
    – Rulle
    Nov 7 at 21:14








  • 1




    Perfect! It is working fine now. Thanks guys!
    – andrehigher
    Nov 8 at 0:08














  • 3




    Why are you trying to change global-matrix? Either it should be immutable and you locally work with modified copies of it, or if you really need mutation, you could make it an atom and reset! it. Clojure is highly functional leaning though, and most of the time, immutable structures are the most appropriate. I've never used Ring, so I can't make any suggestions regarding it unfortunately.
    – Carcigenicate
    Nov 7 at 20:47












  • I need to create a REST API to change this matrix everytime it is called. There is no way to persist this matrix in memory without defining as a variable right? I got it is immutable but how could I change it then? @Carcigenicate
    – andrehigher
    Nov 7 at 21:03






  • 1




    Make global-matrix an atom holding an immutable structure (like a vector of vectors), and swap! or reset! the atom to change it. atoms are mutable containers for immutable values.
    – Carcigenicate
    Nov 7 at 21:05








  • 1




    An atom is an effective way to do this. You would just do (def global-matrix (atom nil)) and then in your function, do (ring-resp/response {:matrix (reset! global-matrix [[0 0 0] [0 0 0]]}). This is one way to do it but not necessarily the best way. Instead of binding the matrix to a var, you could pass it in the request map and then do (ring-resp/response {:matrix (reset! (:matrix request) [[0 0 0][0 0 0]])})
    – Rulle
    Nov 7 at 21:14








  • 1




    Perfect! It is working fine now. Thanks guys!
    – andrehigher
    Nov 8 at 0:08








3




3




Why are you trying to change global-matrix? Either it should be immutable and you locally work with modified copies of it, or if you really need mutation, you could make it an atom and reset! it. Clojure is highly functional leaning though, and most of the time, immutable structures are the most appropriate. I've never used Ring, so I can't make any suggestions regarding it unfortunately.
– Carcigenicate
Nov 7 at 20:47






Why are you trying to change global-matrix? Either it should be immutable and you locally work with modified copies of it, or if you really need mutation, you could make it an atom and reset! it. Clojure is highly functional leaning though, and most of the time, immutable structures are the most appropriate. I've never used Ring, so I can't make any suggestions regarding it unfortunately.
– Carcigenicate
Nov 7 at 20:47














I need to create a REST API to change this matrix everytime it is called. There is no way to persist this matrix in memory without defining as a variable right? I got it is immutable but how could I change it then? @Carcigenicate
– andrehigher
Nov 7 at 21:03




I need to create a REST API to change this matrix everytime it is called. There is no way to persist this matrix in memory without defining as a variable right? I got it is immutable but how could I change it then? @Carcigenicate
– andrehigher
Nov 7 at 21:03




1




1




Make global-matrix an atom holding an immutable structure (like a vector of vectors), and swap! or reset! the atom to change it. atoms are mutable containers for immutable values.
– Carcigenicate
Nov 7 at 21:05






Make global-matrix an atom holding an immutable structure (like a vector of vectors), and swap! or reset! the atom to change it. atoms are mutable containers for immutable values.
– Carcigenicate
Nov 7 at 21:05






1




1




An atom is an effective way to do this. You would just do (def global-matrix (atom nil)) and then in your function, do (ring-resp/response {:matrix (reset! global-matrix [[0 0 0] [0 0 0]]}). This is one way to do it but not necessarily the best way. Instead of binding the matrix to a var, you could pass it in the request map and then do (ring-resp/response {:matrix (reset! (:matrix request) [[0 0 0][0 0 0]])})
– Rulle
Nov 7 at 21:14






An atom is an effective way to do this. You would just do (def global-matrix (atom nil)) and then in your function, do (ring-resp/response {:matrix (reset! global-matrix [[0 0 0] [0 0 0]]}). This is one way to do it but not necessarily the best way. Instead of binding the matrix to a var, you could pass it in the request map and then do (ring-resp/response {:matrix (reset! (:matrix request) [[0 0 0][0 0 0]])})
– Rulle
Nov 7 at 21:14






1




1




Perfect! It is working fine now. Thanks guys!
– andrehigher
Nov 8 at 0:08




Perfect! It is working fine now. Thanks guys!
– andrehigher
Nov 8 at 0:08

















active

oldest

votes











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',
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
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53197228%2fdefine-and-redefine-a-matrix-in-clojure%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53197228%2fdefine-and-redefine-a-matrix-in-clojure%23new-answer', 'question_page');
}
);

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







這個網誌中的熱門文章

Hercules Kyvelos

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud