#| #| #| #| #|
#| #|
import re | |
import collections | |
Failure = collections.namedtuple("Failure", ["unparsed"]) | |
def failure(unparsed=None): | |
return Failure(unparsed) | |
def isfailure(val): | |
return isinstance(val, Failure) |
(defmacro list-comp (bindings body) | |
(cond | |
((= 0 (length bindings)) | |
`(list ,body)) | |
((eq :when (caar bindings)) | |
`(if ,(cadar bindings) | |
(list-comp ,(cdr bindings) ,body))) | |
(t `(apply #'append (mapcar (lambda (,(caar bindings)) | |
(list-comp ,(cdr bindings) ,body)) | |
,(cadar bindings)))))) |
NUMS = ['10', '9', '8', '7', '6', '5', '4', '3', '2', '1'] | |
OPS = ['', '+', '-', '*', '/'] | |
def interpositions(seq, divs): | |
if len(seq) == 1: | |
return [seq] | |
return ([seq[0], div] + interposed for interposed in interpositions(seq[1:], divs) | |
for div in divs) | |
print([''.join(interposed) for interposed in interpositions(NUMS, OPS) if eval(''.join(interposed)) == 2013]) |
julia> function transform(ex) | |
:(println("transformed"); $(esc(ex))) | |
end | |
# methods for generic function transform | |
transform(ex) at none:2 | |
julia> macro t(ex) | |
transform(ex) | |
end |
# Using @async | |
immutable Continuation | |
args | |
end | |
cont(args...) = Continuation(args) | |
immutable Actor | |
ref::RemoteRef | |
mailbox::RemoteRef |
# time | |
# ==== | |
macro time(ex) | |
quote | |
local t0 = time_ns() | |
local val = $(esc(ex)) | |
local t1 = time_ns() | |
println("elapsed time: ", (t1-t0)/1e9, " seconds") | |
val |
remove_quote_block(val) = val | |
remove_quote_block(ex::Expr) = | |
ex.head == :block && length(ex.args) == 2 ? ex.args[2] : ex | |
remove_quote_block(exs...) = map(remove_quote_block, exs) | |
function syntax_bindings(ex, template::Symbol, collected::Dict) | |
s = string(template) | |
if s[1] == '_' && s[end] == '_' | |
collected[template] = ex |
module Unify | |
using FunctionalCollections | |
export unify, extended, lvar, Anything | |
const _LogicVarKey = 0x1f75f6e80ac3828f | |
immutable LogicVar | |
name |