PasteRack.org
Paste # 2039
2015-11-30 04:37:16

Fork as a new paste.

Paste viewed 167 times.


Embed:

Draft of new monadish syntax for Heresy

#lang s-exp "../private/base.rkt"

(import "./things.rkt")
(provide (all-defined-out))

; (:> *initial-value* *fn1* ...)
; The forward pipe operator. Given an initial value and a list of one-argument functions
; applys functions in order from left to right and returns the result
(def fn :> (initial-value . fns)
  (for (f in fns with initial-value)
    (carry (f cry))))

; State
; A handy empty object for use as a State store
(describe State (new (fn () State)))

; (:= *var* *value*)
; State -> State
; Binds a new value to var in State. Can overwrite previous values
(def macro := (name value)
  (fn (s) (thing extends s (name value))))

; (:_ *fn* *args* ...)
; State -> State
; Performs the function, but ignores the return value and instead passes along State.
; Useful for functions called only for their side effects
(def macro :_ (f . args)
  (fn (s)
    (f . args)
    s))

; (:^ (*names* ...) *fn* *args* ...)
; State -> State
; Lifts the names out of State and binds them for the scope of fn, executes fn using their values
; then returns State
(def macro :^ ((name ...) body ...)
  (fn (s)
    (let ([name (s (quote name))] ...)
      body ...
      s)))

; (:! *var* (*names* ...) *value*)
; State -> State
; Essentially :^ + :=. Binds a new name with value to State, while allowing the already bound values in (names ...)
; to be used in the value clause
(def macro :! (var (name ...) value)
  (fn (s)
    (let ([name (s (quote name))] ...)
      (thing extends s (var value)))))

; (return *value*)
; State -> value
; Returns the given value from State
; Always use last
(def macro return (name)
  (fn (s) (s (quote name))))

; (do> *actions* ...)
; The monadish do notation. Defines a State object, then performs actions with it
; see subcomments for available actions
(def fn do> fns
  (apply :> (join State fns)))