Skip to content

Instantly share code, notes, and snippets.

View thezerobit's full-sized avatar

thezerobit thezerobit

  • Portland, OR, USA
View GitHub Profile
@thezerobit
thezerobit / nothing.py
Created July 3, 2014 16:43
some python
def values_in(keys, dict_):
Nothing = object()
vals = (dict_.get(key, Nothing) for key in keys)
return [x for x in vals if x is not Nothing]
@thezerobit
thezerobit / yield_from.boo
Created May 30, 2014 15:11
yield_from macro for Boo
# Boo macro for delegating to subgenerators ala PEP 380
macro yield_from:
yield [|
for _x in $(yield_from.Arguments[0]): yield _x
|]
def myrange():
yield_from range(99)
@thezerobit
thezerobit / prolog peano
Created January 5, 2014 20:54
Peano relation in Prolog using the CLP(FD) that ships with SWI-Prolog.
:- use_module(library(clpfd)).
peano(N,z) :- N #= 0.
peano(N,s(PDec)) :-
N #> 0,
N - 1 #= NDec,
peano(NDec, PDec).
% ?- peano(4, Q).
% Q = s(s(s(s(z)))) ;
@thezerobit
thezerobit / knight.pl
Created September 12, 2013 13:22
knights moves in Prolog
% example: move([0,0],[X,Y],7,7).
% because this is not right: https://github.com/clojure/core.logic/wiki/Translations-from-prolog
move([X, Y], [A, B], Xmax, Ymax) :-
member([Dx,Dy],[[1,2],[2,1],[2,-1],[1,-2],[-1,-2],[-2,-1],[-2,1],[-1,2]]),
A is X + Dx,
B is Y + Dy,
between(0, Xmax, A),
between(0, Ymax, B).
@thezerobit
thezerobit / static_vs_dynamic
Created July 19, 2013 22:12
dynamic typing > static typing ?
Is a dynamically typed language w/ optional type annotations and type-inferring compiler strictly better than a statically typed language with type inference?
0. Both languages have "strong" typing in the sense that values have definite types.
1. Any expression that would trigger an obvious type error (eg. calling string-concatenation function with 2 numeric constants) in compilation in the static lang, would trigger a similar warning in the dynamic language. The dynamic language would allow the program to actually compile and run and (presumably) fail at runtime, but would give you the option to fail compilation on these types of warnings (--fail-on-type-error).
2. Any expression that cannot be properly type inferred would fail compilation for the statically typed language but would succeed (with a lesser warning) for the dynamic language with the option to fail compilation for this type of warning, also (--fail-on-unknown-type).
3. For identical code, there is no reason that the dynamically typed langua
@thezerobit
thezerobit / async3.lisp
Created July 13, 2013 19:07
A simple example of setting up 10 green threads with infinite loops that wait and do work periodically using green-threads and cl-async.
(ql:quickload "cl-async")
(ql:quickload "green-threads")
(cl-cont:defun/cc my-delay (time)
(cl-cont:let/cc continuation
(as:delay continuation :time time)))
(as:start-event-loop
(lambda ()
(gt:with-green-thread
@thezerobit
thezerobit / python_vs_lisp
Created July 2, 2013 16:12
This is an example of how Python's features get misused in ways that hint at symbolic programming in Lisp.
# In Python it is common (though probably inadvisable) to
# use Exceptions for flow control in situations that are
# really not that exceptional. The Exception classes
# function as symbols for what is essentially symbolic
# programming using the exception handling machinery
# for the actual flow control.
class ValidationError(Exception):
pass
@thezerobit
thezerobit / async2.lisp
Created November 6, 2012 22:54
More complex green-threads / async example.
(ql:quickload "green-threads")
(ql:quickload "cl-async")
(defun f-http-client (url &rest rest)
(let ((http-future (green-threads:make-future)))
(apply #'cl-async:http-client
url
#'(lambda (status headers body)
(green-threads:complete-future
http-future status headers body nil))
@thezerobit
thezerobit / async.lisp
Created November 6, 2012 21:22
Use green-threads to build a "blocking" interface for an asynchronous library.
;; you'll need to clone green-threads and cl-async into ~/quicklisp/local-projects
(ql:quickload "green-threads")
(ql:quickload "cl-async")
(defun f-http-client (url &rest rest)
(let ((http-future (green-threads:make-future)))
(apply #'cl-async:http-client
url
#'(lambda (status headers body)
(green-threads:complete-future