parent
f1c315da24
commit
016d25d104
@ -207,6 +207,26 @@ Returns ``True`` if *x* is a float.
|
|||||||
False
|
False
|
||||||
|
|
||||||
|
|
||||||
|
.. _fraction-fn:
|
||||||
|
|
||||||
|
fraction
|
||||||
|
--------
|
||||||
|
|
||||||
|
Returns a Python object of type ``fractions.Fraction``.
|
||||||
|
|
||||||
|
.. code-block:: hy
|
||||||
|
|
||||||
|
=> (fraction 1 2)
|
||||||
|
Fraction(1, 2)
|
||||||
|
|
||||||
|
Note that Hy has a built-in fraction literal that does the same thing:
|
||||||
|
|
||||||
|
.. code-block:: hy
|
||||||
|
|
||||||
|
=> 1/2
|
||||||
|
Fraction(1, 2)
|
||||||
|
|
||||||
|
|
||||||
.. _even?-fn:
|
.. _even?-fn:
|
||||||
|
|
||||||
even?
|
even?
|
||||||
|
@ -200,6 +200,10 @@ Hy. Let's experiment with this in the hy interpreter::
|
|||||||
(1, 2, 3)
|
(1, 2, 3)
|
||||||
=> #{3 1 2}
|
=> #{3 1 2}
|
||||||
{1, 2, 3}
|
{1, 2, 3}
|
||||||
|
=> 1/2
|
||||||
|
Fraction(1, 2)
|
||||||
|
|
||||||
|
Notice the last two lines: Hy has a fraction literal like Clojure.
|
||||||
|
|
||||||
If you are familiar with other Lisps, you may be interested that Hy
|
If you are familiar with other Lisps, you may be interested that Hy
|
||||||
supports the Common Lisp method of quoting:
|
supports the Common Lisp method of quoting:
|
||||||
|
@ -1721,8 +1721,6 @@ class HyASTCompiler(object):
|
|||||||
col_offset=e.start_column)
|
col_offset=e.start_column)
|
||||||
|
|
||||||
@builds("%")
|
@builds("%")
|
||||||
@builds("/")
|
|
||||||
@builds("//")
|
|
||||||
@builds("**")
|
@builds("**")
|
||||||
@builds("<<")
|
@builds("<<")
|
||||||
@builds(">>")
|
@builds(">>")
|
||||||
@ -1764,11 +1762,14 @@ class HyASTCompiler(object):
|
|||||||
|
|
||||||
@builds("+")
|
@builds("+")
|
||||||
@builds("*")
|
@builds("*")
|
||||||
|
@builds("/")
|
||||||
|
@builds("//")
|
||||||
def compile_maths_expression_mul(self, expression):
|
def compile_maths_expression_mul(self, expression):
|
||||||
if len(expression) > 2:
|
if len(expression) > 2:
|
||||||
return self.compile_maths_expression(expression)
|
return self.compile_maths_expression(expression)
|
||||||
else:
|
else:
|
||||||
id_op = {"+": HyInteger(0), "*": HyInteger(1)}
|
id_op = {"+": HyInteger(0), "*": HyInteger(1), "/": HyInteger(1),
|
||||||
|
"//": HyInteger(1)}
|
||||||
|
|
||||||
op = expression.pop(0)
|
op = expression.pop(0)
|
||||||
arg = expression.pop(0) if expression else id_op[op]
|
arg = expression.pop(0) if expression else id_op[op]
|
||||||
|
@ -26,6 +26,7 @@
|
|||||||
(import itertools)
|
(import itertools)
|
||||||
(import functools)
|
(import functools)
|
||||||
(import collections)
|
(import collections)
|
||||||
|
(import [fractions [Fraction :as fraction]])
|
||||||
(import sys)
|
(import sys)
|
||||||
(import [hy._compat [long-type]]) ; long for python2, int for python3
|
(import [hy._compat [long-type]]) ; long for python2, int for python3
|
||||||
(import [hy.models.cons [HyCons]]
|
(import [hy.models.cons [HyCons]]
|
||||||
@ -418,10 +419,10 @@
|
|||||||
(def *exports* '[Botsbuildbots
|
(def *exports* '[Botsbuildbots
|
||||||
butlast calling-module-name coll? cons cons? cycle
|
butlast calling-module-name coll? cons cons? cycle
|
||||||
dec distinct disassemble drop drop-last drop-while empty? even?
|
dec distinct disassemble drop drop-last drop-while empty? even?
|
||||||
every? first filter filterfalse flatten float? gensym identity
|
every? first filter filterfalse flatten float? fraction gensym
|
||||||
inc input instance? integer integer? integer-char? interleave
|
identity inc input instance? integer integer? integer-char?
|
||||||
interpose iterable? iterate iterator? keyword keyword? last list*
|
interleave interpose iterable? iterate iterator? keyword
|
||||||
macroexpand macroexpand-1 map merge-with name neg? nil? none?
|
keyword? last list* macroexpand macroexpand-1 map merge-with
|
||||||
nth numeric? odd? pos? range read remove repeat repeatedly
|
name neg? nil? none? nth numeric? odd? pos? range read remove
|
||||||
rest reduce second some string string? symbol? take take-nth
|
repeat repeatedly rest reduce second some string string?
|
||||||
take-while zero? zip zip_longest zipwith])
|
symbol? take take-nth take-while zero? zip zip_longest zipwith])
|
||||||
|
@ -266,6 +266,14 @@ def t_identifier(p):
|
|||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
if '/' in obj:
|
||||||
|
try:
|
||||||
|
lhs, rhs = obj.split('/')
|
||||||
|
return HyExpression([HySymbol('fraction'), HyInteger(lhs),
|
||||||
|
HyInteger(rhs)])
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return HyFloat(obj)
|
return HyFloat(obj)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
|
@ -108,6 +108,13 @@ def test_lex_integers():
|
|||||||
assert objs == [HyInteger(42)]
|
assert objs == [HyInteger(42)]
|
||||||
|
|
||||||
|
|
||||||
|
def test_lex_fractions():
|
||||||
|
""" Make sure that fractions are valid expressions"""
|
||||||
|
objs = tokenize("1/2")
|
||||||
|
assert objs == [HyExpression([HySymbol("fraction"), HyInteger(1),
|
||||||
|
HyInteger(2)])]
|
||||||
|
|
||||||
|
|
||||||
def test_lex_expression_float():
|
def test_lex_expression_float():
|
||||||
""" Make sure expressions can produce floats """
|
""" Make sure expressions can produce floats """
|
||||||
objs = tokenize("(foo 2.)")
|
objs = tokenize("(foo 2.)")
|
||||||
|
@ -12,6 +12,11 @@
|
|||||||
(assert (isinstance sys.argv list)))
|
(assert (isinstance sys.argv list)))
|
||||||
|
|
||||||
|
|
||||||
|
(defn test-fractions []
|
||||||
|
"NATIVE: test fractions"
|
||||||
|
(assert (= 1/2 (fraction 1 2))))
|
||||||
|
|
||||||
|
|
||||||
(defn test-lists []
|
(defn test-lists []
|
||||||
"NATIVE: test lists work right"
|
"NATIVE: test lists work right"
|
||||||
(assert (= [1 2 3 4] (+ [1 2] [3 4]))))
|
(assert (= [1 2 3 4] (+ [1 2] [3 4]))))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user