Questions tagged [hy]
Hy is a Lisp dialect that's embedded in Python.
82
questions
0
votes
0
answers
58
views
+100
Show whether a Python module is loaded from bytecode
I'm trying to debug Hy's use of bytecode. In particular, each time a module is imported, I want to see the path it was actually imported from, whether source or bytecode. Under the hood, Hy manages ...
2
votes
1
answer
18
views
importing module from current folder in hy
Has import changed in Hy version 1.0a4+199.g22021c56?
I'm trying to import a file from the same folder (or cwd), but I get ModuleNotFoundError: No module named 'thenameofthefile'
Even if I append ...
1
vote
2
answers
26
views
hy build-in with-decorator throws NameError: name 'with_decorator' is not defined
I'm trying to run the with-deocorator example from hy manual (https://docs.hylang.org/en/stable/language/api.html?highlight=with-decorator#with-decorator):
(defn inc-decorator [func]
(fn [value-1 ...
1
vote
1
answer
23
views
Cut from negative integer to last item in list in Hy
If (list (range 10)) is [0 ... 9], and (cut (list (range 10)) -5 -1) is [5, 6, 7, 8], then how do I include the last item in the list as well, i.e. [5, 6, 7, 8, 9]? I've tried (cut (list (range 10)) -...
2
votes
2
answers
72
views
Dynamic bindings with let in Hy?
Comming from Common Lisp, I'm trying to use let to shadow the value of a global variable dynamically.
(setv glob 18)
(defn callee []
(print glob))
(defn nonl [x]
(callee)
(let [glob x]
(...
0
votes
1
answer
53
views
Does Hy have inline if?
What is the Hy equivalent of Python's a if condition else b?
I'm trying to convert something like this
return (quicksort(under) if under else []) + same + (quicksort(over) if over else [])
to Hy. It ...
1
vote
1
answer
51
views
Why is this quicksort overflowing?
I have the following Hy code:
(defn quicksort [lst]
(if (< (len lst) 2)
(return lst)
(do
(setv pivot (// (len lst) 2))
(setv (, under same over) (, [] [] []))
...
0
votes
1
answer
27
views
Convert python literal_eval string to hy
I would like to convert the following to Hy, but I can't seem to figure out how to do it; is there perhaps a way to convert a string of python code to the hy syntax? I don't know if py would work ...
0
votes
1
answer
29
views
Render mangled hy attribute names as regular strings
If I have an attribute self.internal/freezer in a class, and I raise an error via (raise (AttributeError f"Sorry! '{attr}' doesn't exist as an attribute!")), how can I get the attribute name ...
0
votes
1
answer
32
views
"Ran into a RPAREN where it wasn't expected" when using "->>"
I have the following code:
(require [hyrule [-> ->>]])
(defn static/freezer [value freezer]
(cond [(not value) (setv freezer [])]
[(...
0
votes
1
answer
33
views
Access attribute and method names with slashes or asterisks defined in Hy from Python
If I were to do the following:
(setv a/b 12)
How would I access the variable in Python, such as from a regular python module, or simply using (py)?
(py "print(a/b)")
0
votes
1
answer
41
views
Expanding macro results in KeyError
With the following (simplified) code:
(setv agnostic-manager-installers {})
(defmacro alias-assign [am &rest aliases]
(for [alias aliases] (assoc
agnostic-manager-installers
(...
0
votes
2
answers
60
views
Using `click` in a shell script that has a Hy program in a here document
How can I convert the following working example of using click with shell + python repl (I think) to hy?
python3 - "$@" <<'EOF'
import click
@click.command()
@click.option('--count', ...
1
vote
2
answers
60
views
Use get in threading macro
The following three examples fail with the following errors:
(setv dct {1 2 3 4})
(setv dcts "dct")
;; (print (get (. (globals) [dcts]) 1))
(print (-> (globals) (. [dcts]) (get 1)))
;; (...
1
vote
1
answer
39
views
Multiple iterators in "for", "lfor", etc. with something like "dct.items()"
How could I convert the following to hy?
dct = { 1:2, 3:4 }
print([i*j for i, j in dct.items()])
# => [2, 12]
1
vote
3
answers
64
views
Hylang "in" operator
Is there an in operator in Hylang? In python, for example, this would allow for the following test:
lst = [1, 2, 3, 4]
print(0 in lst)
# => False
0
votes
5
answers
134
views
Cosine Similarity in linear time using Lisp
The cosine similarity of two lists can be calculated in linear time using a for-loop. I'm curious as to how one would achieve this using a Lisp-like language. Below is an example of my code in Python ...
0
votes
0
answers
62
views
Parse arguments in Hy macro
I need help with parsing arguments in a Hy Macro that generates code for a class.
What I am trying to accomplish: A macro that will generate a class with predefined properties. The property names are ...
0
votes
3
answers
142
views
How to write a macro like dot(.) operator in hylang?
We have some async properties (using @property) and some usual ones. We have to use hy to check if one is async or not using asyncio.iscoroutine function. The problem is we have to use getattr and not ...
0
votes
1
answer
74
views
Hy equivalent to * operator in Python's function arguments that forces keyword arguments
Recently I have been trying to write a simple bot in Hy with Discord.py. In Discord.py we can write a command like this to turn the last argument into a full string that includes spaces:
@commands....
1
vote
1
answer
54
views
macro inside of macro creates a list of None
I have defined two macros:
(defmacro property [name type]
`(setv ^(of Optional ~type) ~name None))
(defmacro data-type [vname &rest propertys]
`(with-decorator dataclass
(defclass ~vname [...
1
vote
2
answers
42
views
Macro string Interpolation / How to unquote inside string?
I have a macro like:
(defmacro test [x] `"~x")
After running it expands to the following:
=> (test abc)
'~x'
Why doesn't it expand to 'abc'?
What is the correct method to unquote inside ...
1
vote
2
answers
121
views
How to serialize hylang s-expression into PostgreSQL json/json-b?
We are trying to use hylang as DSL for some financial business flow. We were trying to use business rules as JSON, but switched to DSL using hy language. Now we need to persist s-expression items into ...
-1
votes
1
answer
81
views
Function Type Annotations
I think Hy is fantastic! Plan on using it, possibly even hacking on it.
I've noticed type annotations are supported, but apparently only for variables, and at most, function arguments. It doesn't seem ...
0
votes
1
answer
61
views
Why are calls to `defmacro` evaluating to None?
I was writing the following pieces of code:
(require [hy.contrib.walk [let]])
(defn maybe? [command-text]
(let [splitted (.split command-text " ")]
(= (get splitted 0) "maybe&...
0
votes
1
answer
107
views
Dictionary merging in Hy
If I have two dictionaries, and want to merge them with preference to the latter regarding conflicting keys, I would do in Python:
In [1]: x = {'a': 1, 'b': 2}
In [2]: y = {'b': 3, 'c': 4}
In [3]: {*...
0
votes
1
answer
103
views
How to declare 'main' in a hy program
As per the official docs, I should be able to to use
def main [ &rest _ ]
I tried the example in a let block :
(import argparse)
(require [hy.contrib.walk [let]])
(def main [&rest _]
(...
2
votes
1
answer
96
views
What is the difference between if and cond?
Perhaps I am misunderstanding something.
In Hy, if* can take (after the if* symbol)
one predicate (evaluation returned if there are no further expressions)
zero or one consequent (evaluated and ...
0
votes
0
answers
83
views
Calysto Hy not starting properly
I have run an intro to Hy session for the Atlanta Functional Programming meetup.
One audience for this intro is Pythonistas that might want to use Hy to get acquainted with a Lispy syntax by porting ...
0
votes
1
answer
98
views
How to use the pandas apply method in hy
I'd like to create a new column in pandas using the .apply method where I can pass a custom function
df["newcol"] = df["oldcol"].apply(lambda x: x + 1)
or
df["newcol"] = ...
0
votes
1
answer
64
views
macro for pandas column setting in hy
I feel I'm writing functions needlessly for the following operation of setting several derived columns sequentially:
(defn add-cols[d]
(do
(setv (get d "col0") "0")
(...
0
votes
1
answer
56
views
configure automatic Hy REPL imports
I've been absolutely loving the Hy REPL. However it would speed up my workflow immensely if I could have a set of default imports at startup. Clojure has a way of configuring startup imports via ...
0
votes
1
answer
73
views
How to construct AST of Hy lang snippets
I have several Hy Lang code snippets that I use in my python project.
I was wondering if there is a relatively easy way for me to get an Abstract Syntax Tree of the Hy code with some python library or ...
2
votes
1
answer
229
views
HyLang : Howto simplest for loop in hy?
I wanna get Hy!
How, In Hylang, do I do the simple Python loop:
for i in range(5):
print(i)
0
votes
1
answer
88
views
how to use hy to when operating on pandas dataframe columns
There seems to be no documentation on how to use hy on single column pandas operation such as the following. Would appreciate any help:
# simple instantiation to scalar
df['a'] = '2'
# the above can ...
0
votes
2
answers
97
views
Does Hy code require indenting, just like Python?
I am unable to submit multiple lines of Hy to Python without indentation:
import hy
expr=hy.read_str("(+ 1 1)(+ 2 2)")
hy.eval(expr)
=> 2
The second '(+ 2 2)' statement has apparently ...
0
votes
1
answer
132
views
How do I setup a hy project so that I can use pytest for testing
I'm trying to test a hy project with pytest but am having trouble with pytest discovering my tests. What needs to be done so that pytest can pick up tests written in hy? I've made the assumption that ...
0
votes
1
answer
100
views
RecursionError in Python Using Hylang
First off let me just say I know what I'm doing is not ideal, but I'm trying to write the member? function from The Little Schemer using Hy.
(setv else True)
(defn null? [lst]
" If list is empty ...
0
votes
2
answers
81
views
Is there a way to capture a name from the environment when using hy.eval?
I'm trying to create functions in hylang and use them from python but the created functions don't seem to have access to the environment passed to hy.eval.
import hy
env = dict(x=5)
func = hy.eval(...
0
votes
1
answer
43
views
`self` can not use as arguments of a hy macro
The following macro tries to assign a member variable from init argument.
But
name 'self' is not defined
(defmacro optional_assign [x &optional [base self]]
`(lif ~x (setv (. ~base ~x) ~x) ...
0
votes
1
answer
85
views
Importing .hy file with function definition in .py file and after calling function from .hy from python
My .py file looks like:
import hy
import example
foo2()
My .hy file looks like:
(defn foo2 [] (+ 1 1))
.hy file is in same folder as .py file.
If I run .py file I am getting error:
runfile('D:/...
1
vote
1
answer
119
views
How to include Hy code into a separate file, and then import that using Hy?
I am just starting using Hy.
For example we have this Hy code:
(print "Hy, world!")
And we have two pieces of Python code. Piece one:
print("Some python code")
Piece two:
print("Some other ...
1
vote
2
answers
161
views
How to include Hy code inside python code?
For example we have this Hy code:
(print "Hy, world!")
And we have two pieces of Python code. Piece one:
print("Some python code")
Piece two:
print("Some other python code")
How can we make ...
0
votes
1
answer
37
views
HyLang import_buffer_to_ast removed version 0.18.0
I am updating from Hy 0.12.1 to the newest version 0.18.0, and I make heavy use of the import_buffer_to_ast function in my python code.
This function has been removed from hy.importer, I was hoping ...
2
votes
1
answer
114
views
Why is it that '0 is false, but 'False is true?
I was playing around with symbols and was surprised to see that:
hy 0.18.0 using CPython(default) 3.7.3 on Linux
=> (bool '0)
False
=> (bool 'False)
True
=>
Is that a design decision? What ...
0
votes
2
answers
191
views
Auto insert await when asyncio function in hy-lang
The following do/a macro can auto-insert await when asyncio function is used.
The following also shows usage.
(import asyncio)
(import time)
(defmacro do/a [&rest code]
`(do ~@(lfor p code
...
0
votes
1
answer
150
views
How can I create a Python standalone executable using Hy modules?
Given an entry file main.py like so:
#-*- coding: utf-8 -*-
# -*- mode: python -*-
import hy
import os.path
import hymodule
datas=[(os.path.dirname(hy.__file__), 'hy')]
hymodule.hello_world()
...
1
vote
1
answer
43
views
walk must return same type as input?
I'm unclear on how the outer function provided to walk should work.
The example in documentation referenced here:
https://docs.hylang.org/en/stable/contrib/walk.html
Suggests the outer function can ...
2
votes
1
answer
113
views
Pretty print in Hy?
Is there something like pprint or clojure.pprint/pprint available in Hy? More specific, is there something that would convert something like this:
{:a {:a 0 :b 1 :c 2 :d 3 :e 4} :b {:a 0 :b 1 :c 2 :d ...
0
votes
0
answers
53
views
Why is my version of Hy unknown and deftag undefined?
Why hy unknown?
How to use deftag?
Snippet from http://docs.hylang.org/en/stable/language/api.html#deftag
gt@gt-ThinkPad-X230:~$ hy --spy
hy unknown using CPython(default) 3.6.8 on Linux
...