Skip to content

Instantly share code, notes, and snippets.

@tuturto
tuturto / gist:5c9d8fa6e01f0fd37d91deb5548bc5c4
Created February 7, 2017 04:34
on page 34 of Evidence-Based programming language design
x <- rand(25);
y <- rand(25);
comment set x and y to random integers in [1, 25];
write What is <(x)> + <(y)>;
[accept reply; if reply
| = x+y : write Very good; correct_count <- correct_count + 1;
| = x*y : write Add, don’t multiply; again;
| > x+y+10 : write No, that’s more than 10 too much; again;
| else : write No, try again; again;
];
(defmain [&rest args]
(do-something-with args)
None)
;;;
;;; how to tokenize an arbitrary string and evaluate it in local context (quick and dirty)
;;; you can replace call to locals with a dictionary that specifies variables (as strings)
;;; and their values.
;;;
from hy.lex import tokenize
from hy.importer import hy_eval
a = 10
=> (defn foo [a b]
... (if (< a 1)
... b
... (foo (dec a) (inc b))))
=> (foo 100 1)
101
=> (require hy.contrib.profile)
=> (macroexpand '(-> "http://httpbin.org/ip" requests.get (fn [x] (getattr x "text"))))
('fn' ('requests.get' 'http://httpbin.org/ip') ['x'] ('getattr' 'x' 'text'))
=> (macroexpand '(-> "http://httpbin.org/ip" requests.get ((fn [x] (getattr x "text")))))
(('fn' ['x'] ('getattr' 'x' 'text')) ('requests.get' 'http://httpbin.org/ip'))
---
=> (defn foo [x] (getattr x "text"))
=> (defn n! [n]
... (defn+ counter [n acc]
... (if (!= n 0)
... (counter (- n 1) (* n acc))
... acc))
... (counter n 1))
=> (n! 5000)
42285779266055435222010642002335844053907866746266467488497824021813580527081082006908990478717063875370847466573006854458784860666838127363372108937727876312793903630584621606439044789869822398719297088962116126529683217755003992421968370314690726447287878979040475488416221522667192841096923691044956597173635294840022384038112064482023085767110450230617489475542830976178172404080532480992780932878405548619936454829121187625824880218917397790005021321259804363924462646077051135884659510867547058583392465522558903547443598834738317898803463300845863151020909150993565382001093304796574255674193091705517280520023607508599119763522875590790204336974312350691683121192449597155626740752146219898623308862599830285986485757874944596311528697088671004626842364817898990545469086139161321834417414880718623444811483120949036119654687276775561788682872026910481409245641034
=> (setv list-1 ["one" "two" "three"])
=> (setv list-2 ["one" "three"])
=>
=> (run* [q]
... (fresh [x y]
... (membero x list-1)
... (conde [(membero x list-2) (unifyo y 2)]
... [#ss (unifyo y 1)])
... (conde [(unifyo y 2) (unifyo q x)])))
['one', 'three']
@tuturto
tuturto / hypo.hy
Last active February 12, 2016 15:53
Hy, Hypothesis and Hamcrest in action
(import [hypothesis [given example]]
[hypothesis.strategies [text lists integers]]
[hypo.sut [decode encode]])
(import [hamcrest [assert-that is- equal-to has-length less-than
less-than-or-equal-to greater-than]])
(with-decorator (given (text))
(defn decode-inverts-encode [s]
(assert (= (-> (encode s)
(defn foo []
(if check
return-value)
(extra-thingy-1 param1 param2)
(extra-thingy-2 param1 param2))
;; extra-thingies will always be evaluated, regardless of check
;; return value of the function will be extra-thingy-2
(defn foo []
@tuturto
tuturto / demo.hy
Created November 8, 2015 08:54
solver - first steps
hy 0.11.0 using CPython(default) 3.4.3 on Linux
=> (import [pyherc.solver [Variable are-equal! solve]])
=> (setv var1 (Variable 1 2 3 4 5))
=> (setv var2 (Variable 3 4 5 6 7))
=> (setv var3 (Variable 5 6 7 8 9))
=> (are-equal! var1 var2)
=> (are-equal! var2 var3)
=> (solve var1 var2 var3)
=> [var1 var2 var3]
[{5}, {5}, {5}]