From 67a48150242a8f1626e7cb717c7f3f5c40243594 Mon Sep 17 00:00:00 2001 From: Kodi Arfer Date: Wed, 26 Jul 2017 19:10:54 -0700 Subject: [PATCH] Shadow `get` (#1344) --- NEWS | 1 + hy/core/shadow.hy | 9 ++++++++- tests/native_tests/operators.hy | 8 ++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/NEWS b/NEWS index 3785792..0e9fbd8 100644 --- a/NEWS +++ b/NEWS @@ -12,6 +12,7 @@ Changes from 0.13.0 as necessary, so you can write ``(eval `(+ 1 ~n))`` instead of ``(eval `(+ 1 ~(HyInteger n)))`` * Literal `Inf`s and `NaN`s must now be capitalized like that + * `get` is available as a function [ Bug Fixes ] * Numeric literals are no longer parsed as symbols when followed by a dot diff --git a/hy/core/shadow.hy b/hy/core/shadow.hy index c7771ee..ef63da6 100644 --- a/hy/core/shadow.hy +++ b/hy/core/shadow.hy @@ -138,11 +138,18 @@ (defn not-in [x y] (not-in x y)) +(defn get [coll key1 &rest keys] + (setv coll (get coll key1)) + (for* [k keys] + (setv coll (get coll k))) + coll) + (setv *exports* [ '+ '- '* '** '/ '// '% '@ '<< '>> '& '| '^ '~ '< '> '<= '>= '= '!= 'and 'or 'not - 'is 'is-not 'in 'not-in]) + 'is 'is-not 'in 'not-in + 'get]) (if (not PY35) (.remove *exports* '@)) diff --git a/tests/native_tests/operators.hy b/tests/native_tests/operators.hy index be40cc5..ac1fc90 100644 --- a/tests/native_tests/operators.hy +++ b/tests/native_tests/operators.hy @@ -288,3 +288,11 @@ (assert (is (f 3 [1 2]) (!= f-name "in"))) (assert (is (f 2 [1 2]) (= f-name "in"))) (forbid (f 2 [1 2] [3 4]))) + + +(op-and-shadow-test [get] + (forbid (f)) + (forbid (f "hello")) + (assert (= (f "hello" 1) "e")) + (assert (= (f [[1 2 3] [4 5 6] [7 8 9]] 1 2) 6)) + (assert (= (f {"x" {"y" {"z" 12}}} "x" "y" "z") 12)))