Skip to content

Instantly share code, notes, and snippets.

@lucaswerkmeister
Created March 24, 2015 22:19
Show Gist options
  • Save lucaswerkmeister/84fb2b308421711e2e95 to your computer and use it in GitHub Desktop.
Save lucaswerkmeister/84fb2b308421711e2e95 to your computer and use it in GitHub Desktop.
Additional info for ceylon/ceylon-compiler#2077
Result memoize<Result>
(Result() f, variable Result? memo=null)()
given Result satisfies Object
=> memo = memo else f();
T printit<T>(T t) => let (_ = print("evaluated ‘`` t?.string else "<null>" ``’")) t;
shared void run() {
print(1);
value m = memoize(() => printit("value"));
print(2);
print(m());
print(3);
print(m());
print(4);
}
@lucaswerkmeister
Copy link
Author

Output:

1
2
evaluated ‘value’
value
3
value
4

@quintesse
Copy link

final class memoize_ {

    private memoize_() {
    }

    static <Result>.ceylon.language.Callable<? extends Result> memoize(final .com.redhat.ceylon.compiler.java.runtime.model.TypeDescriptor $reified$Result, final .ceylon.language.Callable<? extends Result> f) {
        return (
            let
            {
                Result $ceylontmp$memo$3 = .hello.memoize_.memoize$memo($reified$Result, f);
            }
            returning memoize($reified$Result, f, $ceylontmp$memo$3);
        );
    }

    public static final <Result>Result memoize$memo(final .com.redhat.ceylon.compiler.java.runtime.model.TypeDescriptor $reified$Result, final .ceylon.language.Callable<? extends Result> f) {
        return null;
    }

    static <Result>.ceylon.language.Callable<? extends Result> memoize(final .com.redhat.ceylon.compiler.java.runtime.model.TypeDescriptor $reified$Result, final .ceylon.language.Callable<? extends Result> f, final Result memo$param$) {
        final VariableBox<Result> memo = new VariableBox<Result>(memo$param$);
        return new .com.redhat.ceylon.compiler.java.language.AbstractCallable<Result>($reified$Result, .ceylon.language.Empty.$TypeDescriptor$, "Result()", (short)-1){

            @.java.lang.Override
            public Result $call$() {
                return (
                    let
                    {
                        Result $ceylontmp$op$2 = (
                            let
                            {
                                Result $ceylontmp$1 = memo.ref;
                            }
                            returning $ceylontmp$1 != null ? $ceylontmp$1 : f.$call$();
                        );
                        memo.ref = $ceylontmp$op$2;
                    }
                    returning $ceylontmp$op$2;
                );
            }
        };
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment