beginner question

74 views
Skip to first unread message

Dennis Haupt

unread,
Sep 24, 2011, 3:36:31 PM9/24/11
to clo...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

in java, i would start coding a game with a loop like this:
while (true) {
logic();
render();
}

i would store the current state of the world in an object containing
the complete data of the whole game and update its values in each
iteration.

how would i do this in clojure?

the outer loop could look like
(def next [oldstate] (....)) <- input = current game, return value =
next iteration

(loop [world initalState] (recur (next world))) // <- the loop

but how would be world look like? the "best" (most trivial) thing that
i could think of is for it to be a map which is passed along several
transform functions, for example

(def playerHealthRegen [world] (...)) <- input = world (a map), output
= a new map with a new entry at key "playerhealth"

each function would then return a slightly modified version of the
world, and at the end, i'll have my completely new state.

is that about right? or is there a completely different way i overlooked?

- --

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.14 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQIcBAEBAgAGBQJOfjE+AAoJENRtux+h35aGo0MQAMipkc8e0YTPxsWsLzaVoQuz
MtXerKHHqqbuyxy+mzlc4xFfFUfs//wQGdk/ExZhby7eNVBc9AGYKarCyG/DVxfM
HwN7RVHIKDtWoHQk71dthSAzkHgbZvFxjO2W3EkI10rTsCYNFx1WV4o/PMt/KYJj
phmtO9LcHmb/ySsLveTmSdJTYjSDb7ENudLbM2z/4SP9AqN21sU1HRNF/Y4gLnq3
tnnGmbpRU8Xs6xv8O8oluRrhjgpGF58okG+JnnW+aqF95OaDMp2dQ2mPKxcWLzmt
zkMj41jC28By05oVPIIOstB50rOzU0VAQvEJRDohz2E2sxbhFfUci7G/75hvBkYz
vUXeQi4TCYM/gQlOOiAqUuutWpYWBbgL7OOHck3VkGn7UEKBguhkMTO/xGJjFxbY
6/pxIy7i7+DbSXfq+tu5sw2XAS96tctD1dWVdFjfpKukckvcDff3/L0ObKwIxTQu
BN9tqoUOs1Tp2OBJhkEJfaBMgUKqX5+IW/mKARVywNFLRWTAYs74OTO86ei/jTPo
kqwu2NGE9p/iHpLAxin8sz6I34kOlHJ2X7Xi4PBC19mmVgErt+A8MIvELuxhKBYw
BxoWZ11bccphKHFUdEDaj43pd1DqFhLqqpDvFWumUIO48pnDRpYcYcRLZ/6raCXv
apIq/CL5V7UHCJ+d/ANo
=Ckx2
-----END PGP SIGNATURE-----

Alan Malloy

unread,
Sep 24, 2011, 4:08:56 PM9/24/11
to Clojure
This is about right, though instead of a loop/recur you can just (map
render (iterate next-state start-state))
> Comment: Using GnuPG with Mozilla -http://enigmail.mozdev.org/

John

unread,
Sep 24, 2011, 4:22:39 PM9/24/11
to Clojure
To break down the update into multiple steps use the -> macro:

=>(defn step [world-state])
(-> world-state
update-health
update-physics
update-ai))

where e.g. update-health is something like

=>(defn update-health [world-state]
(update-in world-state [:player :health] inc))

then yeah just iterate or loop/recur

=>(take 20 (iterate step init-state))

this is the purely functional way of doing it.
> Comment: Using GnuPG with Mozilla -http://enigmail.mozdev.org/

Matt Hoyt

unread,
Sep 24, 2011, 4:26:14 PM9/24/11
to clo...@googlegroups.com
You need a check in the loop to see if the player wants to end the game.  Clojure doesn't have a break statement like Java so you created a infinite loop that will never end.  To make sure the game ends you need to have a base case.  Example of a main game loop in clojure:

(loop [game-state initial-state]
  (if (game-ends? game-state)
    (close-game game-state)
    (recur (render (logic game-state)))))

You should also look into records to store the game's state.  Records are faster than hash maps and you have polymorphism with protocols.  Be careful of the lazy functions in clojure like map.  It will only execute when you ask a value for it. 
 
