Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: The LOOP macro

218 views
Skip to first unread message

WJ

unread,
Mar 13, 2012, 11:07:11 PM3/13/12
to
Peter Seibel wrote:

> (loop for w across widgets count (good-widget-p w))

Clojure:

(count (keep good-widget? widgets))

SecretTheatre

unread,
Mar 14, 2012, 1:16:59 AM3/14/12
to
Great! Clojure wins for you. Now please FUCK OFF.

namekuseijin

unread,
Mar 13, 2012, 11:38:27 PM3/13/12
to
(length (filter good-widget? widgets))

to keep with idiomatic FP rather than inventing new names. Anyway, definitely better than loop craze.

BTW, how you doing with your crack smoking?

Marco Antoniotti

unread,
Mar 14, 2012, 5:32:05 AM3/14/12
to
COBOL-Lisp> (count-if good-widget? widgets) ; Where 'widgets' is a proper const-time-access vector.
42

In COBOL-Lisp we mean business.

Orpheus, here I come.

Cheers
--
MA

Leandro Rios

unread,
Mar 14, 2012, 11:20:13 AM3/14/12
to
El 14/03/12 00:07, WJ escribió:
http://clojuredocs.org/clojure_core/1.2.0/clojure.core/keep says:

********************************************************************
keep
clojure.core

(keep f coll)

Returns a lazy sequence of the non-nil results of (f item). Note,
this means false return values will be included. f must be free of
side-effects.
********************************************************************

What does "good-widget?" return? I mean, Clojure is a language where
false<>nil, so good-widget? should return true or false (as all of the
functions ending in "?" do in Clojure). Look at the very first example
in Clojure docs:

user=> (keep even? (range 1 10))
(false true false true false true false true false)

Your code does not work. Shouldn't it be good that you learn how to
properly program in Clojure before trying to post code? That would make
the commissars happy.

Leandro

WJ

unread,
Mar 17, 2012, 10:40:39 PM3/17/12
to
Rob Warnock wrote:

> ;;; Like UTILS:JOIN-STRINGS, but specialized for quoting elements
> ;;; of SQL VALUES() subclause.
> (defun join-strings/sql-quoted (strings)
> (apply #'concatenate 'string
> (loop for tail on strings
> collect "'"
> collect (sql-escape-single-quotes (car tail))
> collect "'"
> when (cdr tail)
> collect ", ")))
>
> (defun sql-escape-single-quotes (string)
> (if (position #\' string) ; Need to do anything at all?
> (let ((escaped (loop for c across string
> collect c
> when (eql c #\')
> collect c)))
> (coerce escaped 'string))
> string))
>
> Usage:
>
> > (let ((x '("Here" "is" "Bob's" "house" "and" "Charles'")))
> (join-strings/sql-quoted x))
>
> "'Here', 'is', 'Bob''s', 'house', 'and', 'Charles'''"

(require 'clojure.string)

(clojure.string/join ", "
(map #(str "'" (.replace % "'" "''") "'")
'("Here" "is" "Bob's" "house" "and" "Charles'")))

==>
"'Here', 'is', 'Bob''s', 'house', 'and', 'Charles'''"

WJ

unread,
Mar 17, 2012, 10:56:09 PM3/17/12
to
WJ wrote:

> Rob Warnock wrote:
>
> > ;;; Like UTILS:JOIN-STRINGS, but specialized for quoting elements
> > ;;; of SQL VALUES() subclause.
> > (defun join-strings/sql-quoted (strings)
> > (apply #'concatenate 'string
> > (loop for tail on strings
> > collect "'"
> > collect (sql-escape-single-quotes (car tail))
> > collect "'"
> > when (cdr tail)
> > collect ", ")))
> >
> > (defun sql-escape-single-quotes (string)
> > (if (position #\' string) ; Need to do anything at all?
> > (let ((escaped (loop for c across string
> > collect c
> > when (eql c #\')
> > collect c)))
> > (coerce escaped 'string))
> > string))

That definitely is COBOL-Like.

WJ

unread,
Mar 17, 2012, 11:49:23 PM3/17/12
to
Peter Seibel wrote:

> (loop for x in things
> when (foo-p x) collect x into foos
> when (bar-p x) collect x into bars
> when (and (foo-p x) (bar-p x)) collect x into both
> finally (return (values foos bars both)))
>
> Or if the predicate functions FOO-P and BAR-P are sufficiently
> expensive that you don't want to compute more often than absolutely
> necessary:
>
> (loop for x in things
> for foo-p = (foo-p x)
> for bar-p = (bar-p x)
> when foo-p collect x into foos
> when bar-p collect x into bars
> when (and foo-p bar-p) collect x into both
> finally (return (values foos bars both)))

Clojure:

Assuming no duplicates and using sets:

(def intersect clojure.set/intersection)

(with [odds negs]
(doseq [x (range -4 4)]
(when (odd? x) (collecting-set x odds))
(when (neg? x) (collecting-set x negs)))
(list @odds @negs (intersect @odds @negs)))

==> (#{1 3 -3 -1} #{-4 -3 -2 -1} #{-3 -1})


Given:

(defmacro with [vars & body]
`(with-local-vars [~@(flatten (map #(list % nil) vars))]
~@body))

(defn collecting-set [x var]
(if (nil? @var) (var-set var #{}))
(var-set var (clojure.set/union @var #{x})))

WJ

unread,
Apr 23, 2012, 4:32:04 PM4/23/12
to
Racket:

(count good-widget? widgets)

WJ

unread,
May 21, 2012, 2:37:10 AM5/21/12
to
Kenny Tilton wrote:


> (loop for n below 3 do (print n))
>
> That ain't Lisp syntax. This is Lisp syntax;
>
> (do ((n 1 (1+ n)))
> ((> n 3))
> (print n))

Incorrect.

* (loop for n below 3 do (print n))

0
1
2
NIL
* (do ((n 1 (1+ n)))
((> n 3))
(print n))

1
2
3
NIL

WJ

unread,
Oct 19, 2012, 11:03:11 AM10/19/12
to
WJ wrote:

> Peter Seibel wrote:
>
> > (loop for x in things
> > when (foo-p x) collect x into foos
> > when (bar-p x) collect x into bars
> > when (and (foo-p x) (bar-p x)) collect x into both
> > finally (return (values foos bars both)))
> >
> > Or if the predicate functions FOO-P and BAR-P are sufficiently
> > expensive that you don't want to compute more often than absolutely
> > necessary:
> >
> > (loop for x in things
> > for foo-p = (foo-p x)
> > for bar-p = (bar-p x)
> > when foo-p collect x into foos
> > when bar-p collect x into bars
> > when (and foo-p bar-p) collect x into both
> > finally (return (values foos bars both)))

Racket:

(define things '(-3 -2 0 4 5 9))

(map (curryr filter things)
(list odd? positive? (lambda(x) (and (odd? x) (positive? x)))))

--> '((-3 5 9) (4 5 9) (5 9))

WJ

unread,
Nov 7, 2012, 2:15:31 PM11/7/12
to
WJ wrote:

> Peter Seibel wrote:
>
> > (loop for x in things
> > when (foo-p x) collect x into foos
> > when (bar-p x) collect x into bars
> > when (and (foo-p x) (bar-p x)) collect x into both
> > finally (return (values foos bars both)))
> >
> > Or if the predicate functions FOO-P and BAR-P are sufficiently
> > expensive that you don't want to compute more often than absolutely
> > necessary:
> >
> > (loop for x in things
> > for foo-p = (foo-p x)
> > for bar-p = (bar-p x)
> > when foo-p collect x into foos
> > when bar-p collect x into bars
> > when (and foo-p bar-p) collect x into both
> > finally (return (values foos bars both)))

Racket:


(with (odds negs both)
(for ([x things])
(define odd (odd? x))
(define neg (negative? x))
(when odd (push! x odds))
(when neg (push! x negs))
(and odd neg (push! x both)))
(values odds negs both))



Given:

(define-syntax-rule (push! val var)
(set! var (cons val var)))

(define-syntax with-helper
(syntax-rules ()
[(_ () (assignments ...) expr ...)
(let (assignments ...) expr ...)]
[(_ ((var val) more ...) (assignments ...) expr ...)
(with-helper (more ...) (assignments ... (var val)) expr ...)]
[(_ (var more ...) (assignments ...) expr ...)
(with-helper (more ...) (assignments ... (var '())) expr ...)]))

(define-syntax-rule (with (locals ...) expr ...)
(with-helper (locals ...) () expr ...))

WJ

unread,
Nov 7, 2012, 2:31:48 PM11/7/12
to
WJ wrote:

> (define-syntax with-helper
> (syntax-rules ()
> [(_ () (assignments ...) expr ...)
> (let (assignments ...) expr ...)]
> [(_ ((var val) more ...) (assignments ...) expr ...)
> (with-helper (more ...) (assignments ... (var val)) expr ...)]
> [(_ (var more ...) (assignments ...) expr ...)
> (with-helper (more ...) (assignments ... (var '())) expr ...)]))
>
> (define-syntax-rule (with (locals ...) expr ...)
> (with-helper (locals ...) () expr ...))

I'll bet that this would be much shorter using syntax-case.

WJ

unread,
Nov 7, 2012, 3:27:10 PM11/7/12
to
In COBOL-Like:

(let (odds negs both)
(dolist (x things)
(let ((odd (oddp x)) (neg (minusp x)))
(when odd (push x odds))
(when neg (push x negs))
(and odd neg (push x both))))
(values odds negs both))

WJ

unread,
Dec 7, 2012, 8:57:28 PM12/7/12
to
WJ wrote:

> Peter Seibel wrote:
>
> > (loop for x in things
> > when (foo-p x) collect x into foos
> > when (bar-p x) collect x into bars
> > when (and (foo-p x) (bar-p x)) collect x into both
> > finally (return (values foos bars both)))
> >
> > Or if the predicate functions FOO-P and BAR-P are sufficiently
> > expensive that you don't want to compute more often than absolutely
> > necessary:
> >
> > (loop for x in things
> > for foo-p = (foo-p x)
> > for bar-p = (bar-p x)
> > when foo-p collect x into foos
> > when bar-p collect x into bars
> > when (and foo-p bar-p) collect x into both
> > finally (return (values foos bars both)))

Wouldn't it be nice to eliminate the need for the temporary
variables foo-p and bar-p?

Racket:

(define things '(-3 -2 -1 0 1 2 3))

(immediate odds $collect odd?)
(immediate negs $collect negative?)
(immediate both $collect)
(for ([x things])
(if (odds x)
(and (negs x) (both x))
(negs x)))
(values odds negs both)

'(-3 -1 1 3)
'(-3 -2 -1)
'(-3 -1)


Given:

(define ($collect [filter-func #f])
(let ((accum '()))
(if filter-func
(case-lambda
[() (reverse accum)]
[(x) (and (filter-func x)
(begin (set! accum (cons x accum))
x))])
(case-lambda
[() (reverse accum)]
[(x) (set! accum (cons x accum))]))))

(define-syntax-rule (immediate name proc args ...)
(begin
(define closure (proc args ...))
(define-syntax name
(syntax-id-rules ()
;; Weird ellipsis because macro within macro.
[(name a (... ...)) (closure a (... ...))]
[name (closure)]))))

WJ

unread,
Dec 22, 2014, 2:44:32 AM12/22/14
to
WJ wrote:

> Peter Seibel wrote:
>
> > (loop for w across widgets count (good-widget-p w))


Gauche Scheme:

gosh> (count odd? (iota 22))
11

WJ

unread,
Dec 31, 2014, 5:08:17 PM12/31/14
to
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
WJ wrote:

> Peter Seibel wrote:
>
> > (loop for x in things
> > when (foo-p x) collect x into foos
> > when (bar-p x) collect x into bars
> > when (and (foo-p x) (bar-p x)) collect x into both
> > finally (return (values foos bars both)))
> >
> > Or if the predicate functions FOO-P and BAR-P are sufficiently
> > expensive that you don't want to compute more often than absolutely
> > necessary:
> >
> > (loop for x in things
> > for foo-p = (foo-p x)
> > for bar-p = (bar-p x)
> > when foo-p collect x into foos
> > when bar-p collect x into bars
> > when (and foo-p bar-p) collect x into both
> > finally (return (values foos bars both)))

Gauche Scheme:

(use srfi-1) ;; lset-intersection

(let ((evens ())
(threes ()))
(dolist (x (iota 20))
(when (even? x) (push! evens x))
(when (zero? (mod x 3)) (push! threes x)))
(values evens threes (lset-intersection equal? evens threes)))

===>
(18 16 14 12 10 8 6 4 2 0)
(18 15 12 9 6 3 0)
(18 12 6 0)

WJ

unread,
Dec 13, 2015, 10:02:06 PM12/13/15
to
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
WJ wrote:

> WJ wrote:
>
> > Peter Seibel wrote:
> >
> > > (loop for x in things
> > > when (foo-p x) collect x into foos
> > > when (bar-p x) collect x into bars
> > > when (and (foo-p x) (bar-p x)) collect x into both
> > > finally (return (values foos bars both)))
> > >
> > > Or if the predicate functions FOO-P and BAR-P are sufficiently
> > > expensive that you don't want to compute more often than absolutely
> > > necessary:
> > >
> > > (loop for x in things
> > > for foo-p = (foo-p x)
> > > for bar-p = (bar-p x)
> > > when foo-p collect x into foos
> > > when bar-p collect x into bars
> > > when (and foo-p bar-p) collect x into both
> > > finally (return (values foos bars both)))

MatzLisp (Ruby):

def foo? x; x.even?; end
def bar? x; 0 == x.modulo(3); end

foos,bars,both = [],[],[]
20.times{|x|
foo = foo? x
bar = bar? x
foos << x if foo
bars << x if bar
both << x if foo && bar }
[foos, bars, both]

===>
[[0, 2, 4, 6, 8, 10, 12, 14, 16, 18],
[0, 3, 6, 9, 12, 15, 18],
[0, 6, 12, 18]]

--
It is extremely gratifying to ... achieve membership in an elite that is going
to make history.... The only drawback is that the self-appointed shepherds are
likely to find eventually that they were only sheepdogs and herded the flock
toward a destination of which they knew nothing. --- R. P. Oliver

WJ

unread,
Jan 9, 2016, 5:08:50 PM1/9/16
to
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
MatzLisp (Ruby):

(0..22).count{|x| x.odd?}
==>11

--
77.6 percent of the country's rapists are identified as "foreigners".... And
even this likely understates the issue, since the Swedish government---in an
effort to obscure the problem---records second-generation Muslim perpetrators
simply as "Swedes." http://redicecreations.com/article.php?id=34829

Jon Atack

unread,
Jan 9, 2016, 6:52:25 PM1/9/16
to
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
On Saturday, January 9, 2016 at 11:08:50 PM UTC+1, WJ wrote:
> > WJ wrote:
> >
> > > Peter Seibel wrote:
> > >
> > > > (loop for w across widgets count (good-widget-p w))
> >
> >
> > Gauche Scheme:
> >
> > gosh> (count odd? (iota 22))
> > 11
>
> MatzLisp (Ruby):
>
> (0..22).count{|x| x.odd?}
> ==>11

(0..22).count(&:odd?)

Marco Antoniotti

unread,
Jan 10, 2016, 4:54:53 AM1/10/16
to
(count-if 'oddp (iota 22))

Cheers
--
MA
Message has been deleted

WJ

unread,
Apr 2, 2016, 6:37:46 PM4/2/16
to
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
WJ wrote:

> WJ wrote:
>
> > WJ wrote:
> >
> > > Peter Seibel wrote:
> > >
> > > > (loop for w across widgets count (good-widget-p w))
> >
> >
> > Gauche Scheme:
> >
> > gosh> (count odd? (iota 22))
> > 11
>
> MatzLisp (Ruby):
>
> (0..22).count{|x| x.odd?}
> ==>11

Shorter:

(0..22).count &:odd?
==>11

--
[M]ore than 30 blacks brutally beat a white couple as another 70 blacks watched
and cheered them on.... [T]he main newspaper in town ... could not even be
bothered to report on the attack of its own employees for two weeks---and even
then it was only as an opinion piece written by a friend of the couple.
americanfreepress.net/media-covers-up-black-hate-crimes-against-whites
0 new messages