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).
matrix clojure
|
show 3 more comments
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).
matrix clojure
3
Why are you trying to changeglobal-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 andreset!
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
Makeglobal-matrix
anatom
holding an immutable structure (like a vector of vectors), andswap!
orreset!
theatom
to change it.atom
s 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
|
show 3 more comments
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).
matrix clojure
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
matrix clojure
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 changeglobal-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 andreset!
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
Makeglobal-matrix
anatom
holding an immutable structure (like a vector of vectors), andswap!
orreset!
theatom
to change it.atom
s 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
|
show 3 more comments
3
Why are you trying to changeglobal-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 andreset!
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
Makeglobal-matrix
anatom
holding an immutable structure (like a vector of vectors), andswap!
orreset!
theatom
to change it.atom
s 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. atom
s 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. atom
s 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
|
show 3 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f53197228%2fdefine-and-redefine-a-matrix-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
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 andreset!
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
anatom
holding an immutable structure (like a vector of vectors), andswap!
orreset!
theatom
to change it.atom
s 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