Matt Hoyt

From: Dennis Haupt <d.ha...@googlemail.com>
To: clo...@googlegroups.com
Sent: Saturday, September 24, 2011 2:36 PM
Subject: beginner question
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+unsub...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Dennis Haupt

unread,
Sep 24, 2011, 4:54:22 PM9/24/11
to clo...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

i assumed my game to be so much fun that no one would ever want to
stop playing it.

Am 24.09.2011 22:26, schrieb Matt Hoyt:
> You need a check in the loop to see if the player wants to end the
> game. Clojure doesn't have a break statement like Java so you
> created a infinite loop that will never end. To make sure the game
> ends you need to have a base case. Example of a main game loop in
> clojure:
>
> (loop [game-state initial-state] (if (game-ends? game-state)
> (close-game game-state) (recur (render (logic game-state)))))
>
> You should also look into records to store the game's state.
> Records are faster than hash maps and you have polymorphism with
> protocols.

if i remember correctly, deftype = map, defrecord = class?
how does "assoc" work on records?


Be
> careful of the lazy functions in clojure like map. It will only
> execute when you ask a value for it.

render should do that

>
> Matt Hoyt
> ------------------------------------------------------------------------
>
>
*From:* Dennis Haupt <d.ha...@googlemail.com>
> *To:* clo...@googlegroups.com *Sent:* Saturday, September 24, 2011
> 2:36 PM *Subject:* beginner question


>
> in java, i would start coding a game with a loop like this: while
> (true) { logic(); render(); }
>
> i would store the current state of the world in an object
> containing the complete data of the whole game and update its
> values in each iteration.
>
> how would i do this in clojure?
>
> the outer loop could look like (def next [oldstate] (....)) <-
> input = current game, return value = next iteration
>
> (loop [world initalState] (recur (next world))) // <- the loop
>
> but how would be world look like? the "best" (most trivial) thing
> that i could think of is for it to be a map which is passed along
> several transform functions, for example
>
> (def playerHealthRegen [world] (...)) <- input = world (a map),
> output = a new map with a new entry at key "playerhealth"
>
> each function would then return a slightly modified version of the
> world, and at the end, i'll have my completely new state.
>
> is that about right? or is there a completely different way i
> overlooked?
>
>

> -- You received this message because you are subscribed to the
> Google Groups "Clojure" group. To post to this group, send email to

> clo...@googlegroups.com <mailto:clo...@googlegroups.com> Note


> that posts from new members are moderated - please be patient with
> your first post. To unsubscribe from this group, send email to

> clojure+u...@googlegroups.com
> <mailto:unsub...@googlegroups.com> For more options, visit this
> group at http://groups.google.com/group/clojure?hl=en
>
>

> -- You received this message because you are subscribed to the
> Google Groups "Clojure" group. To post to this group, send email to
> clo...@googlegroups.com Note that posts from new members are
> moderated - please be patient with your first post. To unsubscribe
> from this group, send email to

> clojure+u...@googlegroups.com For more options, visit this
> group at http://groups.google.com/group/clojure?hl=en


- --

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.14 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQIcBAEBAgAGBQJOfkN+AAoJENRtux+h35aGWBkQAKnIObEE9/uJV2FdOnOLEZ0P
nDSqdKvGr95+OdX24cTQqYcnsjYogaUSFaDjTEl95x68B2SKcAxKSrl35KMbAfry
pSYmcrRDEjDzjKMQf8RY1h5IMvacJFvpcuCX+VT96RCqBwTtjaLC4xG/iGpwLD5T
n9BAn6D5Js5HosAXg5bsu+0g4Lg04L/skaRsrPtA6YvwEkA+7IMCX2Y1s6zmZwYD
ciNWhVvBFsLiFtrPSmgCRw3tAPBWjx8JrtjvMtq5nrdi3hBQMUtSxOdTwWhbKwdT
V4LwynaTt8fusXVrV9cKlWVHH28o6OED2j8fh+Ndrz6MiG89Pjp0DXIYfHJECS4a
N5Pwvs1ID2l78yhoAlmU6IvaQyEcqqR2NzTxqXrv/HqbUYbfnZeX3HX7d6sCTec9
bJ3fFn0mpW8WFF7VCE08A90bNepISBcTMJ7RsI4fRoke/Vvt0DFU3IjyfHwy/Cil
4n3Slt0UsuCIZi+p+sG6zrI4PXDJ4JZ3QoWP7VQM2IerVPAdqw4srP2MHwrwQ17J
MdZFqqEF9ANke+rqQQDiZUGizgA8UT/VYlwUOHnKGqBoegt2TUhb69htvbaKwAIJ
PZMXbKn7eImS/KUQTNFZNYaKWkJJsE4HJ9Ac2EearQ8R6I6N65BKBITdXp+jVd2s
mgt8Hyn3Lgbdk4mTVH8U
=tnGf
-----END PGP SIGNATURE-----

