@@ -267,22 +267,11 @@ def flatten_in(iterable, pred=None):
267267def test ():
268268 from operator import add , mul , itemgetter
269269 from functools import partial
270+ from unpythonic .fun import curry , composer , composerc , composel , to1st , rotate , identity
271+ from unpythonic .llist import cons , nil , ll
270272
271- import unpythonic .fun
272- curry = unpythonic .fun .curry
273- composer = unpythonic .fun .composer
274- composerc = unpythonic .fun .composerc
275- composel = unpythonic .fun .composel
276- to1st = unpythonic .fun .to1st
277- rotate = unpythonic .fun .rotate
278- identity = unpythonic .fun .identity
279-
280- # just a testing hack; for a "real" cons, see unpythonic.llist.cons
281- nil = ()
282- def cons (x , l ): # elt, acc
283- return (x ,) + l
284- assert foldl (cons , nil , (1 , 2 , 3 )) == (3 , 2 , 1 )
285- assert foldr (cons , nil , (1 , 2 , 3 )) == (1 , 2 , 3 )
273+ assert foldl (cons , nil , ll (1 , 2 , 3 )) == ll (3 , 2 , 1 )
274+ assert foldr (cons , nil , ll (1 , 2 , 3 )) == ll (1 , 2 , 3 )
286275
287276 assert reducel (add , (1 , 2 , 3 )) == 6
288277 assert reducer (add , (1 , 2 , 3 )) == 6
@@ -296,19 +285,19 @@ def mymap_one(f, sequence):
296285 f_then_cons = composer (cons , to1st (f )) # args: elt, acc
297286 return foldr (f_then_cons , nil , sequence )
298287 double = lambda x : 2 * x
299- assert mymap_one (double , (1 , 2 , 3 )) == (2 , 4 , 6 )
288+ assert mymap_one (double , ll (1 , 2 , 3 )) == ll (2 , 4 , 6 )
300289 def mymap_one2 (f , sequence ):
301290 f_then_cons = composel (to1st (f ), cons ) # args: elt, acc
302291 return foldr (f_then_cons , nil , sequence )
303- assert mymap_one2 (double , (1 , 2 , 3 )) == (2 , 4 , 6 )
292+ assert mymap_one2 (double , ll (1 , 2 , 3 )) == ll (2 , 4 , 6 )
304293
305294 # point-free-ish style
306295 mymap_one3 = lambda f : partial (foldr , composer (cons , to1st (f )), nil )
307296 doubler = mymap_one3 (double )
308- assert doubler ((1 , 2 , 3 )) == (2 , 4 , 6 )
297+ assert doubler (ll (1 , 2 , 3 )) == ll (2 , 4 , 6 )
309298
310299 try :
311- doubler ((1 , 2 , 3 ), (4 , 5 , 6 ))
300+ doubler (ll (1 , 2 , 3 ), ll (4 , 5 , 6 ))
312301 except TypeError :
313302 pass
314303 else :
@@ -317,25 +306,25 @@ def mymap_one2(f, sequence):
317306 # minimum arity of fold functions is 3, to allow use with curry:
318307 mymap_one4 = lambda f : curry (foldr , composer (cons , to1st (f )), nil )
319308 doubler = mymap_one4 (double )
320- assert doubler ((1 , 2 , 3 )) == (2 , 4 , 6 )
309+ assert doubler (ll (1 , 2 , 3 )) == ll (2 , 4 , 6 )
321310
322311 # curry supports passing through on the right any args over the max arity.
323312 assert curry (double , 2 , "foo" ) == (4 , "foo" ) # arity of double is 1
324313
325314 # In passthrough, if an intermediate result is a callable,
326315 # it is invoked on the remaining positional args:
327- assert curry (mymap_one4 , double , (1 , 2 , 3 )) == (2 , 4 , 6 )
316+ assert curry (mymap_one4 , double , ll (1 , 2 , 3 )) == ll (2 , 4 , 6 )
328317
329318 # This also works; curried f takes one argument and the second one is passed
330319 # through on the right; this two-tuple then ends up as the arguments to cons.
331320 mymap_one5 = lambda f : curry (foldr , composer (cons , curry (f )), nil )
332- assert curry (mymap_one5 , double , (1 , 2 , 3 )) == (2 , 4 , 6 )
321+ assert curry (mymap_one5 , double , ll (1 , 2 , 3 )) == ll (2 , 4 , 6 )
333322
334323 # Finally, we can drop the inner curry by using a currying compose.
335324 # This is as close to "(define (map f) (foldr (compose cons f) empty)"
336325 # (#lang spicy) as we're gonna get in Python.
337326 mymap = lambda f : curry (foldr , composerc (cons , f ), nil )
338- assert curry (mymap , double , (1 , 2 , 3 )) == (2 , 4 , 6 )
327+ assert curry (mymap , double , ll (1 , 2 , 3 )) == ll (2 , 4 , 6 )
339328
340329 # The currying has actually made it not just map one, but general map that
341330 # accepts multiple input sequences.
@@ -344,21 +333,23 @@ def mymap_one2(f, sequence):
344333 # argument, is passed through on the right. The output from the processing
345334 # function - one new item - and acc then become a two-tuple, which gets
346335 # passed into cons.
347- assert curry (mymap , lambda x , y : x + y , (1 , 2 , 3 ), (2 , 4 , 6 )) == (3 , 6 , 9 )
336+ add = lambda x , y : x + y
337+ assert curry (mymap , add , ll (1 , 2 , 3 ), ll (2 , 4 , 6 )) == ll (3 , 6 , 9 )
348338
349339 reverse_one = curry (foldl , cons , nil )
350- assert reverse_one ((1 , 2 , 3 )) == (3 , 2 , 1 )
340+ assert reverse_one (ll (1 , 2 , 3 )) == ll (3 , 2 , 1 )
351341
352- append_two = lambda a , b : foldr (cons , b , a )
353- assert append_two ((1 , 2 , 3 ), (4 , 5 , 6 )) == (1 , 2 , 3 , 4 , 5 , 6 )
342+ append_two = lambda a , b : foldr (cons , b , a ) # a, b: linked lists
343+ assert append_two (ll (1 , 2 , 3 ), ll (4 , 5 , 6 )) == ll (1 , 2 , 3 , 4 , 5 , 6 )
354344
345+ # see upythonic.llist.lappend
355346 append_many = lambda * lsts : foldr (append_two , nil , lsts )
356- assert append_many ((1 , 2 ), (3 , 4 ), (5 , 6 )) == (1 , 2 , 3 , 4 , 5 , 6 )
347+ assert append_many (ll (1 , 2 ), ll (3 , 4 ), ll (5 , 6 )) == ll (1 , 2 , 3 , 4 , 5 , 6 )
357348
358349 mysum = curry (foldl , add , 0 )
359350 myprod = curry (foldl , mul , 1 )
360- a = (1 , 2 )
361- b = (3 , 4 )
351+ a = ll (1 , 2 )
352+ b = ll (3 , 4 )
362353 assert mysum (append_two (a , b )) == 10
363354 assert myprod (b ) == 12
364355
0 commit comments