Ruby hash with lazy keys
up vote
0
down vote
favorite
I have a collection of 'data endpoints'. Each endpoint has a name and can be available or unavailable. In Ruby I want to present the available endpoints as a Hash to make it easy to work with them. The difficulty is that getting information about the endpoints is costly and should be done lazily.
Some examples of how I want my object to behave:
endpoints = get_endpoints.call # No endpoint information is accessed yet
result = endpoints['name1'] # This should only query endpoint "name1"
is_available = endpoints.key? 'name2' # This should only query endpoint "name2"
all_available = endpoints.keys # This has to query all endpoints
The comments describe how the object internally makes requests to the 'data endpoints'.
It is straightforward to make a Hash that can do the first 2 lines. However I don't know how to support the last 2 lines. To do this I need a way to make the keys lazy, not just the values.
Thank you for taking a look!
ruby hashmap key lazy-evaluation
|
show 5 more comments
up vote
0
down vote
favorite
I have a collection of 'data endpoints'. Each endpoint has a name and can be available or unavailable. In Ruby I want to present the available endpoints as a Hash to make it easy to work with them. The difficulty is that getting information about the endpoints is costly and should be done lazily.
Some examples of how I want my object to behave:
endpoints = get_endpoints.call # No endpoint information is accessed yet
result = endpoints['name1'] # This should only query endpoint "name1"
is_available = endpoints.key? 'name2' # This should only query endpoint "name2"
all_available = endpoints.keys # This has to query all endpoints
The comments describe how the object internally makes requests to the 'data endpoints'.
It is straightforward to make a Hash that can do the first 2 lines. However I don't know how to support the last 2 lines. To do this I need a way to make the keys lazy, not just the values.
Thank you for taking a look!
ruby hashmap key lazy-evaluation
Not sure what aspect of Hash you would like to be lazy-implemented ? Keys evaluation ? How would it be addressed in memory ?
– MrAleister
Nov 21 at 14:18
Hi @MrAleister Yes, keys evaluation (or rather, all aspects of the map). I have no idea how it would be addressed in memory. I thought that didn't really matter since it's object oriented, the same way I can just provide a lazy evaluation block for the values I would hope to provide a lazy evaluation block for the keys.
– user183966
Nov 21 at 14:22
What do you mean by "I have made the value lookup lazy in the straightforward way"? Why is that "no advantage in [your] scenario if [you] have to look up the keys eagerly"?
– Tom Lord
Nov 21 at 14:42
1
I don't understand what you're trying to achieve. (Why do you want to do this?) Can you show what code you've written so far? You could just usemy_hash.keys.lazy
, but... why? What would be the purpose?
– Tom Lord
Nov 21 at 14:45
Hi @TomLord . It is not laziness on the output side of the hash that I want, but laziness on the input side of the hash. I hope that makes sense.
– user183966
Nov 21 at 15:02
|
show 5 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a collection of 'data endpoints'. Each endpoint has a name and can be available or unavailable. In Ruby I want to present the available endpoints as a Hash to make it easy to work with them. The difficulty is that getting information about the endpoints is costly and should be done lazily.
Some examples of how I want my object to behave:
endpoints = get_endpoints.call # No endpoint information is accessed yet
result = endpoints['name1'] # This should only query endpoint "name1"
is_available = endpoints.key? 'name2' # This should only query endpoint "name2"
all_available = endpoints.keys # This has to query all endpoints
The comments describe how the object internally makes requests to the 'data endpoints'.
It is straightforward to make a Hash that can do the first 2 lines. However I don't know how to support the last 2 lines. To do this I need a way to make the keys lazy, not just the values.
Thank you for taking a look!
ruby hashmap key lazy-evaluation
I have a collection of 'data endpoints'. Each endpoint has a name and can be available or unavailable. In Ruby I want to present the available endpoints as a Hash to make it easy to work with them. The difficulty is that getting information about the endpoints is costly and should be done lazily.
Some examples of how I want my object to behave:
endpoints = get_endpoints.call # No endpoint information is accessed yet
result = endpoints['name1'] # This should only query endpoint "name1"
is_available = endpoints.key? 'name2' # This should only query endpoint "name2"
all_available = endpoints.keys # This has to query all endpoints
The comments describe how the object internally makes requests to the 'data endpoints'.
It is straightforward to make a Hash that can do the first 2 lines. However I don't know how to support the last 2 lines. To do this I need a way to make the keys lazy, not just the values.
Thank you for taking a look!
ruby hashmap key lazy-evaluation
ruby hashmap key lazy-evaluation
edited Nov 21 at 22:29
asked Nov 21 at 14:10
user183966
11
11
Not sure what aspect of Hash you would like to be lazy-implemented ? Keys evaluation ? How would it be addressed in memory ?
– MrAleister
Nov 21 at 14:18
Hi @MrAleister Yes, keys evaluation (or rather, all aspects of the map). I have no idea how it would be addressed in memory. I thought that didn't really matter since it's object oriented, the same way I can just provide a lazy evaluation block for the values I would hope to provide a lazy evaluation block for the keys.
– user183966
Nov 21 at 14:22
What do you mean by "I have made the value lookup lazy in the straightforward way"? Why is that "no advantage in [your] scenario if [you] have to look up the keys eagerly"?
– Tom Lord
Nov 21 at 14:42
1
I don't understand what you're trying to achieve. (Why do you want to do this?) Can you show what code you've written so far? You could just usemy_hash.keys.lazy
, but... why? What would be the purpose?
– Tom Lord
Nov 21 at 14:45
Hi @TomLord . It is not laziness on the output side of the hash that I want, but laziness on the input side of the hash. I hope that makes sense.
– user183966
Nov 21 at 15:02
|
show 5 more comments
Not sure what aspect of Hash you would like to be lazy-implemented ? Keys evaluation ? How would it be addressed in memory ?
– MrAleister
Nov 21 at 14:18
Hi @MrAleister Yes, keys evaluation (or rather, all aspects of the map). I have no idea how it would be addressed in memory. I thought that didn't really matter since it's object oriented, the same way I can just provide a lazy evaluation block for the values I would hope to provide a lazy evaluation block for the keys.
– user183966
Nov 21 at 14:22
What do you mean by "I have made the value lookup lazy in the straightforward way"? Why is that "no advantage in [your] scenario if [you] have to look up the keys eagerly"?
– Tom Lord
Nov 21 at 14:42
1
I don't understand what you're trying to achieve. (Why do you want to do this?) Can you show what code you've written so far? You could just usemy_hash.keys.lazy
, but... why? What would be the purpose?
– Tom Lord
Nov 21 at 14:45
Hi @TomLord . It is not laziness on the output side of the hash that I want, but laziness on the input side of the hash. I hope that makes sense.
– user183966
Nov 21 at 15:02
Not sure what aspect of Hash you would like to be lazy-implemented ? Keys evaluation ? How would it be addressed in memory ?
– MrAleister
Nov 21 at 14:18
Not sure what aspect of Hash you would like to be lazy-implemented ? Keys evaluation ? How would it be addressed in memory ?
– MrAleister
Nov 21 at 14:18
Hi @MrAleister Yes, keys evaluation (or rather, all aspects of the map). I have no idea how it would be addressed in memory. I thought that didn't really matter since it's object oriented, the same way I can just provide a lazy evaluation block for the values I would hope to provide a lazy evaluation block for the keys.
– user183966
Nov 21 at 14:22
Hi @MrAleister Yes, keys evaluation (or rather, all aspects of the map). I have no idea how it would be addressed in memory. I thought that didn't really matter since it's object oriented, the same way I can just provide a lazy evaluation block for the values I would hope to provide a lazy evaluation block for the keys.
– user183966
Nov 21 at 14:22
What do you mean by "I have made the value lookup lazy in the straightforward way"? Why is that "no advantage in [your] scenario if [you] have to look up the keys eagerly"?
– Tom Lord
Nov 21 at 14:42
What do you mean by "I have made the value lookup lazy in the straightforward way"? Why is that "no advantage in [your] scenario if [you] have to look up the keys eagerly"?
– Tom Lord
Nov 21 at 14:42
1
1
I don't understand what you're trying to achieve. (Why do you want to do this?) Can you show what code you've written so far? You could just use
my_hash.keys.lazy
, but... why? What would be the purpose?– Tom Lord
Nov 21 at 14:45
I don't understand what you're trying to achieve. (Why do you want to do this?) Can you show what code you've written so far? You could just use
my_hash.keys.lazy
, but... why? What would be the purpose?– Tom Lord
Nov 21 at 14:45
Hi @TomLord . It is not laziness on the output side of the hash that I want, but laziness on the input side of the hash. I hope that makes sense.
– user183966
Nov 21 at 15:02
Hi @TomLord . It is not laziness on the output side of the hash that I want, but laziness on the input side of the hash. I hope that makes sense.
– user183966
Nov 21 at 15:02
|
show 5 more comments
1 Answer
1
active
oldest
votes
up vote
0
down vote
You'd have to override the key?
method, and do your own checking in there.
class LazyHash < Hash
def key?(key)
# Do your checking here. However that looks for your application
end
end
In my opinion, you're asking for trouble though. One of the most powerful virtues in computer science is expectability. If you're changing the behavior of something, modifying it far beyond it's intent, it doesn't serve you to continue calling it by the original name. You don't need to shoe-horn your solution into existing classes/interfaces.
Programming offers you plenty of flexibility, so you can do stuff like this (dependent on the language of course), but in that same argument, you have no reason not to simply build a new object/service with it's own API.
I recommend starting fresh with a new class and building out your desired interface and functionality.
class LazyEndpoints
def on?(name)
end
def set(name, value)
end
end
(Or something like that, the world is yours for the taking!)
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You'd have to override the key?
method, and do your own checking in there.
class LazyHash < Hash
def key?(key)
# Do your checking here. However that looks for your application
end
end
In my opinion, you're asking for trouble though. One of the most powerful virtues in computer science is expectability. If you're changing the behavior of something, modifying it far beyond it's intent, it doesn't serve you to continue calling it by the original name. You don't need to shoe-horn your solution into existing classes/interfaces.
Programming offers you plenty of flexibility, so you can do stuff like this (dependent on the language of course), but in that same argument, you have no reason not to simply build a new object/service with it's own API.
I recommend starting fresh with a new class and building out your desired interface and functionality.
class LazyEndpoints
def on?(name)
end
def set(name, value)
end
end
(Or something like that, the world is yours for the taking!)
add a comment |
up vote
0
down vote
You'd have to override the key?
method, and do your own checking in there.
class LazyHash < Hash
def key?(key)
# Do your checking here. However that looks for your application
end
end
In my opinion, you're asking for trouble though. One of the most powerful virtues in computer science is expectability. If you're changing the behavior of something, modifying it far beyond it's intent, it doesn't serve you to continue calling it by the original name. You don't need to shoe-horn your solution into existing classes/interfaces.
Programming offers you plenty of flexibility, so you can do stuff like this (dependent on the language of course), but in that same argument, you have no reason not to simply build a new object/service with it's own API.
I recommend starting fresh with a new class and building out your desired interface and functionality.
class LazyEndpoints
def on?(name)
end
def set(name, value)
end
end
(Or something like that, the world is yours for the taking!)
add a comment |
up vote
0
down vote
up vote
0
down vote
You'd have to override the key?
method, and do your own checking in there.
class LazyHash < Hash
def key?(key)
# Do your checking here. However that looks for your application
end
end
In my opinion, you're asking for trouble though. One of the most powerful virtues in computer science is expectability. If you're changing the behavior of something, modifying it far beyond it's intent, it doesn't serve you to continue calling it by the original name. You don't need to shoe-horn your solution into existing classes/interfaces.
Programming offers you plenty of flexibility, so you can do stuff like this (dependent on the language of course), but in that same argument, you have no reason not to simply build a new object/service with it's own API.
I recommend starting fresh with a new class and building out your desired interface and functionality.
class LazyEndpoints
def on?(name)
end
def set(name, value)
end
end
(Or something like that, the world is yours for the taking!)
You'd have to override the key?
method, and do your own checking in there.
class LazyHash < Hash
def key?(key)
# Do your checking here. However that looks for your application
end
end
In my opinion, you're asking for trouble though. One of the most powerful virtues in computer science is expectability. If you're changing the behavior of something, modifying it far beyond it's intent, it doesn't serve you to continue calling it by the original name. You don't need to shoe-horn your solution into existing classes/interfaces.
Programming offers you plenty of flexibility, so you can do stuff like this (dependent on the language of course), but in that same argument, you have no reason not to simply build a new object/service with it's own API.
I recommend starting fresh with a new class and building out your desired interface and functionality.
class LazyEndpoints
def on?(name)
end
def set(name, value)
end
end
(Or something like that, the world is yours for the taking!)
answered Nov 22 at 15:11
Volte
1,5731322
1,5731322
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53413940%2fruby-hash-with-lazy-keys%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
Not sure what aspect of Hash you would like to be lazy-implemented ? Keys evaluation ? How would it be addressed in memory ?
– MrAleister
Nov 21 at 14:18
Hi @MrAleister Yes, keys evaluation (or rather, all aspects of the map). I have no idea how it would be addressed in memory. I thought that didn't really matter since it's object oriented, the same way I can just provide a lazy evaluation block for the values I would hope to provide a lazy evaluation block for the keys.
– user183966
Nov 21 at 14:22
What do you mean by "I have made the value lookup lazy in the straightforward way"? Why is that "no advantage in [your] scenario if [you] have to look up the keys eagerly"?
– Tom Lord
Nov 21 at 14:42
1
I don't understand what you're trying to achieve. (Why do you want to do this?) Can you show what code you've written so far? You could just use
my_hash.keys.lazy
, but... why? What would be the purpose?– Tom Lord
Nov 21 at 14:45
Hi @TomLord . It is not laziness on the output side of the hash that I want, but laziness on the input side of the hash. I hope that makes sense.
– user183966
Nov 21 at 15:02