Matt Hoyt

unread,
Sep 24, 2011, 5:11:55 PM9/24/11
to clo...@googlegroups.com
Both of them are java objects.  Records has more default functionality like implementing equals, hashcode, etc.  You can read more about the differences here: http://clojure.org/datatypes

assoc for records sets the value of the property for the record. 

Matt Hoyt
Sent: Saturday, September 24, 2011 3:54 PM
Subject: Re: beginner question

> <mailto:unsub...@googlegroups.com> For more options, visit this
> group at http://groups.google.com/group/clojure?hl=en
>
>
> -- You received this message because you are subscribed to the
> Google Groups "Clojure" group. To post to this group, send email to
> clo...@googlegroups.com Note that posts from new members are
> moderated - please be patient with your first post. To unsubscribe
> from this group, send email to
> clojure+unsub...@googlegroups.com For more options, visit this

Dennis Haupt

unread,
Sep 24, 2011, 5:13:53 PM9/24/11
to clo...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

mutable? like in "not functional"?
*reading*

Am 24.09.2011 23:11, schrieb Matt Hoyt:
> Both of them are java objects. Records has more default
> functionality like implementing equals, hashcode, etc. You can
> read more about the differences here: http://clojure.org/datatypes
>
> assoc for records sets the value of the property for the record.
>
> Matt Hoyt

> ------------------------------------------------------------------------
>
>
*From:* Dennis Haupt <d.ha...@googlemail.com>
> *To:* clo...@googlegroups.com *Sent:* Saturday, September 24, 2011

> 3:54 PM *Subject:* Re: beginner question


>
> i assumed my game to be so much fun that no one would ever want to
> stop playing it.
>
> Am 24.09.2011 22:26, schrieb Matt Hoyt:
>> You need a check in the loop to see if the player wants to end
>> the game. Clojure doesn't have a break statement like Java so
>> you created a infinite loop that will never end. To make sure
>> the game ends you need to have a base case. Example of a main
>> game loop in clojure:
>
>> (loop [game-state initial-state] (if (game-ends? game-state)
>> (close-game game-state) (recur (render (logic game-state)))))
>
>> You should also look into records to store the game's state.
>> Records are faster than hash maps and you have polymorphism with
>> protocols.
>
> if i remember correctly, deftype = map, defrecord = class? how does
> "assoc" work on records?
>
>
> Be
>> careful of the lazy functions in clojure like map. It will only
>> execute when you ask a value for it.
>
> render should do that
>
>
>> Matt Hoyt
>> ------------------------------------------------------------------------
>
>>
>
> *From:* Dennis Haupt <d.ha...@googlemail.com

> <mailto:d.ha...@googlemail.com>>
>> *To:* clo...@googlegroups.com <mailto:clo...@googlegroups.com>


> *Sent:* Saturday, September 24, 2011
>> 2:36 PM *Subject:* beginner question
>
>> in java, i would start coding a game with a loop like this:
>> while (true) { logic(); render(); }
>
>> i would store the current state of the world in an object
>> containing the complete data of the whole game and update its
>> values in each iteration.
>
>> how would i do this in clojure?
>
>> the outer loop could look like (def next [oldstate] (....)) <-
>> input = current game, return value = next iteration
>
>> (loop [world initalState] (recur (next world))) // <- the loop
>
>> but how would be world look like? the "best" (most trivial)
>> thing that i could think of is for it to be a map which is passed
>> along several transform functions, for example
>
>> (def playerHealthRegen [world] (...)) <- input = world (a map),
>> output = a new map with a new entry at key "playerhealth"
>
>> each function would then return a slightly modified version of
>> the world, and at the end, i'll have my completely new state.
>
>> is that about right? or is there a completely different way i
>> overlooked?
>
>
>> -- You received this message because you are subscribed to the
>> Google Groups "Clojure" group. To post to this group, send email
>> to clo...@googlegroups.com <mailto:clo...@googlegroups.com>

