filter expressions

22 views
Skip to first unread message

Steve

unread,
Jul 6, 2012, 10:27:06 AM7/6/12
to loop...@googlegroups.com
Hi again

I was wondering whether there is any reason one can not use a function as a filter.
E.g.
i for i in [1..10] if isPrime(i)

I managed to write a filter pattern instead, though it not half as elegant as the above would be.

BTW I came across the following issue:
filter(criteria, ls) =>
    *, [] : []
    *, [x:xs] | criteria.@call(x) : [x] + filter(criteria,xs)
                | else : filter(criteria, xs)


main ->
    print(filter(divBy2, [1..20]))
    where
      divBy2: @(n) -> {(n % 2) == 0}

This causes an infinite loop and eventually a stack overflow in the code generator:
Exception in thread "main" java.lang.StackOverflowError
at java.util.regex.Pattern.append(Unknown Source)
at java.util.regex.Pattern.atom(Unknown Source)
at java.util.regex.Pattern.sequence(Unknown Source)
at java.util.regex.Pattern.expr(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.util.regex.Pattern.<init>(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.lang.String.replaceFirst(Unknown Source)
at loop.AsmCodeEmitter.normalizeMethodName(AsmCodeEmitter.java:720)
at loop.AsmCodeEmitter.access$500(AsmCodeEmitter.java:68)
at loop.AsmCodeEmitter$18.emitCode(AsmCodeEmitter.java:971)
at loop.AsmCodeEmitter.emit(AsmCodeEmitter.java:235)
at loop.AsmCodeEmitter$6.emitCode(AsmCodeEmitter.java:649)
 at loop.AsmCodeEmitter.emit(AsmCodeEmitter.java:235)
at loop.AsmCodeEmitter$18.emitCode(AsmCodeEmitter.java:1001)
at loop.AsmCodeEmitter.emit(AsmCodeEmitter.java:235)
at loop.AsmCodeEmitter$6.emitCode(AsmCodeEmitter.java:649)
etc.

Where as:

main ->
    print(filter( @(n) -> {(n % 2) == 0} , [1..20]))
works great. So does:

main ->
    print(filter(divBy2, [1..20]))
    where
     divBy2(n) -> 
        (n % 2) == 0

So it seems to do with the the closure in the where statement?

Dhanji R. Prasanna

unread,
Jul 15, 2012, 7:18:32 PM7/15/12
to loop...@googlegroups.com
Hmm very strange, no there is no reason this should happen. It's probably an emitting error. If you can file a bug on github I'll track it and try to fix soon. In the meantime your solution seems reasonable.
Reply all
Reply to author
Forward
0 new messages