Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
Horse Clocks
Dec 14, 2004


Barnyard Protein posted:

Does anyone know of a racket function/macro that does something like these clojure operators?

http://clojuredocs.org/clojure.core/-%3E
http://clojuredocs.org/clojure.core/-%3E%3E

The clojure docs use the word "thread" to describe the behavior, which is a good word to use except for the fact that there is another more common thing that uses that same word so its hard to search for.

Out of boredom I took a stab at implementing thread-first as a function. I got close, but don't know much about rackets quoting/unquoting, so the syntax is dumb.

I also don't have racket installed, so I'm using try-racket.org and I want to stab my hands to death.

Lisp code:
(define (thread-first value functions)
  (if (eq? (length functions) 0)
    value
    (let* ([head (car functions)]
           [tail (cdr functions)]
           [func (car head)]
           [args (cdr head)])
      (thread-first 
        (apply func value args) tail))))

(thread-first 0 (list (list + 1)
                      (list * 2)
                      (list / 4)))
1/2

Adbot
ADBOT LOVES YOU

  • Locked thread