> <mailto:clo...@googlegroups.com <mailto:clo...@googlegroups.com>>


> Note
>> that posts from new members are moderated - please be patient
>> with your first post. To unsubscribe from this group, send email

>> to clojure+u...@googlegroups.com
>> <mailto:unsub...@googlegroups.com>
>> <mailto:unsub...@googlegroups.com


> <mailto:unsub...@googlegroups.com>> For more options, visit
> this
>> group at http://groups.google.com/group/clojure?hl=en
>
>
>> -- You received this message because you are subscribed to the
>> Google Groups "Clojure" group. To post to this group, send email

>> to clo...@googlegroups.com <mailto:clo...@googlegroups.com>


>> Note that
> posts from new members are
>> moderated - please be patient with your first post. To
>> unsubscribe from this group, send email to

>> clojure+u...@googlegroups.com


> <mailto:unsub...@googlegroups.com> For more options, visit this
>> group at http://groups.google.com/group/clojure?hl=en
>
>
>
> -- You received this message because you are subscribed to the
> Google Groups "Clojure" group. To post to this group, send email to

> clo...@googlegroups.com <mailto:clo...@googlegroups.com> Note
> that posts from new members are moderated - please be patient with
> your first post. To unsubscribe from this group, send email to

> clojure+u...@googlegroups.com


> <mailto:unsub...@googlegroups.com> For more options, visit this
> group at http://groups.google.com/group/clojure?hl=en
>
>
> -- You received this message because you are subscribed to the
> Google Groups "Clojure" group. To post to this group, send email to
> clo...@googlegroups.com Note that posts from new members are
> moderated - please be patient with your first post. To unsubscribe
> from this group, send email to

> clojure+u...@googlegroups.com For more options, visit this
> group at http://groups.google.com/group/clojure?hl=en


- --

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.14 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQIcBAEBAgAGBQJOfkgRAAoJENRtux+h35aGcvoP/iJ1c8jnt6OV3ALH9jFqdevW
ddHxYq54mK2QU+HzEbu0NSEikKd1djYE3DfJmJnnnm0gHVOOe/6OxC8BlMf1Zg+D
cFP2i9RnlrZeOocFIATZRk6hpv7+AwYOjICn/x4KV7/sRhw3kPkrZFz5hOTrH2bM
2sTP2WqRElPu+Mz59eL0yy5kKyHLk2azMOtW9IU98Z45onMHeZdx7ASH1/eIx6Iq
GdW+yPB5fRUXlWmDanGMG2fxvfglA5A+NFX4B6WziT2C1O8KU555ZYuzFPUhrqPT
uWU8HPWS7HqmeYyCgTHOW1W7m7NVQY4Yz9o4bCiyy3Im0/V659WB9eMEVq+hZzZc
kJ1+U2kWGG8bMxPOt7ytI6vDuFX2CYv/tHnArX68AXKOqVgsiMmfA/yzoIkXzW6W
U07MgMdS0jVKPH+hA7tqVNsgEcEAQLA5uqVewDO7UbeqJKsmiwZyZHvZLHXj9t3T
EfPfhXrdl0W3UqWN2EUqkiveBuHwGqGhkfsxsSlIVFzkjsSCOqMSPpCpzsDGvB5v
0AUDAslmFOAAKi3AP0h+4JBrUMSnD2l1cQfWCci0hyALKQBVlI+gOtBNtYHzGQQG
kpFBsqvYbg1s1zRWLMOCBD7QXK5vs6rr/GsP1H00lFRlJ+YyKnNusulIxW/hrgsU
aftax9uhf6FsI4cCwOhu
=nrB6
-----END PGP SIGNATURE-----

