Created
March 4, 2022 08:03
-
-
Save ilyash-b/909461aee302de7f3286d1470863119f to your computer and use it in GitHub Desktop.
Recurse into hashes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
F recurse_hash(h:Hash, cb:Fun) { | |
F is_not_hash(x) x is not Hash | |
h.tr(is_not_hash -> F(val, ctx) { | |
cb(val, ctx._path) | |
}) | |
} | |
test("recurse_hash(Hash, Fun) sum", { | |
h = { | |
'a': {'b': 2} | |
'c': 3 | |
} | |
sum = collector/0 { | |
recurse_hash(h, F(val, _path) collect(val)) | |
# Shorter alternative to the above line: | |
# recurse_hash(h, collect(X)) | |
} | |
assert(sum, 5) | |
}) | |
F recurse_hash(h:Hash, pa:PatternAction) { | |
F is_not_hash(x) x is not Hash | |
h.tr(is_not_hash -> F(val, ctx) { | |
if val =~ pa.pattern { | |
pa::action(val, ctx._path) | |
} | |
}) | |
} | |
test("recurse_hash(Hash, PatternAction) sum", { | |
h = { | |
'a': {'b': 2} | |
'c': 3 | |
'd': 'a string' | |
'e': 10 | |
} | |
r = Result({ collector/0 recurse_hash(h, F(val, _path) collect(val)) }) | |
assert(r, Failure) | |
assert(r, {'val': MethodNotFound}) | |
assert(r, {'val': {'args': [5, 'a string']}}) | |
sum = collector/0 recurse_hash(h, Int -> F(val, _path) collect(val)) | |
assert(sum, 15) | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi IIya,
Thank you for considering implementing this deep hash-based data structure's walk method.
From what I understand, this
recurse_hash()
has 2 variantsFun
object as its 2nd argumentPatternAction
object as its 2nd argumentPatternAction
is a comprise because in my FR both the condition or pattern and the Action which I calledclosure
wereclosure
sTo be honest, I don't understand all of your test examples yet, but that's ok for now.
Well done!