Skip to content

Commit

Permalink
Add &# /# /XY#
Browse files Browse the repository at this point in the history
And remove functool imports from prelude.
  • Loading branch information
gilch committed Aug 25, 2024
1 parent 984fd5a commit 206f12e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/lissp_whirlwind_tour.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1864,7 +1864,7 @@ Lissp Whirlwind Tour

;;; The prelude copies _macro_ from hissp._macro_ like the REPL, defines
;;; some Python interop helper functions, and imports Python's standard-library
;;; functional utilities.
;;; functional programming utilities from operator and itertools.

(help _macro_.prelude)

Expand Down
3 changes: 1 addition & 2 deletions docs/macro_tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,7 @@ And push it to the REPL as well:
#> hissp..prelude#:
>>> # hissp.macros.._macro_.prelude
... __import__('builtins').exec(
... ('from functools import partial,reduce\n'
... 'from itertools import *;from operator import *\n'
... ('from itertools import *;from operator import *\n'
... 'def engarde(xs,h,f,/,*a,**kw):\n'
... ' try:return f(*a,**kw)\n'
... ' except xs as e:return h(e)\n'
Expand Down
44 changes: 38 additions & 6 deletions src/hissp/macros.lissp
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,10 @@ Hidden doctest adds bundled macros for REPL-consistent behavior.
"``chain#`` Abbreviation for `itertools.chain.from_iterable`."
`(i#chain.from_iterable ,xss))

(defmacro &\# e
"``&#`` Abbreviation for `functools.partial`."
`(functools..partial ,@e))

(defmacro get\# e
<#;``get#`` 'itemgetter-' Makes an `operator.itemgetter` function from ``e``.
;;
Expand Down Expand Up @@ -2154,6 +2158,38 @@ Hidden doctest adds bundled macros for REPL-consistent behavior.
`(lambda (: :* $#xs)
(,f $#xs))))

(defmacro /\# f
"Wrap a binary function as a variadic via reduce."
`(lambda (: :* $#xs)
(functools..reduce ,f $#xs)))

(defmacro /XY\# e
<#;``/XY#`` Anaphoric. Make ``e`` a reducer with parameters X Y.
;;
;; The resulting function is a partial application of
;; `functools.reduce`.
;;
;; .. code-block:: REPL
;;
;; #> /XY#(op#add Y X)
;; >>> __import__('functools').partial(
;; ... __import__('functools').reduce,
;; ... (lambda X, Y:
;; ... __import__('operator').add(
;; ... Y,
;; ... X)
;; ... ))
;; functools.partial(<built-in function reduce>, <function <lambda> at 0x...>)
;;
;; #> (_ 'ABCD)
;; >>> _(
;; ... 'ABCD')
;; 'DCBA'
;;
;; See also: `XY#<XYQzHASH_>`, `/#<QzSOL_QzHASH_>`.
;;
`(functools..partial functools..reduce (lambda ,'XY ,e)))

;;;; Collection

(defmacro @ (: :* xs)
Expand Down Expand Up @@ -2275,10 +2311,8 @@ Hidden doctest adds bundled macros for REPL-consistent behavior.
;;
;; The prelude has several effects:
;;
;; * Imports `functools.partial` and `functools.reduce`.
;; Star imports from `itertools` and `operator`::
;; * Star imports from `itertools` and `operator`::
;;
;; from functools import partial,reduce
;; from itertools import *;from operator import *
;;
;; .. _engarde:
Expand Down Expand Up @@ -2352,8 +2386,7 @@ Hidden doctest adds bundled macros for REPL-consistent behavior.
;; #> (prelude)
;; >>> # prelude
;; ... __import__('builtins').exec(
;; ... ('from functools import partial,reduce\n'
;; ... 'from itertools import *;from operator import *\n'
;; ... ('from itertools import *;from operator import *\n'
;; ... 'def engarde(xs,h,f,/,*a,**kw):\n'
;; ... ' try:return f(*a,**kw)\n'
;; ... ' except xs as e:return h(e)\n'
Expand Down Expand Up @@ -2820,7 +2853,6 @@ Hidden doctest adds bundled macros for REPL-consistent behavior.
;; Exception
;;
`(exec ',"\
from functools import partial,reduce
from itertools import *;from operator import *
def engarde(xs,h,f,/,*a,**kw):
try:return f(*a,**kw)
Expand Down

0 comments on commit 206f12e

Please sign in to comment.