Dennis Haupt

unread,
Sep 24, 2011, 5:18:49 PM9/24/11
to clo...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

the website says:

deftype supports mutable fields, defrecord does not

so deftype seems to be what would be a java bean with simple
properties in java

Am 24.09.2011 23:11, schrieb Matt Hoyt:

> Both of them are java objects. Records has more default
> functionality like implementing equals, hashcode, etc. You can
> read more about the differences here: http://clojure.org/datatypes
>
> assoc for records sets the value of the property for the record.
>
> Matt Hoyt

> ------------------------------------------------------------------------
>
>
*From:* Dennis Haupt <d.ha...@googlemail.com>
> *To:* clo...@googlegroups.com *Sent:* Saturday, September 24, 2011

> 3:54 PM *Subject:* Re: beginner question
>

> i assumed my game to be so much fun that no one would ever want to
> stop playing it.
>
> Am 24.09.2011 22:26, schrieb Matt Hoyt:
>> You need a check in the loop to see if the player wants to end
>> the game. Clojure doesn't have a break statement like Java so
>> you created a infinite loop that will never end. To make sure
>> the game ends you need to have a base case. Example of a main
>> game loop in clojure:
>
>> (loop [game-state initial-state] (if (game-ends? game-state)
>> (close-game game-state) (recur (render (logic game-state)))))
>
>> You should also look into records to store the game's state.
>> Records are faster than hash maps and you have polymorphism with
>> protocols.
>
> if i remember correctly, deftype = map, defrecord = class? how does
> "assoc" work on records?
>
>
> Be
>> careful of the lazy functions in clojure like map. It will only
>> execute when you ask a value for it.
>
> render should do that
>
>
>> Matt Hoyt
>> ------------------------------------------------------------------------
>
>>
>
> *From:* Dennis Haupt <d.ha...@googlemail.com

> <mailto:d.ha...@googlemail.com>>
>> *To:* clo...@googlegroups.com <mailto:clo...@googlegroups.com>


> *Sent:* Saturday, September 24, 2011
>> 2:36 PM *Subject:* beginner question
>
>> in java, i would start coding a game with a loop like this:
>> while (true) { logic(); render(); }
>
>> i would store the current state of the world in an object
>> containing the complete data of the whole game and update its
>> values in each iteration.
>
>> how would i do this in clojure?
>
>> the outer loop could look like (def next [oldstate] (....)) <-
>> input = current game, return value = next iteration
>
>> (loop [world initalState] (recur (next world))) // <- the loop
>
>> but how would be world look like? the "best" (most trivial)
>> thing that i could think of is for it to be a map which is passed
>> along several transform functions, for example
>
>> (def playerHealthRegen [world] (...)) <- input = world (a map),
>> output = a new map with a new entry at key "playerhealth"
>
>> each function would then return a slightly modified version of
>> the world, and at the end, i'll have my completely new state.
>
>> is that about right? or is there a completely different way i
>> overlooked?
>
>
>> -- You received this message because you are subscribed to the
>> Google Groups "Clojure" group. To post to this group, send email
>> to clo...@googlegroups.com <mailto:clo...@googlegroups.com>

> <mailto:clo...@googlegroups.com <mailto:clo...@googlegroups.com>>


> Note
>> that posts from new members are moderated - please be patient
>> with your first post. To unsubscribe from this group, send email

> <mailto:unsub...@googlegroups.com>> For more options, visit
> this
>> group at http://groups.google.com/group/clojure?hl=en
>
>
>> -- You received this message because you are subscribed to the
>> Google Groups "Clojure" group. To post to this group, send email

>> to clo...@googlegroups.com <mailto:clo...@googlegroups.com>


>> Note that
> posts from new members are
>> moderated - please be patient with your first post. To
>> unsubscribe from this group, send email to

>> clojure+u...@googlegroups.com


> <mailto:unsub...@googlegroups.com> For more options, visit this
>> group at http://groups.google.com/group/clojure?hl=en
>
>
>
> -- You received this message because you are subscribed to the
> Google Groups "Clojure" group. To post to this group, send email to

> clo...@googlegroups.com <mailto:clo...@googlegroups.com> Note
> that posts from new members are moderated - please be patient with
> your first post. To unsubscribe from this group, send email to

> clojure+u...@googlegroups.com


> <mailto:unsub...@googlegroups.com> For more options, visit this
> group at http://groups.google.com/group/clojure?hl=en
>
>
> -- You received this message because you are subscribed to the
> Google Groups "Clojure" group. To post to this group, send email to
> clo...@googlegroups.com Note that posts from new members are
> moderated - please be patient with your first post. To unsubscribe
> from this group, send email to

> clojure+u...@googlegroups.com For more options, visit this
> group at http://groups.google.com/group/clojure?hl=en


- --

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.14 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQIcBAEBAgAGBQJOfkk4AAoJENRtux+h35aG2VAP/0+Z+1cN1nVgh2KG9JjlTIdj
KwXrNxPPCkbPu1SbVezCUfZMg0Fy2oSHsRkrP+5RnEM7NdA0jRrQskiUWcJlxsVK
0inPpd1Y8anADgsO6Kl2Wl0tA/XOMMTQFGj1DNex12npZ0PQgvDOa/GNgg3oUj8p
7bimHLWJ+fe4Ly7FVngqNWUZxXgXU1kRQ7MRZyzfFuAUOT/HGns58AugI588SmKQ
CcH+qkVBMUugXWsbJCt0EZUTlJW87np1iwiFHzUBSC5rKwb3MEfmhtGW/l+AgjKX
+/wcOkLb7/DC+1j9pbcrn1BeK3wKrwdlC+tk4HSAJuVPpoF8nQ4RJUtzKJ7ejgYd
g4XiAMkgIPfYcUMWsorzmtOyoLQ2upkFy9Eh05GHd+ajgSIrECdMAN9f/jTrgg7W
WYC03UWCzLpKZ9D9ShlMeqtAgG6P0v4xr3uC34qLAWnAm33wIrj+JcgPwveD9tM9
kUys3B+3WIz4BdRkI2Nf1svd5RLHVP67wnohqpXd/8SxEbSztwTJAncFEEuIcii8
5CMK+MMyUxe799ELNQIGg7b/YPtFJguVusel+r3EF/PLep4H5P/H4cSZ8HgPePtR
qsn4C3L7qOu0brix4ln7xgI717h58VJYesuZT4/O8OQWgyNLwDZdmUNTXlG3r2Ci
nYigqcxy9NfXcIC5/jhm
=kQJa
-----END PGP SIGNATURE-----

Stefan Kamphausen

unread,
Sep 25, 2011, 6:25:18 AM9/25/11
to clo...@googlegroups.com
Hi,

regarding the writing of a game in Clojure, I think http://codethat.wordpress.com/2011/09/10/writing-tetris-in-clojure/ is a good post to read.

Regards,
Stefan

Stuart Halloway

unread,
Sep 25, 2011, 8:00:06 AM9/25/11
to clo...@googlegroups.com
> the website says:
>
> deftype supports mutable fields, defrecord does not
>
> so deftype seems to be what would be a java bean with simple
> properties in java

Nope. :-)

Domain information should use defrecord, and should never be mutable. This is the closest thing to a Java bean, but is radically different in being (1) immutable, (2) persistent, and (3) accessible generically as a map. Game state would modeled with defrecord.

deftype is for things like custom data structures. In a Clojure-in-Clojure implementation, deftype would be used to implement maps, vectors, and lists. deftype's mutation ability would be used to implement transients.

Stu


Dennis Haupt

unread,
Sep 25, 2011, 9:12:07 AM9/25/11
to clo...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Am 25.09.2011 14:00, schrieb Stuart Halloway:
>> the website says:
>>
>> deftype supports mutable fields, defrecord does not
>>
>> so deftype seems to be what would be a java bean with simple
>> properties in java
>
> Nope. :-)
>
> Domain information should use defrecord, and should never be
> mutable. This is the closest thing to a Java bean, but is radically
> different in being (1) immutable, (2) persistent, and (3)
> accessible generically as a map. Game state would modeled with
> defrecord.

what's the difference between persistent and immutable?

