2 from itertools
import chain
4 import mal_types
as types
5 from mal_types
import List
, Vector
9 def throw(exc
): raise Exception(exc
)
14 return " ".join(map(lambda exp
: printer
._pr
_str
(exp
, True), args
))
17 return "".join(map(lambda exp
: printer
._pr
_str
(exp
, False), args
))
20 print " ".join(map(lambda exp
: printer
._pr
_str
(exp
, True), args
))
24 line
= " ".join(map(lambda exp
: printer
._pr
_str
(exp
, False), args
))
25 print line
.replace('\\n', '\n')
30 def assoc(src_hm
, *key_vals
):
31 hm
= copy
.copy(src_hm
)
32 for i
in range(0,len(key_vals
),2): hm
[key_vals
[i
]] = key_vals
[i
+1]
35 def dissoc(src_hm
, *keys
):
36 hm
= copy
.copy(src_hm
)
38 if key
in hm
: del hm
[key
]
47 def contains_Q(hm
, key
): return key
in hm
49 def keys(hm
): return types
._list
(*hm
.keys())
51 def vals(hm
): return types
._list
(*hm
.values())
55 def coll_Q(coll
): return sequential_Q(coll
) or hash_map_Q(coll
)
57 def cons(x
, seq
): return List([x
]) + List(seq
)
59 def concat(*lsts
): return List(chain(*lsts
))
61 def nth(lst
, idx
): return lst
[idx
]
63 def first(lst
): return lst
[0]
65 def rest(lst
): return List(lst
[1:])
67 def empty_Q(lst
): return len(lst
) == 0
69 def count(lst
): return len(lst
)
73 if types
._list
_Q
(lst
):
74 new_lst
= List(list(reversed(list(args
))) + lst
)
76 new_lst
= Vector(lst
+ list(args
))
77 if hasattr(lst
, "__meta__"):
78 new_lst
.__meta
__ = lst
.__meta
__
81 def apply(f
, *args
): return f(*(list(args
[0:-1])+args
[-1]))
83 def mapf(f
, lst
): return List(map(f
, lst
))
87 def with_meta(obj
, meta
):
88 if type(obj
) == type(lambda x
:x
):
89 new_obj
= obj
.__copy
__()
91 new_obj
= copy
.copy(obj
)
92 new_obj
.__meta
__ = meta
96 if hasattr(obj
, "__meta__"): return obj
.__meta
__
101 def deref(atm
): return atm
.val
102 def reset_BANG(atm
,val
):
105 def swap_BANG(atm
,f
,*args
):
106 atm
.val
= f(atm
.val
,*args
)
113 'nil?': types
._nil
_Q
,
114 'true?': types
._true
_Q
,
115 'false?': types
._false
_Q
,
116 'symbol': types
._symbol
,
117 'symbol?': types
._symbol
_Q
,
122 '<': lambda a
,b
: a
<b
,
123 '<=': lambda a
,b
: a
<=b
,
124 '>': lambda a
,b
: a
>b
,
125 '>=': lambda a
,b
: a
>=b
,
126 '+': lambda a
,b
: a
+b
,
127 '-': lambda a
,b
: a
-b
,
128 '*': lambda a
,b
: a
*b
,
129 '/': lambda a
,b
: a
/b
,
132 'list?': types
._list
_Q
,
133 'vector': types
._vector
,
134 'vector?': types
._vector
_Q
,
135 'hash-map': types
._hash
_map
,
136 'map?': types
._hash
_map
_Q
,
140 'contains?': contains_Q
,
144 'sequential?': types
._sequential
_Q
,
156 'with-meta': with_meta
,
159 'atom?': types
._atom
_Q
,
161 'reset!': reset_BANG
,