slack1
2017-12-21 20:43:36

How might one dynamically access a struct?


slack1
2017-12-21 20:44:02

Such as (struct-{calculated} struct)


lexi.lambda
2017-12-21 20:47:33

you usually don’t; structs are fairly static by design. but maybe if you explain what you’re trying to do we can give a better answer.


slack1
2017-12-21 20:52:53

I’ll try to hide the details of my problem, but I’ve mapped numbers onto letters of the alphabet in a struct


slack1
2017-12-21 20:53:15

And now I would like to dynamically access the values


slack1
2017-12-21 20:53:39

I wish to create a function that does letter->number


slack1
2017-12-21 20:54:00

And I thought a function that looks up a struct literal was the most fitting representation


dedbox
2017-12-21 20:57:43

@slack1 the opposite direction (e.g. number->whatever is easier to think about. That’s whatvectors are for.


brendan
2017-12-21 20:58:49

I think a dictionary would be a better fit than a struct the other way


dedbox
2017-12-21 20:59:11

In the forward direction, is reach for a hash / dict first.


slack1
2017-12-21 20:59:26

Ah, I just thought that a struct was an immutable dictionary / hash


dedbox
2017-12-21 21:00:19

I think they’re implemented with vectors, actually.


dedbox
2017-12-21 21:01:30

Not sure when Racket does the name-to-index lookup for you.


slack1
2017-12-21 21:07:58

What’s the difference between an immutable hash and a struct?


slack1
2017-12-21 21:08:24

Aside from access semantics and pre-included methods


brendan
2017-12-21 21:09:14

I’d say it’s that a hash can have keys of anything hashable, but the “keys” of a struct are just symbols


slack1
2017-12-21 21:09:39

Oh I see that’s an interesting distinction


slack1
2017-12-21 21:24:55

I guess I might also ask, how do people differ between arrays and vectors?


slack1
2017-12-21 21:25:01

Whether immutable / growable


notjack
2017-12-21 23:45:55

I’d say arrays are best if you’re doing things like slicing, flipping, merging, or folding over elements, especially if you need 2d or 3d arrays


notjack
2017-12-21 23:46:43

vectors work best if you just need constant time random access by index and don’t care much about other sorts of operations


notjack
2017-12-21 23:47:32

and generally I prefer to stick an immutable data structure in a mutable box instead of using a mutable data structure


notjack
2017-12-21 23:48:02

so I’ve yet to find a use for growable vectors


dedbox
2017-12-22 03:43:10

It helps to remember that arrays are in the math library, and come with a lot of linear algebra operations.


dedbox
2017-12-22 04:14:10

You can think of a hash as a normal array (Racket vector) that uses a “hashing” function to translate keys into numeric indices.


dedbox
2017-12-22 06:15:27

@slack1 have you considered using an association list, or “alist,” instead of a hash or struct? Alists are a LISP/Scheme idiom, so the base library has excellent support for them. It may also be the most efficient solution to your problem.