>
> deftype is for things like custom data structures. In a
> Clojure-in-Clojure implementation, deftype would be used to
> implement maps, vectors, and lists. deftype's mutation ability
> would be used to implement transients.
>
> Stu
>
>


- --

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.14 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQIcBAEBAgAGBQJOfyimAAoJENRtux+h35aGtacQALmFsfHsPuCMt/QZM1gfwMNN
e3C0Q2Dju0GgG/PNGTyV25mWII0JGrvl5UK13VLb2q93bNW4l43SeS4glxE1USBd
Btbo1QwGiAkIffhSrcXLQT+2K4PM4b0fbLwkF41obAEVEf3JDpMjZ9Qjxbrz9D7v
WDH+JBx5rQxvk3ctPqtK59OCkH7fRtMM6bLuwHdFc9YhQt7VjmloaZaAmuXfL3+B
sq//dTOuUy94ZZSsERUWWYeqRIvL5gTVkT1QTbElVHCzixH25fnTX8G3b/04bmX6
ME2P/3iSsrYdHfO35caRwQfIm29JYkljwER1vyX0n4iotouodOyTxo4s4du5PZM1
G+xWrzw65ejn3Y6GaSOFjb02wJUhku2cHyXcjY+Xdb3RXNgtTBQurW5Jx+wCmGV5
Kj3GtharuM16n4weCS0aK5meNFFmu8Djn11+cWePyjQ9qVos1ei3f8s01bN13Qbv
p1qUh+5D4eJb39A4cETrJpINXIhXf2Ngg7tpyQQjzppT/iqtIVdKHMVFOiazHzh2
1aLFCBRGVoErCukB84fCmwZ9JJP8NiRzyw7choMbNeBREKhX9+WFJfwDZyygrezD
kTRa7F2iwz1gEfO3kP8vevdObmtQdQgAMxQ/4IG4xLtmptfL25U/5pgvvi9jOX5T
WYY70N5yr64Evgw6Bt9S
=y3Ge
-----END PGP SIGNATURE-----

Stuart Halloway

unread,
Sep 25, 2011, 9:28:33 AM9/25/11
to clo...@googlegroups.com
> what's the difference between persistent and immutable?

See http://en.wikipedia.org/wiki/Persistent_data_structure, which now has a nice shout out to Clojure.

Stu

Phil Hagelberg

unread,
Sep 25, 2011, 12:59:32 PM9/25/11
to clo...@googlegroups.com


On Sep 25, 2011 6:12 AM, "Dennis Haupt" <d.ha...@googlemail.com> wrote:
> what's the difference between persistent and immutable?

I have written a summary of this distinction on my blog: http://technomancy.us/132

Hope that helps.

-Phil

Dennis Haupt

unread,
Sep 25, 2011, 2:34:26 PM9/25/11
to clo...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

so there is no difference.


- --

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.14 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQIcBAEBAgAGBQJOf3QyAAoJENRtux+h35aGsKQP/jHBNqhUCQfSQ7XjCe3vTlF5
zhjcXmyhWbRUF7fEsrmljtpOu630gAnl51cmlBOwpOQXLmGJg386422GzUtRexG3
A4KcrYEahAKBK5R1Tiu3WMqAr/h3t9oYi6APYaU29qJqqO6lnZR/bp1yAR+wnmZT
ausVGzUlE+p5DlfWHAMaAsEVYp1XX282BKecgr/cHsBy9Jwl2NdWQl0Ss/ZYPj7x
JE04/y6T/1jomWdM+dwXZ/oCucWmDjSgg3nHMUy/P9yab0kN2qHprqomBCBQeOpj
zY5KS2/0x6nS9XpNfKF1f7VdqG8RkVdE+iE14a5uzCWzAQfMbeZUQcyOv8H8QQXU
v0VYO1htvWwoJRqoCUan5UhrucM+LDqFEml7n8S4y7kbWWy9CJHD0bq9VjgIBIJY
oo8VrFM7ciO+9mmJ7VfdNmUKPmcclWVFp3PVbNKJVMFXO7s+Myj5y/irWDJ3fBTY
VYiCKNtbU9uaNZRVfyTQhVF5i+607BfH78S6wA1fEXYWWcvNiwEBoTaKi4cHh6wF
nbaVf2xOLahJngEg6m8yBQfcnRm5H6WZ2h53UA+c1AgwiRgOAt+FGiK6J695EZbp
LlslJGUH9QVZXsDVWPthtr2Qk8fJtT7c7UQksfYU+F8lANtyTMcN/kAQ2ngZ2pbq
aLOthkidIm51E9NkBRH5
=SNNL
-----END PGP SIGNATURE-----

