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?