Dennis Haupt

unread,
Sep 25, 2011, 3:24:12 PM9/25/11
to clo...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

so persistent is immutable + x like "car" is "movable" + x. it doesn't
make sense to ask what the difference is.

Am 25.09.2011 18:59, schrieb Phil Hagelberg:
>
> On Sep 25, 2011 6:12 AM, "Dennis Haupt" <d.ha...@googlemail.com

> <mailto:d.ha...@googlemail.com>> wrote:
>> what's the difference between persistent and immutable?
>
> I have written a summary of this distinction on my blog:
> http://technomancy.us/132
>
> Hope that helps.
>
> -Phil
>

> -- You received this message because you are subscribed to the
> Google Groups "Clojure" group. To post to this group, send email to
> clo...@googlegroups.com Note that posts from new members are
> moderated - please be patient with your first post. To unsubscribe
> from this group, send email to
> clojure+u...@googlegroups.com For more options, visit this
> group at http://groups.google.com/group/clojure?hl=en


- --

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.14 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQIcBAEBAgAGBQJOf3/bAAoJENRtux+h35aGFrIQAJCTY0ZVAQ1LHda9tIkiV/am
hi/bSleO4qFJceGUWuoDP8nrTIc6kDZrhNv7IIYeV59GzETYsEctfUIIosNEnS94
Oya6DpoOWLFlVwLsSVrIkFNbl5WEnEhoi7myO3zxuO0PksefmzLs57oC5ijw24PP
3CQrGiLpO1dI9s7bk8fBEuBDnC+d0GOZH4YAa3EYMp/1VQoRFLgBy3Occ1DVcqcZ
nTac2Gexru0vA9On1RW6d0xSNEPQk33gAnt5fU72LCTi9bAekEPb66y538LKElS4
0nc4x6rEI9ijOLxvTkariCPEIy3yGZ5NcQxZWI1XR1c6J5sD8rsB3d5HZTzsy2Ez
n4NnRXw/zrIxyqLWwoYaZyfqltJdoIvE2qf9d4zMmuj0JHlW518GF7M3aXigdImt
uuSqQkUJ4tadSb/UKWITqEN+F/UpaohNamnWLCbvgDl4MGJ2kGU46tC91Su4N79e
iPfZ3gcyNMuRuO2i2v31OTw86TOwxGhtAJEEDvL8MRIce038fpt+QVybHUsDiGzS
G9OczcysVZstfPphSzfUpMsUUUNSc5lmyskYBQV6xiqwC8VOE+bY/+ejlNm4nHQC
8Qdfqzq7sI7gXPg5yUUvc3xaT8RdT/X2FqL1B54g17pz44gDJW9LlvNXNwP0jTOm
cBFvQnI1tQZRvEcqbaTO
=C3n7
-----END PGP SIGNATURE-----

Andy Fingerhut

unread,
Sep 25, 2011, 10:06:54 PM9/25/11
to clo...@googlegroups.com
All persistent data structures are immutable, but not all immutable data structures are persistent.

For example, imagine an immutable array that, unlike Clojure's vector data structure, implemented "conj" by copying the entire array into a new one with the original elements plus the new one.  Immutable, but the performance characteristics are abysmal.

As a more subtle example, imagine an immutable array that, unlike Clojure's vector data structure, let you "update" arbitrary elements, and did not copy the entire array, but created an imbalanced tree of vector indices that had their values "updated".  A lookup in this augmented data structure would require first looking through the imbalanced tree for the index, to see if it had been modified, and only if it failed to find it in the tree would it be looked up in the original array.  Immutable, but not persistent in the sense that it maintains the performance characteristics of the original data structure -- the augmented/updated data structure has slow access times than the original.

Andy

Reply all
Reply to author
Forward
0 new messages